我有这个对话框,我想在点击偏好
时显示它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();
}
}
这是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;
}});
我在super(arg0)中得到Java Null Pointer Exception;为什么?
这是LOGCAT。设置行57对应于ADialog对话框=新ADialog(上下文);
> 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.<init>(AlertDialog.java:360) 11-17
> 18:15:45.416: E/AndroidRuntime(14709): at
> com.packagename.ADialog.<init>(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
我也是这个
Context mContext;
audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
为了在这个类中使用它我使用了Context但是logcat是一个Java空指针异常
答案 0 :(得分:0)
这里传递null context
,所以传递正确的context
ADialog dialog = new ADialog(context);
到
ADialog dialog = new ADialog(getAppicationContext());s..
或像这样添加
Context context=getApplicationContext()