我必须在我的课程中使用此代码
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);
Context mContext;
audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
setTitle("Dialog");
setView(Viewlayout);
setNeutralButton("Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
create();
show();
}
}
如果我尝试初始化放置上下文getApplicationContext()eclipse告诉我
对于ADialog
类型,未定义getApplicationContext()方法那么,如何解决这个问题?
logcat的
11-17 18:40:05.079:E / AndroidRuntime(16392): android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序11-17 18:40:05.079: E / AndroidRuntime(16392):at android.view.ViewRootImpl.setView(ViewRootImpl.java:563)11-17 18:40:05.079:E / AndroidRuntime(16392):at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269) 11-17 18:40:05.079:E / AndroidRuntime(16392):at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 11-17 18:40:05.079:E / AndroidRuntime(16392):at android.app.Dialog.show(Dialog.java:281)11-17 18:40:05.079: E / AndroidRuntime(16392):at android.app.AlertDialog $ Builder.show(AlertDialog.java:951)11-17 18:40:05.079:E / AndroidRuntime(16392):at com.packagename.Dialog。(Dialog.java:236)11-17 18:40:05.079: E / AndroidRuntime(16392):at com.packagename.Settings $ 1.onPreferenceClick(Settings.java:59)11-17 18:40:05.079:E / AndroidRuntime(16392):at android.preference.Preference.performClick(Preference.java:952)11-17 18:40:05.079:E / AndroidRuntime(16392):at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215) 11-17 18:40:05.079:E / AndroidRuntime(16392):at android.widget.AdapterView.performItemClick(AdapterView.java:297) 11-17 18:40:05.079:E / AndroidRuntime(16392):at android.widget.AbsListView.performItemClick(AbsListView.java:1100) 11-17 18:40:05.079:E / AndroidRuntime(16392):at android.widget.AbsListView $ PerformClick.run(AbsListView.java:2788) 11-17 18:40:05.079:E / AndroidRuntime(16392):at android.widget.AbsListView $ 1.run(AbsListView.java:3463)11-17 18:40:05.079:E / AndroidRuntime(16392):at android.os.Handler.handleCallback(Handler.java:730)11-17 18:40:05.079:E / AndroidRuntime(16392):at android.os.Handler.dispatchMessage(Handler.java:92)11-17 18:40:05.079:E / AndroidRuntime(16392):at android.os.Looper.loop(Looper.java:137)11-17 18:40:05.079: E / AndroidRuntime(16392):at android.app.ActivityThread.main(ActivityThread.java:5289)11-17 18:40:05.079:E / AndroidRuntime(16392):at java.lang.reflect.Method.invokeNative(Native Method)11-17 18:40:05.079:E / AndroidRuntime(16392):at java.lang.reflect.Method.invoke(Method.java:525)11-17 18:40:05.079: E / AndroidRuntime(16392):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:739) 11-17 18:40:05.079:E / AndroidRuntime(16392):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)11-17 18:40:05.079:E / AndroidRuntime(16392):at dalvik.system.NativeStart.main(原生方法)
答案 0 :(得分:0)
像这样更改此行
Context mContext;
到
Context mContext=arg0;
或者像这样调用音频管理器..
audioManager = (AudioManager) arg0.getSystemService(Context.AUDIO_SERVICE);