1

アクティビティ内からではなく、Java クラス内で次のことを行っています。

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 なしでコードを試すと、ボタンが完全に表示されます (ボタンをタップしても何も起こりません)。ボタンに何も設定できないのはなぜですか?

4

4 に答える 4

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) { ... }});
于 2012-07-14T01:19:10.900 に答える
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);
于 2012-07-14T01:23:56.033 に答える
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;
}
}

これで解決することを願っています。

于 2012-07-14T06:07:34.577 に答える
0

見逃して btnSignIn.setOnClickListener(this)いませんか?

また、xml の ID は小文字のみであるべきではありませんか?

于 2012-07-14T01:17:04.240 に答える