Multiple SeekBar activity fatal crash-Collection of common programming errors

I have this MainActivity class in Android with multiple SeekBars:

package com.simplemathgame;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity  implements OnSeekBarChangeListener {
    private TextView numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
    private TextView numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
    private TextView numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
    private TextView numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SeekBar addSeekBar = (SeekBar) findViewById(R.id.add_seek_bar);
        SeekBar subSeekBar = (SeekBar) findViewById(R.id.sub_seek_bar);
        SeekBar mulSeekBar = (SeekBar) findViewById(R.id.mul_seek_bar);
        SeekBar divSeekBar = (SeekBar) findViewById(R.id.div_seek_bar);

        addSeekBar.setOnSeekBarChangeListener(this);
        subSeekBar.setOnSeekBarChangeListener(this);
        mulSeekBar.setOnSeekBarChangeListener(this);
        divSeekBar.setOnSeekBarChangeListener(this);
        Log.w("here","2");
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2){
        Log.w("here","3");
        switch (arg0.getId()) 
        {
        case R.id.add_seek_bar:
            numberOfAddDrills.setText(""+arg1);
            break;
        case R.id.sub_seek_bar:
            numberOfSubDrills.setText(""+arg1);
            break;
        case R.id.mul_seek_bar:
            numberOfMulDrills.setText(""+arg1);
            break;
        case R.id.div_seek_bar:
            numberOfDivDrills.setText(""+arg1);
            break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }   
}

Here is the LogCat:

12-20 08:58:49.402: D/dalvikvm(668): GC_EXTERNAL_ALLOC freed 43K, 53% free 2546K/5379K, external 1917K/2137K, paused 43ms
12-20 08:58:52.563: D/AndroidRuntime(668): Shutting down VM
12-20 08:58:52.563: W/dalvikvm(668): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-20 08:58:52.582: E/AndroidRuntime(668): FATAL EXCEPTION: main
12-20 08:58:52.582: E/AndroidRuntime(668): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.simplemathgame/com.simplemathgame.MainActivity}: java.lang.NullPointerException
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.os.Looper.loop(Looper.java:123)
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-20 08:58:52.582: E/AndroidRuntime(668):  at java.lang.reflect.Method.invokeNative(Native Method)
12-20 08:58:52.582: E/AndroidRuntime(668):  at java.lang.reflect.Method.invoke(Method.java:507)
12-20 08:58:52.582: E/AndroidRuntime(668):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-20 08:58:52.582: E/AndroidRuntime(668):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-20 08:58:52.582: E/AndroidRuntime(668):  at dalvik.system.NativeStart.main(Native Method)
12-20 08:58:52.582: E/AndroidRuntime(668): Caused by: java.lang.NullPointerException
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.app.Activity.findViewById(Activity.java:1647)
12-20 08:58:52.582: E/AndroidRuntime(668):  at com.simplemathgame.MainActivity.(MainActivity.java:11)
12-20 08:58:52.582: E/AndroidRuntime(668):  at java.lang.Class.newInstanceImpl(Native Method)
12-20 08:58:52.582: E/AndroidRuntime(668):  at java.lang.Class.newInstance(Class.java:1409)
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-20 08:58:52.582: E/AndroidRuntime(668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
12-20 08:58:52.582: E/AndroidRuntime(668):  ... 11 more
12-20 08:58:54.923: I/Process(668): Sending signal. PID: 668 SIG: 9

I don’t know where is this java.lang.RuntimeException. Thanks

  1. You should take a look here, and see what’s null at that point:

    12-20 08:58:52.582: E/AndroidRuntime(668): Caused by: java.lang.NullPointerException
    12-20 08:58:52.582: E/AndroidRuntime(668):  at android.app.Activity.findViewById(Activity.java:1647)
    12-20 08:58:52.582: E/AndroidRuntime(668):  at com.simplemathgame.MainActivity.(MainActivity.java:11)
    

    So line 11 is where the crash is.

    The problem is you cannot call findViewById from there, call them inside the OnCreate method, after the setContentView.

    Hope that helps.

  2. You have to include all the findViewById() for TextView’s inside onCreate() because without setContentView() it will return null.

    private TextView numberOfAddDrills;
    private TextView numberOfSubDrills;
    private TextView numberOfMulDrills;
    private TextView numberOfDivDrills;
    
         @Override
            public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
            numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
            numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
            numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);
            .....
        }