2012-07-14 47 views
1

我在Java類中執行以下操作,而不是在Activity中執行以下操作。嘗試將OnClickListener添加到按鈕時發生NullPointerException

LinearLayout buttonLayout = (LinearLayout) parentContext.getLayoutInflater().inflate(R.layout.app_dialog, null); 
Button btnSignIn = (Button) parentContext.findViewById(R.id.BTN_ID_SIGN_IN); 
Button btnCancel = (Button) parentContext.findViewById(R.id.BTN_ID_CANCEL); 
btnSignIn.setOnClickListener(new OnClickListener(){ 
    public void onClick(View v) { ... }}); 

佈局的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/footer" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:orientation="horizontal" 
    android:layout_alignParentBottom="true" style="@android:style/ButtonBar" > 

    <Button android:id="@+id/BTN_ID_SIGN_IN" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_weight="1" 
     android:text="@string/em_btn_signin" /> 

    <Button android:id="@+id/BTN_ID_CANCEL" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_weight="1" 
     android:text="@string/em_btn_cancel" /> 
</LinearLayout> 

最後一行拋出NPE。當我在沒有onClickListener的情況下嘗試我的代碼時,按鈕顯示完美(儘管當我點擊它們時沒有任何反應)。爲什麼它不讓我設置任何按鈕?

+0

你能發表更多的代碼嗎?什麼是parentContext? – 2012-07-14 01:09:33

+0

parentContext是調用Activity的上下文。我是從一個單獨的課程中完成的,這不是一個活動)。 – PlanetaryFortressRush 2012-07-14 01:11:52

+0

爲什麼你有「空」作爲你的膨脹的第二個參數?第二個參數應該是根佈局。 – 0gravity 2012-07-14 01:12:35

回答

2

試試這個:

Button btnSignIn = (Button) buttonLayout.findViewById(R.id.BTN_ID_SIGN_IN); 
Button btnCancel = (Button) buttonLayout.findViewById(R.id.BTN_ID_CANCEL); 
btnSignIn.setOnClickListener(new OnClickListener(){ 
    public void onClick(View v) { ... }}); 
+0

就是這樣,謝謝! – PlanetaryFortressRush 2012-07-14 01:26:37

0

什麼btnSignIn.setOnClickListener(this)是不是你錯過了?

as不應該xml中的ID只能是小寫?

+0

不,它們不必小寫,只有xml文件的名稱必須小寫,並且有多種方法來設置onClickListener。 setOnClickListener(this)僅在您實現了內部OnClickListener類時纔有效。你也可以按照他的方式創建一個新的實例,而不是聲明一個新的內部類。 – skUDA 2012-07-14 01:22:29

0

如果你的按鈕都在裏面R.layout.app_dialog,請撥打:

Button btnSignIn = (Button) buttonLayout.findViewById(R.id.BTN_ID_SIGN_IN); 
Button btnCancel = (Button) buttonLayout.findViewById(R.id.BTN_ID_CANCEL); 
0

嘗試這樣

public class className extends Activity implements OnClickListener { 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.your_layout); 

      Button btnSignIn = (Button) buttonLayout.findViewById(R.id.BTN_ID_SIGN_IN); 
      Button btnCancel = (Button) buttonLayout.findViewById(R.id.BTN_ID_CANCEL); 
} 

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.BTN_ID_SIGN_IN: 
     Intent reg = new Intent(this, Your_Next_Activity.class); 
     startActivity(reg); 
     finish(); 
     break; 

    case R.id.BTN_ID_CANCEL: 
     finish(); 
     break; 

      default:break; 
} 
} 

希望你會得到解決通過這個。

+0

你顯然沒有讀過我說我在活動之外做這件事的部分。 – PlanetaryFortressRush 2012-07-14 06:45:46

+0

oohk ..我力量閱讀正確.. – 2012-07-14 06:49:47

相關問題