Saturday 8 June 2019

java - Null Pointer Exception with Dialog Android




i have this Dialog and i would like to show it when i click on a preference



public class ADialog extends AlertDialog.Builder {

public ADialog(Context arg0) {
super(arg0);
final LayoutInflater inflater = LayoutInflater.from(arg0);
final View Viewlayout = inflater.inflate(R.layout.myLayout, null);


setTitle("Text Dialog");
setView(Viewlayout);

setNeutralButton("CLOSE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});


create();
show();
}
}


This is the code from Preference



Context context;
Pref = (Preference) this.findPreference("pref");

pref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
@Override
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
ADialog dialog = new ADialog(context);

return false;


}});



I'm getting a Java Null Pointer Exception in super(arg0); Why?



THIS IS THE LOGCAT. Settings line 57 correspond to ADialog dialog = new ADialog(context);



> 11-17 18:15:45.416: E/AndroidRuntime(14709): FATAL EXCEPTION: main
> 11-17 18:15:45.416: E/AndroidRuntime(14709):
> java.lang.NullPointerException 11-17 18:15:45.416:
> E/AndroidRuntime(14709): at

> android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at
> android.app.AlertDialog$Builder.(AlertDialog.java:360) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at
> com.packagename.ADialog.(Dialog.java:21) 11-17 18:15:45.416:
> E/AndroidRuntime(14709): at
> com.packagename.Settings$1.onPreferenceClick(Settings.java:57)
> 11-17 18:15:45.416: E/AndroidRuntime(14709): at
> android.preference.Preference.performClick(Preference.java:952) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at

> android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)
> 11-17 18:15:45.416: E/AndroidRuntime(14709): at
> android.widget.AdapterView.performItemClick(AdapterView.java:297)
> 11-17 18:15:45.416: E/AndroidRuntime(14709): at
> android.widget.AbsListView.performItemClick(AbsListView.java:1100)
> 11-17 18:15:45.416: E/AndroidRuntime(14709): at
> android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
> 11-17 18:15:45.416: E/AndroidRuntime(14709): at
> android.widget.AbsListView$1.run(AbsListView.java:3463) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at

> android.os.Handler.handleCallback(Handler.java:730) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at
> android.os.Handler.dispatchMessage(Handler.java:92) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at
> android.os.Looper.loop(Looper.java:137) 11-17 18:15:45.416:
> E/AndroidRuntime(14709): at
> android.app.ActivityThread.main(ActivityThread.java:5289) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at
> java.lang.reflect.Method.invokeNative(Native Method) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at

> java.lang.reflect.Method.invoke(Method.java:525) 11-17 18:15:45.416:
> E/AndroidRuntime(14709): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
> 11-17 18:15:45.416: E/AndroidRuntime(14709): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at
> dalvik.system.NativeStart.main(Native Method


I've also this




    Context mContext;

audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);


For use it in this class i used the Context but the logcat is a Java Null Pointer Exception


Answer



here you are passing null context so pass the correct context




ADialog dialog = new ADialog(context);


into



ADialog dialog = new ADialog(getAppicationContext());s..


or add like this




Context context=getApplicationContext()

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...