Android 如何从对话框启动活动?
您知道如何启动活动并通过对话框按钮发送值吗 这是我目前的情况。尝试了多种变体,但按下按钮时应用程序崩溃:Android 如何从对话框启动活动?,android,button,android-activity,dialog,android-intent,Android,Button,Android Activity,Dialog,Android Intent,您知道如何启动活动并通过对话框按钮发送值吗 这是我目前的情况。尝试了多种变体,但按下按钮时应用程序崩溃: dialog.setPositiveButton("View Profile", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(); intent.se
dialog.setPositiveButton("View Profile", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setClass(context, Profile.class);
intent.putExtra("profileID", "8");
startActivity(intent);
dialog.cancel();
return;
}
});
全班:
public class PlacesItemizedOverlay extends ItemizedOverlay {
private Context context;
private ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
private Activity aClass;
public PlacesItemizedOverlay(Context aContext, Drawable marker) {
super(boundCenterBottom(marker));
context = aContext;
}
public void addOverlayItem(OverlayItem item) {
items.add(item);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return (OverlayItem) items.get(i);
}
@Override
public int size() {
return items.size();
}
@Override
protected boolean onTap(int index) {
aClass = new Activity();
OverlayItem item = (OverlayItem) items.get(index);
if(item.getTitle() != null)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setPositiveButton("View Profile",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setClass(context, Profile.class);
intent.putExtra("profileID", "8");
aClass.startActivity(intent);
dialog.cancel();
return;
}
});
dialog.show();
}
return true;
}
}
在按钮的onClick上,我相信您需要启动Tactivity并使用新活动设置意图,并在AndroidManifest.xml中引用它。您是否检查过是否已在清单文件中添加Profile.class,如下所示:
<activity android:name=".Profile" />
我不得不重新构造代码,以下是最终有效的方法:
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(item.getTitle())
.setCancelable(true)
.setPositiveButton("View Details", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(mContext, Profile.class);
intent.putExtra("id", item.getSnippet());
mContext.startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
putExtra()
是正确的方法。堆栈跟踪中的错误消息是什么?你必须随意告诉我,我还不习惯调试它。我会看看是否能找到“堆栈跟踪”并发布它。非常感谢。在命令窗口中键入adb logcat
,查找大错误消息。首先,如果我现在是个十足的白痴,请允许我道歉,我在eclipse中运行windows命令promt,我输入了“adb logcat”,我得到的“adb”未被识别为内部或外部命令。感谢您在这方面的帮助:)您必须将adb
添加到您的环境变量中。或者,只需在。\android sdk windows\platform tools
文件夹+1中调用adb
——我猜活动不在清单文件中。你是什么意思?您需要引用清单中的所有活动。抱歉,伙计们,该活动位于清单文件中,但我现在在setClass中收到一个错误,我将在上面更新该错误。谢谢你的输入:)是的,它粘得很紧。试着检查你的导入是否正确,我已经在我的代码中运行了你的代码,并且它处于工作状态:导入android.content.DialogInterface;导入android.content.Intent;谢谢你花时间检查,我很感激。看着代码,我觉得不错,但现在它只是不喜欢setClass。我想我会发布完整的文件,以防万一我在某处设置了一些愚蠢的冲突。。。
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(item.getTitle())
.setCancelable(true)
.setPositiveButton("View Details", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(mContext, Profile.class);
intent.putExtra("id", item.getSnippet());
mContext.startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
return true;