0

我有一个带有 3 个按钮的活动来启动相同的活动,但布局不同。按钮 2 和 3 正在工作,但按钮 1 给出了运行时异常(应用程序崩溃)。这是源代码:

       public class Choose extends Activity implements OnClickListener{

    Button sniper;
    Button machine;
    Button colt;
    int btnsound;
    Intent t;

    SoundPool spool;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose);


        t = new Intent(Choose.this, MainActivity.class);
        sniper=(Button)findViewById(R.id.button1);
        machine=(Button)findViewById(R.id.button2); 
        colt=(Button)findViewById(R.id.button3);
        colt.setOnClickListener(this);
        sniper.setOnClickListener(this);
        machine.setOnClickListener(this);
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        spool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);       
        btnsound= spool.load(this, R.raw.btn, 1);






    }


      public void btnsound(){
            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            spool.play(btnsound, volume, volume, 1, 0, 1f);
            }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {


        case R.id.button1:

            t.putExtra("button", 1);
            startActivity(t); 
            btnsound();
          break;

        case R.id.button2:


            t.putExtra("button", 2);
            startActivity(t); 
            btnsound();
          break;

        case R.id.button3:


            t.putExtra("button", 3);
            startActivity(t); 
            btnsound();
          break;


    }

    }


}

布局代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/choosewall" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="none" >


        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             >

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"


                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1" >


                    <Button
        android:id="@+id/button1"
        android:layout_width="300sp"
        android:layout_height="100sp"       
        android:layout_centerHorizontal="true"
        android:background="@drawable/sniperstyle" />


                    </TableRow>


            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"


                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1" >


                <Button
        android:id="@+id/button2"
        android:layout_width="300sp"
        android:layout_height="100sp"       
        android:layout_centerHorizontal="true"
        android:background="@drawable/machinegunstyle" />

                </TableRow>

                       <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"


                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1" >


                <Button
        android:id="@+id/button3"
        android:layout_width="300sp"
        android:layout_height="100sp"       
        android:layout_centerHorizontal="true"
        android:background="@drawable/pistolstyle" />

                </TableRow>

            </TableLayout>


    </ScrollView>







</RelativeLayout>

单击 button1 时的堆栈跟踪:

07-15 00:30:56.202: E/AndroidRuntime(32404): FATAL EXCEPTION: main
07-15 00:30:56.202: E/AndroidRuntime(32404): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jouni.camsniper/com.jouni.camsniper.MainActivity}: java.lang.NullPointerException
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.os.Looper.loop(Looper.java:137)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at java.lang.reflect.Method.invokeNative(Native Method)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at java.lang.reflect.Method.invoke(Method.java:511)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at dalvik.system.NativeStart.main(Native Method)
07-15 00:30:56.202: E/AndroidRuntime(32404): Caused by: java.lang.NullPointerException
07-15 00:30:56.202: E/AndroidRuntime(32404):    at com.jouni.camsniper.MainActivity.onCreate(MainActivity.java:73)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.app.Activity.performCreate(Activity.java:5104)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-15 00:30:56.202: E/AndroidRuntime(32404):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-15 00:30:56.202: E/AndroidRuntime(32404):    ... 11 more
07-15 00:30:58.664: I/Process(32404): Sending signal. PID: 32404 SIG: 9
4

2 回答 2

1
07-15 00:30:56.202: E/AndroidRuntime(32404): Caused by: java.lang.NullPointerException
07-15 00:30:56.202: E/AndroidRuntime(32404):    at com.jouni.camsniper.MainActivity.onCreate(MainActivity.java:73)

is rather obvious. You are calling a method on reference that points to nothing on line 73 in MainActivity.java.

于 2013-07-14T22:04:52.860 回答
1

对于那些在单击注册按钮时发生崩溃的人。我从 register.java 文件中删除了这段代码,现在它可以工作了。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){ ActionBar actionBar = getActionBar(); actionBar.hide();

}

于 2014-08-24T00:22:07.383 回答