多个SeekBar活动致命崩溃

时间:2012-12-20 09:00:12

标签: android logcat

我在Android中有多个SeekBars的MainActivity类:

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

    }   
}

这是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.<init>(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

我不知道这个java.lang.RuntimeException在哪里。感谢

2 个答案:

答案 0 :(得分:2)

你应该看看这里,看看那时的什么是空的:

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.<init>(MainActivity.java:11)

因此第11行是崩溃的地方。

问题是你不能从那里调用findViewById,在setContentView之后在OnCreate方法中调用它们。

希望有所帮助。

答案 1 :(得分:2)

您必须在findViewById()内包含TextView的所有onCreate(),因为如果没有setContentView(),它将返回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);
        .....
    }