【问题标题】:Problem retriving components of AlertDialog - android检索 AlertDialog 组件的问题 - android
【发布时间】:2011-08-10 21:05:43
【问题描述】:

我正在尝试使用视图构建警报对话框并访问 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。谁能帮我知道我哪里出错了!

谢谢

【问题讨论】:

  • 有没有办法设置AlertDialog的文字字体大小。我的意思是,我可以更改添加到 SetMessage() 的文本大小吗?如果能做到这一点,那我的工作也能完成。

标签: android android-alertdialog uicomponents


【解决方案1】:

这似乎有效。玩得开心!

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

【讨论】:

  • 感谢日航。 myView.findViewbyId() 完成了这项工作。
【解决方案2】:

这是什么意思..?

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

** 表示..?

【讨论】:

  • 在这里发帖时,我刚刚将 id 文本设为 BOLD。而已。没有别的了。
  • 只评论这一行 TextView text = (TextView)ad.findViewById(R.id.logTextView_log); n 检查对话框 n textview 是否会显示
  • 是 CapDroid,如果我删除 TextView 部分,那么可以完美地看到对话框。所以问题在于检索 TextView !!!!
【解决方案3】:

为什么不首先将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() {   

【讨论】:

  • 然后我在 ad.show() 处得到“必须在添加内容之前调用 requestFeature()”异常。
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 2011-10-17
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多