2011-06-01 3 views
4
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(); 
} 

je reçois l'erreur suivanteerreur à gonfler une mise en page dans AlertDialog (Android)

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 est la dernière ligne de code qui est "alertDialog.show()". Quelqu'un peut-il me suggérer comment corriger cela. Et suivant est le 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> 

Répondre

2

Faire le mcontext soit public et mis essayer

mContext =this in oncreate method 

et utiliser ce tout en gonflant le AlertDialog .... Je suis touché par ce problème maint fois ... La seule solution que j'utiliser pour résoudre ce problème est celui-ci ...

Hope this helps ...

0

essayer cette

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

Essayez de changer

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

à

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

Je pense que votre mise en page de haut niveau ID layout_root on ne trouve pas le gonfleur

Questions connexes