Monday, November 23, 2009

[android-developers] Re: custom dialog crashing

ok! i moved the code to onCreateDialog

and even tough vg is null, it created the alert. but should vg be
null ?

but it crashed again !

here is code

protected Dialog onCreateDialog(int id) {


AlertDialog.Builder builder;
AlertDialog alertDialog = null;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
(LAYOUT_INFLATER_SERVICE);

switch(id) {
case MY_DIALOG_ID:


ViewGroup vg = (ViewGroup) findViewById(R.id.layout_root);
View layout = inflater.inflate(R.layout.mydialog,
vg);

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

break;

default:

alertDialog = null;
}

return alertDialog;
}

void CheckHighScore() {

showDialog(MY_DIALOG_ID);

}

On Nov 23, 8:26 pm, Jags <jag...@gmail.com> wrote:
> I understand that this is coming null because my current activity
> layout is main.xml. but is it that i need to do a setlayout to
> mydialog before starting any of my code ?
>
> On Nov 23, 8:11 pm, Jags <jag...@gmail.com> wrote:
>
> > I have written below code for showing a custom dialog
>
> >         void CheckHighScore() {
>
> >                         AlertDialog.Builder builder;
> >                         AlertDialog alertDialog;
>
> >                         Context mContext = getApplicationContext();
> >                         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
> > (LAYOUT_INFLATER_SERVICE);
> >                         ViewGroup vg = (ViewGroup) findViewById(R.id.layout_root);
> >                         View layout = inflater.inflate(R.layout.mydialog,
> >                                                        vg);
>
> >                         builder = new AlertDialog.Builder(mContext);
> >                         builder.setView(layout);
> >                         alertDialog = builder.create();
>
> >                         alertDialog.show();
>
> >         }
>
> > __________
> > xml file
> > __________
>
> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> > android"
> >         android:id="@+id/layout_root" android:orientation="vertical"
> >         android:layout_width="fill_parent"
> > android:layout_height="fill_parent"
> >         android:padding="10dp">
> >         <TextView android:id="@+id/desc" android:text="@string/ask_info"
> > android:layout_width="wrap_content"
> >                 android:layout_height="wrap_content" android:textColor="#FFF" />
>
> >         <EditText android:text="Enter your info here" android:id="@+id/
> > yourinfo" android:layout_width="wrap_content"
> > android:layout_height="wrap_content"></EditText>
> > </LinearLayout>
>
> > it is unable to find ViewGroup in above code and is crashing. I have
> > another main.xml file which is set to content layout in this activity.
> > So how can I correct this thing ?
>
> > Also I need to add some validation here to this dialog like info
> > cannot be empty !
>
> > thanks in advance
> > Jags

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate