简体   繁体   中英

Android: Application crash on Start (Opening intent on Button)

I am trying to open another activity through Intent on Button press. But the app crashes every time on launch.

IntentTest.java:

package com.example.intenttest;

public class IntentTest extends Activity {

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

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent2 = new Intent(IntentTest.this, IntentTest2.class);
            startActivity(intent2);
        }
    });

  }

}

IntentTest2.java:

package com.example.intenttest;

public class IntentTest2 extends Activity {

TextView textView1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intent_test2);
    textView1=(TextView)findViewById(R.id.textView1);
    }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intenttest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.intenttest.IntentTest"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:label="@string/app_name" android:name="com.example.intenttest.IntentTest2" />
</application>

</manifest>

Here is the LogCat where the app crashes:

12-18 11:59:59.563: W/Trace(20985): error opening trace file: No such file or directory    (2)
12-18 11:59:59.623: D/AndroidRuntime(20985): Shutting down VM
12-18 11:59:59.623: W/dalvikvm(20985): threadid=1: thread exiting with uncaught exception (group=0x40c8d930)
12-18 11:59:59.633: E/AndroidRuntime(20985): FATAL EXCEPTION: main
12-18 11:59:59.633: E/AndroidRuntime(20985): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intenttest/com.example.intenttest.IntentTest}:   java.lang.NullPointerException

I cant find out what is wrong. Please help.

Because you did not initialize b1(Button)

 b1 = (Button)findViewById(R.id.button_id);

b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent2 = new Intent(IntentTest.this, IntentTest2.class);
            startActivity(intent2);
        }
    });

you forgot to initialze button object in IntentTest

b1=(Button)findViewById(R.id.b1);

like this way with your button id.

You did not initialize your button b1 in IntentTest class,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intent_test);
    b1=(Button)findViewById(R.id.your_btn_name);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent2 = new Intent(IntentTest.this, IntentTest2.class);
            startActivity(intent2);
        }
    });

  }

Button b1 is not initialized. So it's null.

Fisrt initialize your button

Button b1 = (Button)findViewById(R.id.Buttonid);
b1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent intent2 = new Intent(IntentTest.this, IntentTest2.class);
        startActivity(intent2);
    }
});

else directly use like this

  findViewById(R.id.Buttonid).setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent intent2 = new Intent(IntentTest.this, IntentTest2.class);
        startActivity(intent2);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM