开发者

Error in inflating a layout into AlertDialog (Android)

开发者 https://www.devze.com 2023-03-09 14:08 出处:网络
public void popInstructionsDialog(String title, String text, String buttonText, Activity activity){ AlertDialog.Builder builder;
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();
}

I get the following error

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 is the last line of code which is "alertDialog.show()". Can any one suggest me how to correct this. And following is the 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="horiz开发者_开发知识库ontal"
              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>


Make the mcontext to be public and set try

mContext =this in oncreate method

and use this while inflating the alertdialog.... I have been hit by this problem many a times... The only solution that i use to solve this is this one...

Hope this helps...


Try changing

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

to

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

I think your top-level layout ID layout_root isn't being found by the inflater


try this

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

精彩评论

暂无评论...
验证码 换一张
取 消