开发者

Problem retriving components of AlertDialog - android

开发者 https://www.devze.com 2023-02-28 05:45 出处:网络
开发者_运维知识库I am tring to build an alertdialog with a view and access a TextView to set its text value. Here\'s the code :

开发者_运维知识库I am tring to build an alertdialog with a view and access a TextView to set its text value. Here's the code :

    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>

Why an I not able to access TextView ? it returns null and throws NullPointerException. I can't access directly using findBiewById, so am using ad.findViewById, yet I receive it null only. Can anyone help me know where am I going wrong !

Thanks


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();


what is meaning of this..?

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

** means..?


Why not returning LayoutInflater.from(this).inflate(R.layout.log_layout, null) to a View variable first ? (The below code is untested)

    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() {   
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号