PDA

View Full Version : Dialog



DragonFire353
02-04-2011, 09:58 PM
It says I have no errors but when I click the button the application stops responding. Here is the code:
package com.failtestfail.dragonfire353;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class DragonFire353 extends Activity {
Button sephirothButton;
/** Called when the activity is first created. */
Context context=this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sephirothButton = (Button)findViewById(R.id.sephirothbutton);
sephirothButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Context mContext = getApplicationContext();
Dialog sephirothdialog = new Dialog(mContext);

sephirothdialog.setContentView(R.layout.sephiroth) ;
sephirothdialog.setTitle("SEPHIROTH!!!!");
sephirothdialog.setCancelable(true);

TextView sephirothtext = (TextView) sephirothdialog.findViewById(R.id.sephirothtext);
sephirothtext.setText("WINBERRIEZ!!! YO MEATBILLY IS HIGHLY ACTIVE!!!");
ImageView sephirothimage = (ImageView) sephirothdialog.findViewById(R.id.sephirothimage);
sephirothimage.setImageResource(R.drawable.sephiro thuavatar);
sephirothdialog.show();
}
});
}
}

Scythe
02-04-2011, 10:22 PM
What does logcat say?

DragonFire353
02-04-2011, 11:09 PM
02-04 23:07:27.016: WARN/WindowManager(52): Attempted to add window with non-application token WindowToken{44c07518 token=null}. Aborting.

alostpacket
02-05-2011, 08:53 PM
You're trying to add the dialog to the button onClickListener

You need to call a method in the activity to add the dialog or override onCreateDialog() in the activity.

http://developer.android.com/guide/topics/ui/dialogs.html


try something more like this:


static final int MY_DIALOD_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sephirothButton = (Button)findViewById(R.id.sephirothbutton);
sephirothButton.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
showMyDialog();
}
});
}

private void showMyDialog()
{
showDialog(MY_DIALOD_ID);
}


@Override
protected Dialog onCreateDialog(int id)
{
Dialog sephirothdialog

switch(id)
{
case MY_DIALOD_ID:

sephirothdialog = new Dialog(this);

sephirothdialog.setContentView(R.layout.sephiroth) ;
sephirothdialog.setTitle("SEPHIROTH!!!!");
//do your other stuff

break;

}

return sephirothdialog ;
}

DragonFire353
02-05-2011, 10:24 PM
wow ty so much

alostpacket
02-06-2011, 05:47 PM
you're welcome :)