2012-01-26 260 views
1

我剛剛實現了onClickListener。該應用程序應該在兩個佈局之間切換。在每個佈局上有一個按鈕。觸摸按鈕後,佈局應該改變。實施onClickListener後應用程序崩潰

我搜索槽通過各種教程和紀錄片,我的代碼應該是正確的,我根本沒有看到錯誤。 Eclipse也表示代碼沒問題。

package ralationship.v1; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class RelationshipActivity extends Activity implements OnClickListener{ 
    /** Called when the activity is first created. */ 
    private Button OptionsButton; 
    private Button SaveButton; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     SaveButton = (Button)findViewById(R.id.btnSave); 
     OptionsButton = (Button)findViewById(R.id.btnOptions); 
     OptionsButton.setOnClickListener(this); 
     SaveButton.setOnClickListener(this); 

    } 

    public void onClick(View v) { 
     if(v.getId() == (R.id.btnOptions)){ 
      setContentView(R.layout.options); 
     }else if (v.getId() == (R.id.btnSave)){ 
      setContentView(R.layout.main); 
     } 

    } 

} 

有沒有人會發現錯誤?

//編輯:

這裏是logcat的報告:

01-26 15:47:29.273: D/dalvikvm(336): GC_EXTERNAL_ALLOC freed 49K, 53% free 2549K/5379K, external 1625K/2137K, paused 67ms 
01-26 15:47:29.453: D/AndroidRuntime(336): Shutting down VM 
01-26 15:47:29.462: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
01-26 15:47:29.473: E/AndroidRuntime(336): FATAL EXCEPTION: main 
01-26 15:47:29.473: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{ralationship.v1/ralationship.v1.RelationshipActivity}: java.lang.NullPointerException 
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
01-26 15:47:29.473: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-26 15:47:29.473: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123) 
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:3683) 
01-26 15:47:29.473: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method) 
01-26 15:47:29.473: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:507) 
01-26 15:47:29.473: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
01-26 15:47:29.473: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
01-26 15:47:29.473: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method) 
01-26 15:47:29.473: E/AndroidRuntime(336): Caused by: java.lang.NullPointerException 
01-26 15:47:29.473: E/AndroidRuntime(336): at ralationship.v1.RelationshipActivity.onCreate(RelationshipActivity.java:22) 
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
01-26 15:47:29.473: E/AndroidRuntime(336): ... 11 more 
01-26 15:47:33.633: I/Process(336): Sending signal. PID: 336 SIG: 9 
+0

從logcat發佈錯誤。 – kosa

+0

@凱文,發佈logcat報告.. –

回答

1

我假設你saveButton是在R.layout.options佈局?如果是這樣,當您的活動首次創建時無法找到它,因爲您只有R.layout.main的內容。而應該把線

SaveButton = (Button)findViewById(R.id.btnSave); 
SaveButton.setOnClickListener(this); 

onClickListener裏面,你有setContentLayout()改變佈局,以正確的佈局之後。

+0

,爲我工作,非常感謝你 – Kevin

1

如果選項按鈕的選項XML比你必須手動充氣這一點。當您調用setContentView並分配佈局xml時,您可以自動從該特定xml上的內容膨脹。如果您需要充氣別的,你必須使用一個layoutInflater context.getSystemService(context.layoutInflaterservice)

基本上不直到你設置的內容以它實例化選項按鈕