[SOLVED]issue with custom dialog

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

[SOLVED]issue with custom dialog

Postby Lenea » Fri Jan 01, 2010 8:04 pm

hey guys, i'm trying to display a custom dialog whenever a button is clicked
here is my java code where the dialog is built
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. case R.id.PlayerOneName:
  3.  
  4.                         Toast.makeText(getBaseContext(), "salut", Toast.LENGTH_SHORT).show();
  5.  
  6.                        
  7.  
  8.                         AlertDialog.Builder builder;
  9.  
  10.                         AlertDialog alertDialog;
  11.  
  12.  
  13.  
  14.                         Context mContext = getApplicationContext();
  15.  
  16.                         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  17.  
  18.                         View layout = inflater.inflate(R.layout.player_name_dialog,
  19.  
  20.                                                        (LinearLayout) findViewById(R.id.root_view));
  21.  
  22.  
  23.  
  24.                        
  25.  
  26.                        
  27.  
  28.  
  29.  
  30.                         builder = new AlertDialog.Builder(mContext);
  31.  
  32.                         builder.setView(layout);
  33.  
  34.                         alertDialog = builder.create();
  35.  
  36.                         alertDialog.show();
  37.  
  38.                        
  39.  
  40.                  
  41.  
  42.                        
  43.  
  44.                        
  45.  
  46.                         break;
  47.  
  48.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

and my player_name_dialog.xml looks like this
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.  
  3.  
  4. <LinearLayout android:layout_width="fill_parent"
  5.  
  6.         android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.         android:id="@+id/root_view">
  9.  
  10.         <EditText android:layout_width="wrap_content"
  11.  
  12.                 android:layout_height="wrap_content" android:padding="20dp"
  13.  
  14.                 android:id="@+id/editPlayerNames" android:text="PlayerOne"></EditText>
  15.  
  16.         <Button android:layout_width="wrap_content"
  17.  
  18.                 android:layout_height="wrap_content" android:text="OK"
  19.  
  20.                 android:id="@+id/buttonPlayerNames" android:padding="20dp"></Button>
  21.  
  22. </LinearLayout>
  23.  
  24.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


any ideas?


Cheers,
Lenea
Last edited by Lenea on Sat Jan 02, 2010 2:20 am, edited 3 times in total.
Lenea
Freshman
Freshman
 
Posts: 6
Joined: Mon Dec 28, 2009 12:49 am

Top

Postby jwei512 » Fri Jan 01, 2010 9:55 pm

Do you get a "Cannot add window to view - null token not accepted" error? Something along those lines?

If so, then try to change

Context mContext = getApplicationContext();

To just (I'm going to name your class A):

LayoutInflater inflater = (LayoutInflater) A.this.getSystemService(LAYOUT_INFLATER_SERVICE);
builder = new AlertDialog.Builder(A.this);

I think I ran into a similar problem a few days ago and that was the fix for me at least.

Let me know if it works.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby grzegorz.zajac » Fri Jan 01, 2010 10:24 pm

Hi Lenea,

In line:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. View layout = inflater.inflate(R.layout.player_name_dialog, (LinearLayout) findViewById(R.id.root_view));
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

you are trying to find a view by its ID, but the view is not yet inflated from the XML file.

Try this way:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. View layout = inflater.inflate(R.layout.player_name_dialog, null);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Postby Lenea » Fri Jan 01, 2010 10:28 pm

thanks alot for the tip guys, works perfectly now. i owe you one


Cheers,
Lenea
Lenea
Freshman
Freshman
 
Posts: 6
Joined: Mon Dec 28, 2009 12:49 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests