2011-09-27 119 views
0

如何爲第二個佈局實現Buttonlistener,該佈局仍在主Acitivity中調用? 我已經通過一個名爲Button的偵聽器並通過匿名嘗試了它。但仍然得到空指針異常。在第二個佈局中製作Buttonistener

代碼:

back = (Button) findViewById(R.id.backToMain); 
if(back != null) 
    back.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 
        setLayout(R.layout.main); 
       } 
      }); 

Layout.xml

<Button android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:id="@+id/backToMain" 
     android:text="Zurück"> 
    </Button> 
+0

我想你應該提供一些更多的細節...... – LambergaR

+0

你是否想要自定義監聽器? –

回答

2

試試這個

更改按鈕ATTR標籤

<Button android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:id="@+id/backToMain" 
     android:text="Zurück" 
     android:onClick="goBack"> 
    </Button> 

在您的活動創建一個方法

public void goBack(View v) { 
//Write code here 
} 
+0

我試過這個,但是我使得這個方法是私人的而不是公開的, – Sim

相關問題