2013-12-18 71 views
-1

我試圖通過按下按鈕意向來打開另一個活動。 但應用程序崩潰,在啓動每一次安卓:在啓動應用程序崩潰(上按鈕開口意圖)

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 

我無法找出什麼是錯的。請幫忙。

+0

使用B1 =(按鈕)findViewById(R.id.b1)初始化您的按鈕B1; –

+0

X- |我現在感到很尷尬。我怎麼會想念那個。非常感謝。 –

回答

3

因爲你沒有初始化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); 
     } 
    }); 
+0

我怎麼會想念那個。非常感謝! –

+0

@AniketKapse ple接受我的答案。 – Hariharan

+0

是的,我會的。我必須再等一分鐘:) –

0

你忘了initialze按鍵對象IntentTest

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

這樣的方式與您的按鈕的ID。

0

你沒有初始化IntentTest類的按鈕B1,

@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); 
     } 
    }); 

    } 
0

按鈕B1未初始化。所以它是空的。

0

最前一頁初始化按鈕

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); 
    } 
}); 

否則直接使用這樣

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

    @Override 
    public void onClick(View v) { 

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