dbm*_*dbm 8 android android-alertdialog
这个让我走向疯狂的边缘!
我有一个活动,我用onClickListener初始化一个Button视图,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
mMyButton = (Button) findViewById(R.id.myButtonId);
mMyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(ID_MYDIALOG);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我还编写了自己的对话框构建器类(单例),因为我在我的应用程序中有几个其他的Activity,它将使用一组通用的对话框,因此在onCreateDialog(int id)我调用的给定Activity 的函数中:
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch (id) {
case ID_MYDIALOG:
dialog = DialogBuilder.getInstance().getMyDialog(this, mUri, mListener);
break;
default:
dialog = null;
break;
}
return dialog;
}
Run Code Online (Sandbox Code Playgroud)
在mUri和mListener变量定义在其他地方,我已经验证它们是有效的.单例DialogBuilder类的相关部分如下所示:
public AlertDialog getMyDialog(Context context, Uri uri, DialogInterface.OnClickListener listener) {
// Inflate the custom body for the AlertDialog.
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.myDialogBody, null);
// Get the data to show in the dialog.
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// Create a suitable data adapter and use it to populate the dialog body with data.
MyDialogDataAdapter adapter = new MyDialogDataAdapter(context, cursor);
// Populate the GridView in the dialog body with data.
GridView grid = (GridView) view.findViewById(R.id.myDialogDataGrid);
grid.setAdapter(adapter);
return new AlertDialog.Builder(context)
.setIcon(R.drawable.myDialogIcon)
.setTitle(R.string.myDialogTitle)
.setView(view)
.setPositiveButton(R.string.myDialogDone, listener)
.create();
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行我的应用程序并点击按钮(即mMyButton)时,我得到以下异常堆栈跟踪:
E/AndroidRuntime( 1247): FATAL EXCEPTION: main
E/AndroidRuntime( 1247): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
E/AndroidRuntime( 1247): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
E/AndroidRuntime( 1247): at com.android.internal.app.AlertController.installContent(AlertController.java:203)
E/AndroidRuntime( 1247): at android.app.AlertDialog.onCreate(AlertDialog.java:251)
E/AndroidRuntime( 1247): at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
E/AndroidRuntime( 1247): at android.app.Activity.createDialog(Activity.java:886)
E/AndroidRuntime( 1247): at android.app.Activity.showDialog(Activity.java:2557)
E/AndroidRuntime( 1247): at android.app.Activity.showDialog(Activity.java:2524)
E/AndroidRuntime( 1247): at com.dbm.myApp.myActivity$4.onClick(myActivity.java:266)
E/AndroidRuntime( 1247): at android.view.View.performClick(View.java:2534)
E/AndroidRuntime( 1247): at android.view.View$PerformClick.run(View.java:9210)
E/AndroidRuntime( 1247): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1247): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1247): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1247): at android.app.ActivityThread.main(ActivityThread.java:3652)
E/AndroidRuntime( 1247): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1247): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 1247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 1247): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 212): Force finishing activity com.dbm.myApp/.myActivity
Run Code Online (Sandbox Code Playgroud)
如果我正确地解释了堆栈跟踪,那么当我点击我的按钮时,就会触发该问题(com.dbm.myApp.myActivity$4.onClick(myActivity.java:266)其中的确切showDialog(ID_MYDIALOG);行为).
Android开发人员说,你是什么人?您认为什么导致了我的问题?
根据编译和单元测试等,假设上面的代码是有效的是公平的.但是,给定的代码被简化并剥离以适合论坛,因此,请不要努力评论命名约定等.
dbm*_*dbm 17
我想我可能已经解决了这个问题:如果我出于某种原因创建了AlertDialog:
return new AlertDialog.Builder(context)
.setIcon(R.drawable.myDialogIcon)
.setTitle(R.string.myDialogTitle)
.setView(view)
.setPositiveButton(R.string.myDialogDone, listener)
.show();
Run Code Online (Sandbox Code Playgroud)
即.show();代替,.create();问题不再存在.我不知道为什么这个AlertDialog.Builder::show()功能或功能与功能有什么不同AlertDialog.Builder::create();(超过各个功能名称中反映的明显差异).
| 归档时间: |
|
| 查看次数: |
17242 次 |
| 最近记录: |