Use same AlertDialog in onCreate and onActivityResult

Put your problem here if it does not fit any of the other categories.

Use same AlertDialog in onCreate and onActivityResult

Postby matinba » Sat Nov 29, 2008 2:33 am

Hi,

sorry if I'm missing something very basic here, but I can't find a way to declare a AlertDialog so I can use it in onCreate() and also in a catch() block of onActivityResult();

My Way was defining the dialog variable before onCreate():
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. AlertDialog.Builder MyDialog;
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


and assigning its settings in onCreate():
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. AlertDialog.Builder MyDialog =  new AlertDialog.Builder(this)
  2.  
  3.                         .setCancelable(false)
  4.  
  5.                         .setMessage(sDefaultMessage)
  6.  
  7.                         .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
  8.  
  9.  
  10.  
  11.                                 public void onClick(DialogInterface dialog, int whichButton) {
  12.  
  13.                                         startActivity(new Intent(Session.this, Dial.class));
  14.  
  15.                                 }
  16.  
  17.                         });
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


It gives me a NullPointerException unless I copy and paste the code to onActivityResult.
Maybe someone can give me advice.
matinba
Freshman
Freshman
 
Posts: 5
Joined: Thu Oct 16, 2008 3:50 pm

Top

Postby guillaume » Sat Nov 29, 2008 1:45 pm

Hi,

It may be because you wrote twice "AlertDialog.Builder MyDialog"

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. AlertDialog.Builder MyDialog ; // so MyDialog is a member of your activity
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


and

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. AlertDialog.Builder MyDialog = new ... // so MyDialog is a local variable of your onCreate method.
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


So MyDialog is assigned to a new dialog in onCreate, but when you refer to it outside of that method, it's still null.

The solution may be to remove "AlertDialog.Builder" in onCreate so it becomes

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MyDialog = new ...
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
guillaume
Freshman
Freshman
 
Posts: 2
Joined: Sat Nov 29, 2008 1:25 pm

Postby matinba » Sat Nov 29, 2008 8:46 pm

Oh :oops: , thank you. It works now.
matinba
Freshman
Freshman
 
Posts: 5
Joined: Thu Oct 16, 2008 3:50 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests