我試圖通過按下按鈕意向來打開另一個活動。 但應用程序崩潰,在啓動每一次。安卓:在啓動應用程序崩潰(上按鈕開口意圖)
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);
}
}
清單:
<?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>
這裏在logcat的在應用程序崩潰:
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
我無法找出什麼是錯的。請幫忙。
使用B1 =(按鈕)findViewById(R.id.b1)初始化您的按鈕B1; –
X- |我現在感到很尷尬。我怎麼會想念那個。非常感謝。 –