2011-04-22 26 views
2

我打算构建一个alertdialog视图并访问TextView来设置其文本值。下面的代码:AlertDialog的组件检索问题 - android

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。任何人都可以帮助我知道我哪里错了!

谢谢

+0

有什么办法可以设置AlertDialog的文字字体大小。我的意思是,我可以更改添加到SetMessage()的文本大小。如果这可以实现,那么我的工作也可以完成。 – Tvd 2011-04-22 10:38:46

回答

2

这似乎工作。玩的开心!

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

谢谢JAL。 myView.findViewbyId()完成了这项工作。 – Tvd 2011-04-25 13:28:25

0

这是什么意思..?

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

**的一样吗?

+0

只是删除**ñ检查 – 2011-04-22 10:18:36

+0

我刚刚在这里发布id文本BOLD时。而已。没有其他的。 – Tvd 2011-04-22 10:35:44

+0

只是评论此行TextView text =(TextView)ad.findViewById(R.id.logTextView_log); n检查对话框n textview是否显示 – 2011-04-22 10:47:12

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

然后我得到“requestFeature()必须在ad.show()添加内容之前调用”异常。 – Tvd 2011-04-22 10:34:10