单击列表项时出现应用程序错误

时间:2011-09-07 13:01:43

标签: android dialog android-alertdialog

当我点击列表项时我得到错误并关闭应用程序。怎么解决?哪里错了。 我需要在自定义对话框窗口中显示文本WTF

customdialoglayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/layout_root"
          android:orientation="vertical"
          android:background="@drawable/kmp"              
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

<TextView android:id="@+id/dialog_title"        
    android:textColor="#FFF"
    android:textSize="16sp"
    android:layout_width="fill_parent" android:gravity="center"  android:layout_height="30dp" android:layout_marginTop="5dp"/>

<TextView android:id="@+id/dialog_text"     
    android:textColor="#FFF"
    android:layout_height="35dp" android:layout_width="fill_parent"  android:gravity="center"/>

中的

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String toastMessage = messages.get(position).toString();
    Builder customdialoglayout = new AlertDialog.Builder(this)
    .setCancelable(true)
    .setIcon(R.drawable.icon);
    TextView text = (TextView) findViewById(R.id.dialog_text);
    text.setText("WTF");
    setContentView(R.layout.customdialoglayout);    
}

已修复问题:

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.customdialoglayout);
    dialog.setTitle(rssTitle);
    dialog.setCancelable(true);
    TextView text = (TextView) dialog.findViewById(R.id.dialog_text);
    text.setText("WTF");
    dialog.show();

1 个答案:

答案 0 :(得分:0)

您必须使用 .Builder

here对Android开发指南的引用!

    AlertDialog.Builder yourDialog =  new AlertDialog.Builder(this);
    yourDialog .setTitle(rssTitle);
    dialog.show();

AlertDialog.Builder yourDialog = new AlertDialog.Builder(this); yourDialog .setTitle(rssTitle); dialog.show();