Any ideas how to launch an activity and send a value from a dialog button?
Here's what I have at the moment. Tried a number of variations but app crashes when the button is pressed:
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;
}
});
Full class:
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(inde开发者_开发问答x);
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
Did you check that you have added the Profile.class in the manifest file like such:
<activity android:name=".Profile" />
On the button's onClick, I believe you need to startactivity and set the intent using the new activity and also reference it in the AndroidManifest.xml
I ended up having to restructure the code, here is what ended up working:
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;
精彩评论