将其传递给外部类并从 Android 上的 Alertdialog 列表中启动一个 Activity
我正在开发一个Android应用程序,这个应用程序有十几个Activity,每个Activity对应一个屏幕。现在我在屏幕顶部有了这个通用的字幕栏。 该字幕栏有一个按钮可以显示警报对话框,该对话框显示链接列表以转到不同的屏幕。
我可以为每个活动编写一个相同的函数来调用警报对话框,但是如果我想修改它们,那会很乏味,所以我创建了这个类:
public class MenuAlertDialog extends Activity {
/*
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
*/
public void createMenu(final Context context){
AlertDialog.Builder dlg = new AlertDialog.Builder(context);
dlg.setTitle("menu");
String[] items = {"pageA", "pageB", "pageC", "pageD", "pageE"};
dlg.setItems(items, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
switch(which){
case 0:
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
dlg.show();
}
}
并从每个活动中调用它,如下所示:
MenuAlertDialog menu = new MenuAlertDialog();
menu.createMenu(this);
从 onCreate 内部。
它可以显示警报对话框,但每当我按 pageA 链接时,它就会失败并出现意外错误。
Logcat 说它是一个空指针错误,原因似乎是
startActivity(intent);
我做错了什么?
I am developing an Android app, this app has a dozen of Activities, each one is for a corresponding screen. Now I have this common subtitle bar on top of the screens.
this subtitle bar has a button to display an alert dialog which shows link list to go to a different screen.
I could write a same function for each activity to call the alert dialog, but that would be tedious if I want to modify them, so I created this class:
public class MenuAlertDialog extends Activity {
/*
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
*/
public void createMenu(final Context context){
AlertDialog.Builder dlg = new AlertDialog.Builder(context);
dlg.setTitle("menu");
String[] items = {"pageA", "pageB", "pageC", "pageD", "pageE"};
dlg.setItems(items, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
switch(which){
case 0:
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
dlg.show();
}
}
and call it from each activity, like this:
MenuAlertDialog menu = new MenuAlertDialog();
menu.createMenu(this);
from inside of onCreate.
It can display the alertDialog, but whenever I press pageA link, it fails with an unexpected error.
Logcat says its a nullpointererror and the cause seems
startActivity(intent);
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除代码
,因为您不需要扩展您正在创建的类,因为它不依赖于任何与活动相关的功能。
在您调用
startActivity(intent);
的地方将其替换为Remove the code
as you have no need to extend your class that you are creating since it does not rely on any activity related functionality.
Where you call
startActivity(intent);
replace it with您应该将类更改为“扩展对话框”而不是“活动”。
另请尝试以下操作:
查看本教程,了解如何创建自定义对话框。 自定义对话框
另外这里另一个教程
以及此处
You should change the class to Extends Dialog and not activity.
Also for Try this:
Check out this tutorial on how to create a custom dialog. Custom Dialog
Also Here Another Tutorial
And Here