2

ビューを使用してアラートダイアログを作成し、TextView にアクセスしてテキスト値を設定しようとしています。コードは次のとおりです。

    private void ShowLogDialog() {
    AlertDialog ad = new AlertDialog.Builder(this).create();
    ad.setIcon(R.drawable.icon);
    ad.setTitle("Ultimate Logs");
    ad.setView(LayoutInflater.from(this).inflate(R.layout.log_layout, null));
    TextView text = (TextView)ad.findViewById(R.id.logTextView_log);  // RETURNS NULL
    //Log.i(TAG, "Into ShowLogDialog : GoT TextView = " + text);
    //String logsText = "";
    //text.setText(logsText);
    ad.setButton("Ok", new DialogInterface.OnClickListener() {          
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    ad.show();
}

log_layout.xml

   <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" 
            android:orientation="vertical" android:scrollbars="vertical" android:scrollbarAlwaysDrawVerticalTrack="true" xmlns:android="http://schemas.android.com/apk/res/android">
            <TextView android:layout_height="wrap_content" android:text="TextView" android:id="@+id/**logTextView_log**" android:layout_width="wrap_content"></TextView>
        </LinearLayout>

TextView にアクセスできないのはなぜですか? null を返し、NullPointerException をスローします。findBiewById を使用して直接アクセスできないため、ad.findViewById を使用していますが、null しか受信しません。どこが間違っているのか誰でも教えてくれますか!

ありがとう

4

3 に答える 3

2

This seems to work. Have fun!

        AlertDialog.Builder builder= new AlertDialog.Builder(this);
        LayoutInflater inflater= getLayoutInflater();
        final View myView= inflater.inflate(R.layout.alert_dialog_text_entry, null);
        builder.setTitle("About");
        builder.setMessage("Test");
        builder.setView(myView);
        AlertDialog alert= builder.create();
        TextView stateful= (TextView)myView.findViewById(R.id.TextView01);
        stateful.setText("More Testing");
        Log.d(Utilities.TAG,stateful.toString());
        alert.show();
于 2011-04-22T20:01:39.917 に答える
0

これの意味は何ですか..?

android:id="@+id/**logTextView_log**

** 意味..?

于 2011-04-22T10:05:35.193 に答える
0

LayoutInflater.from(this).inflate(R.layout.log_layout, null)最初にView変数に戻らないのはなぜですか? (以下のコードはテストされていません)

    ad.setTitle("Ultimate Logs");
    View inflatedView = LayoutInflater.from(this).inflate(R.layout.log_layout, null);         
    TextView text = (TextView)inflatedView.findViewById(R.id.logTextView_log);  // RETURNS NULL
    Log.i(TAG, "Into ShowLogDialog : GoT TextView = " + text);
    String logsText = "";
    text.setText(logsText);
    ad.setView(inflatedView );
    ad.setButton("Ok", new DialogInterface.OnClickListener() {   
于 2011-04-22T10:13:18.650 に答える