1

我在带有多个 SeekBars 的 Android 中有这个 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 在哪里。谢谢

4

2 回答 2

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 方法中调用它们。

希望有帮助。

于 2012-12-20T09:05:20.873 回答
2

您必须在findViewById()里面包含所有 for 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);
        .....
    }
于 2012-12-20T09:05:44.743 回答