如何启动活动并从对话框按钮发送值?
这就是我现在所拥有的。尝试了许多变化,但按下按钮时应用程序崩溃:
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;
}
}
logcat的:
06-24 10:35:31.253: WARN/dalvikvm(30118): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): FATAL EXCEPTION: main
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): java.lang.NullPointerException
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at android.app.Activity.startActivityForResult(Activity.java:2901)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at android.app.Activity.startActivity(Activity.java:3007)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at com.example.android.test.PlacesItemizedOverlay$1.onClick(PlacesItemizedOverlay.java:57)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at android.os.Looper.loop(Looper.java:143)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at android.app.ActivityThread.main(ActivityThread.java:4196)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at java.lang.reflect.Method.invoke(Method.java:507)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-24 10:35:31.283: ERROR/AndroidRuntime(30118): at dalvik.system.NativeStart.main(Native Method)
06-24 10:35:31.293: WARN/ActivityManager(1344): Force finishing activity com.example.android.test/.SearchActivity
答案 0 :(得分:1)
您是否检查过您已在清单文件中添加了Profile.class,如下所示:
<activity android:name=".Profile" />
答案 1 :(得分:0)
在按钮的onClick上,我相信您需要启动活动并使用新活动设置意图,并在AndroidManifest.xml中引用它
答案 2 :(得分:0)
我最终不得不重新构建代码,以下是最终的工作:
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;