对话框崩溃的应用程序

时间:2011-10-31 16:49:29

标签: android button dialog

好的只是要知道,我从android网站上复制粘贴代码,所以我认为没有错。

问题是,当我按下按钮时它会崩溃。它会从对话框代码崩溃,因为我没有其他任何东西。

代码是:

MainDialog.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="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="10dp"
          >
          <ImageView 
           android:id="@+id/image"
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
           android:layout_marginRight="10dp"
           />
          <TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:textColor="#FFF"
          />

          </LinearLayout>

在按下button1的情况下:

                    Context mContext = getApplicationContext();
        Dialog dialog = new Dialog(mContext);

        dialog.setContentView(R.layout.maindialog);
        dialog.setTitle("Custom Dialog");

        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Hello, this is a custom dialog!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.icon);    
        dialog.show();  

1 个答案:

答案 0 :(得分:1)

您使用Activity上下文并添加对dialog.show();的调用:

    Context mContext = this; //Assumes you are calling this from within an activity
    Dialog dialog = new Dialog(mContext);

    dialog.setContentView(R.layout.maindialog);
    dialog.setTitle("Custom Dialog");

    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");
    ImageView image = (ImageView) dialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.icon);    
    dialog.show();