将布局膨胀到AlertDialog(Android)时出错

时间:2011-06-01 11:04:33

标签: android

public void popInstructionsDialog(String title, String text, String buttonText, Activity activity){

    AlertDialog.Builder builder;
    AlertDialog alertDialog;

    Context mContext = activity.getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.instructions, (ViewGroup) activity.findViewById(R.id.layout_root));

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

    builder = new AlertDialog.Builder(mContext);
    builder.setView(layout);
    alertDialog = builder.create();

    alertDialog.show();
}

我收到以下错误

06-01 10:59:20.908: ERROR/AndroidRuntime(664): FATAL EXCEPTION: main
06-01 10:59:20.908: ERROR/AndroidRuntime(664): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-01 10:59:20.908: ERROR/AndroidRuntime(664):     at android.view.ViewRoot.setView(ViewRoot.java:531)
06-01 10:59:20.908: ERROR/AndroidRuntime(664):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-01 10:59:20.908: ERROR/AndroidRuntime(664):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-01 10:59:20.908: ERROR/AndroidRuntime(664):     at android.app.Dialog.show(Dialog.java:241)
06-01 10:59:20.908: ERROR/AndroidRuntime(664):     at hiit.nopsa.pirate.InstructionDialog.popInstructionsDialog(InstructionDialog.java:34)

InstructionDialog.java:34是代码的最后一行,即“alertDialog.show()”。任何人都可以建议我如何纠正这个问题。以下是instructions.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"
              >
    <TextView android:id="@+id/title"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
    <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>

3 个答案:

答案 0 :(得分:2)

将mcontext设为公开并设置try

mContext =this in oncreate method

并在使用alertdialog时充气使用....我已经被这个问题多次击中了......我用来解决这个问题的唯一解决方案就是这个......

希望这会有所帮助......

答案 1 :(得分:1)

尝试更改

View layout = inflater.inflate(R.layout.instructions, (ViewGroup) activity.findViewById(R.id.layout_root));

View layout = inflater.inflate(R.layout.instructions, null);

我认为inflater找不到您的顶级布局ID layout_root

答案 2 :(得分:0)

试试这个

    AlertDialog.Builder alert = new AlertDialog.Builder(activity.this);     
    LayoutInflater  factory = LayoutInflater.from(activity.this);
    View layout =factory.inflate(R.layout.instructions,null);
    TextView t = (TextView)layout.findViewById(R.id.text);
    t.setText("message");
    alert.setView(layout);
相关问题