0

I am building an alert dialog box with 3 text fields, 2 buttons and 1 spinner dropdown box. The layout for this is in "custom_dialog.xml".

However, when it pops up all the items are null. I get a null pointer exception. If I add a if condition as shown below, the alert dialog is shown, if not I get a null pointer exception on spinner and the buttons.

Here is the code; Please note that the alert shows up on long click of current view in MainActivity.

View promptsView = li.inflate(R.layout.custom_dialog, null);
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setView(promptsView); 
alert.setIcon(R.drawable.airside);
alert.setTitle("Calculate Distance To");
final TextView from = new TextView(MainActivity.this);
from.setText("From: "+item.getTitle1());
final TextView to1 = new TextView(MainActivity.this);
to1.setText("To: "+item.getTitle1());
final TextView result = (TextView) dialog.findViewById(R.id.txt3);
final AlertDialog alertDialog = alert.create();
final Button btn1 = (Button) dialog.findViewById(R.id.btn1);
final Button btn2 = (Button) dialog.findViewById(R.id.btn2);


ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,airNames);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
if (spinner!=null){
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener( new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
toLat=items.get(position).getPosition().latitude;
toLon=items.get(position).getPosition().longitude;
}
 @Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}

if (btn1!=null)
btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Double dist=calculateDistance(fromLon,fromLat,toLon,toLat);
result.setText(dist.toString());

}

});
if (btn2!=null)
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();

}

});

alert.show();

Please check how I've added an if condition with spinner, btn1, and btn2. That means those are null every time I run it. With the if condition, the alert dialog pops up and I see all the alert box.

EDIT1:

Logcat error (with the if commands above removed);

12-02 14:44:17.305: E/OK(7093): LONGCLICKED
12-02 14:44:17.335: E/AndroidRuntime(7093): FATAL EXCEPTION: main
12-02 14:44:17.335: E/AndroidRuntime(7093): Process: com.mapsupport, PID: 7093
12-02 14:44:17.335: E/AndroidRuntime(7093): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.mapsupport.MainActivity$2.onMapLongClick(MainActivity.java:457)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.google.android.gms.maps.GoogleMap$9.onMapLongClick(Unknown Source)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.google.android.gms.maps.internal.zzk$zza.onTransact(Unknown Source)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at android.os.Binder.transact(Binder.java:380)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.google.android.gms.maps.internal.bb.a(SourceFile:93)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.google.maps.api.android.lib6.gmm6.c.ac.a(Unknown Source)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.google.maps.api.android.lib6.gmm6.m.bt.f(Unknown Source)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.google.maps.api.android.lib6.gmm6.m.ak.onLongPress(Unknown Source)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.google.maps.api.android.lib6.d.g.onLongPress(Unknown Source)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.google.maps.api.android.lib6.d.h.c(Unknown Source)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.google.maps.api.android.lib6.d.i.handleMessage(Unknown Source)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at android.os.Looper.loop(Looper.java:135)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at android.app.ActivityThread.main(ActivityThread.java:5375)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at java.lang.reflect.Method.invoke(Native Method)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at java.lang.reflect.Method.invoke(Method.java:372)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
12-02 14:44:17.335: E/AndroidRuntime(7093):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Zac1
  • 208
  • 7
  • 34

1 Answers1

4

NPE occured because the spinner is in the layout of the dialog and not in the layout of the activity.

Add:

alertDialog.show();

after this line of code:

final AlertDialog alertDialog = alert.create();

Then initialize all your views after you call the show() method on your alertDialog and not before it:

TextView result = (TextView) promptsView.findViewById(R.id.txt3);
Spinner spinner = (Spinner) promptsView.findViewById(R.id.spinner1);
final Button btn1 = (Button) promptsView.findViewById(R.id.btn1);
final Button btn2 = (Button) promptsView.findViewById(R.id.btn2);

Also make sure to remove this line at the end from your code:

alert.show();
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
  • I changed it and still get the same error: "12-02 14:55:46.120: E/AndroidRuntime(9267): FATAL EXCEPTION: main 12-02 14:55:46.120: E/AndroidRuntime(9267): Process: com.mapsupport, PID: 9267 12-02 14:55:46.120: E/AndroidRuntime(9267): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference " – Zac1 Dec 02 '15 at 19:56
  • It worked, thank you. Yes, I moved my alert.show() after the create method, and initialized my textviews properly. Sorry I forgot to reference them textviews from the XML! – Zac1 Dec 02 '15 at 20:23
  • Do you know how to dismiss the alert box? Sth like "alert.dismiss();" does not exist? – Zac1 Dec 02 '15 at 20:28
  • It's **alertDialog.dismiss();** – Hussein El Feky Dec 02 '15 at 20:30
  • I removed all references to alertDialog. I was using the "alert" variable - " AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); and then alert.create and alert.show... " – Zac1 Dec 02 '15 at 20:32
  • Hi, yes I tried it, and it says "alertDialog cannot be resolved".. Sorry. – Zac1 Dec 02 '15 at 21:56
  • I am sure that it works. Where do you call it? You must call it within the same method in your code. I mean if you wrote all this code on a button click, you should write it inside it too. Unless you can declare **AlertDialog alertDialog;** before onCreate() method, then initialize it on the button click by just removing **final AlertDialog** from the line of its initialization. You will, then, be able to call **alertDialog.dimiss();** from anywhere in your code, but make sure to put an if statement to check if alertDialog is null or not first. Let me know if you still have any problem. – Hussein El Feky Dec 02 '15 at 21:57
  • No, it didn't work... I have declared "AlertDialog dialog;" above onCreate. Then I do a "dialog = alert.create();" on long click of MainActivity. Then, on click of btn2, I do a "dialog.dismiss();" and it didn't work... The on click on btn1 works and displays the result, so the code is right. – Zac1 Dec 02 '15 at 22:03
  • Yeah, I thought you were just writing it in another method without referencing it. That's why I said to try to declare it above onCreate(). Definitely, no need to declare it as you won't reference it from another method. – Hussein El Feky Dec 02 '15 at 22:05
  • I also tried dialog.cancel() and even that doesn't work... How to solve this? I have a log message that detects the click on btn2, but can't close it. I tried "onBackPressed()" but that crashes the app. – Zac1 Dec 02 '15 at 22:29
  • Can you post your logcat showing the error? Preferably in another new question. – Hussein El Feky Dec 02 '15 at 22:55
  • There is no error. The dismiss() just does not execute. – Zac1 Dec 02 '15 at 23:04
  • How do you call it? This link clearly explains how you dismiss it: http://stackoverflow.com/questions/14853325/how-to-dismiss-alertdialog-in-android – Hussein El Feky Dec 03 '15 at 09:54