0

I have an activity with 3 buttons to start the same activity but with different layouts.Button 2 & 3 are working but button 1 is giving a runtime exception (the app is crashing).This is the source code:

       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;


    }

    }


}

Layout code:

    <?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>

Stack Trace when button1 is clicked:

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
0

2 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

1

for those of you with the crash when you click the register button. i removed this bit of code from the register.java file and now it works.

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.