Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to launch an activity from a dialog?
    primarykey
    data
    text
    <p>Any ideas how to launch an activity and send a value from a dialog button?</p> <p>Here's what I have at the moment. Tried a number of variations but app crashes when the button is pressed:</p> <pre><code>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; } }); </code></pre> <p>Full class:</p> <pre><code>public class PlacesItemizedOverlay extends ItemizedOverlay { private Context context; private ArrayList&lt;OverlayItem&gt; items = new ArrayList&lt;OverlayItem&gt;(); 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; } } </code></pre> <p>LogCat:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload