I have an alert dialog started in onCreate that lets user to choose from two different activities to start from. I used the alert dialog box that has positive and negative values but unfortunately I get an error. Is it not possible to use this kind of alert dialogs to run two different activities? if yes how is it possible? I get this kind of error:
[Android Application]
[Android Application]
[Android Application]
[Android Application]
[Android Application]
[Android Application]
[Android Application]
[Android Application]
DalvikVM [localhost:8602]
Thread [<1> main] (Running)
Thread [<10> Binder_2] (Running)
Thread [<9> Binder_1] (Running)
Thread [<11> Thread-198] (Suspended (exception RuntimeException))
<VM does not provide monitor information>
Handler.<init>(Handler$Callback, boolean) line: 200
Handler.<init>() line: 114
AlertDialog(Dialog).<init>(Context, int, boolean) line: 109
AlertDialog.<init>(Context, int, boolean) line: 114
AlertDialog$Builder.create() line: 931
MainActivity$splashscreen.run() line: 68
[Android Application]
here is my code for the alert dialog box
public class MainActivity extends Activity {
//ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Removes title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Removes notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
splashscreen view = new splashscreen();
view.start();
}
public class splashscreen extends Thread{
public void run(){
try{
Thread.sleep(3*1000);
}catch(Exception e){
Log.v("Exception", e.toString());
}
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose your activity to start");
builder.setMessage("Pick desired activity");
builder.setPositiveButton("Office",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent home = new Intent(getApplicationContext(), OfficeActivity.class);
startActivity(home);
finish();
//dialog.cancel();
}
});
builder.setNegativeButton("School", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent home = new Intent(getApplicationContext(), SchoolActivity.class);
startActivity(home);
finish();
//dialog.cancel();
}
});
AlertDialog alertdialog=builder.create();
alertdialog.show();
}
}
}
This is the solution for your problem,try this...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose your activity to start");
builder.setMessage("Pick desired activity");
builder.setPositiveButton("Office",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent home = new Intent(getApplicationContext(), OfficeActivity.class);
startActivity(home);
finish();
}
});
builder.setNegativeButton("School", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent home = new Intent(getApplicationContext(), SchoolActivity.class);
startActivity(home);
finish();
}
});
AlertDialog alertdialog=builder.create();
alertdialog.show();
}
}, 4000);
}