Can not start new activity from alertdialog onclicklistener when activitygroup is used
-
29-05-2021 - |
문제
I simply have 2 tabs and used Experience - Multiple Android Activities in a TabActivity as reference.
My class Architecture is like this:
MainActivity extends TabActivity
1.TabGroup1Activity extends TabGroupActivity (TabGroupActivity-class implemented from above reference)
1.i. Tab1Activity extends MapActivity (which has multiple marker)
2.TabGroup2Activity extends TabGroupActivity
2.i. Tab2Activity
In second tab (Tab2Activity) i show the google map which has multiple markers. On Taping the marker I showed the alertdialog with More Info option.
Onclicking the More Info Option i have to start new activity without loosing tabs at bottom.
where is the error i dont know ??
MapItemizedOverlay.java
public class InformationItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
Activity parentContext;
public InformationItemizedOverlay(Drawable defaultMarker, Context context,
Activity parent) {
super(boundCenterBottom(defaultMarker));
mContext = context;
parentContext = parent;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(parentContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("More Info..",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent test = new Intent(parentContext,
AboutActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity) parentContext;
parentActivity
.startChildActivity("AboutActivity", test);
}
});
dialog.show();
return true;
}
}
When i try with above code, error gives when i click the More Info button at alertdialog.
When i remove the alertdialog and write the following code, it works fine, opens new activity in the same tab:
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Intent test = new Intent(parentContext,
AboutActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity) parentContext;
parentActivity
.startChildActivity("AboutActivity", test);
}
});
return true;
}
How can i make it work to open new activity when i click the More Info button at alertdialog ??
Help !!
04-05 17:42:02.171: W/dalvikvm(2631): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 04-05 17:42:02.171: E/AndroidRuntime(2631): FATAL EXCEPTION: main 04-05 17:42:02.171: E/AndroidRuntime(2631): java.lang.ClassCastException: com.bbs.MainActivity 04-05 17:42:02.171: E/AndroidRuntime(2631): at com.bbs1.InformationItemizedOverlay$1.onClick(InformationItemizedOverlay.java:63) 04-05 17:42:02.171: E/AndroidRuntime(2631): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 04-05 17:42:02.171: E/AndroidRuntime(2631): at android.os.Handler.dispatchMessage(Handler.java:99) 04-05 17:42:02.171: E/AndroidRuntime(2631): at android.os.Looper.loop(Looper.java:123) 04-05 17:42:02.171: E/AndroidRuntime(2631): at android.app.ActivityThread.main(ActivityThread.java:4627) 04-05 17:42:02.171: E/AndroidRuntime(2631): at java.lang.reflect.Method.invokeNative(Native Method) 04-05 17:42:02.171: E/AndroidRuntime(2631): at java.lang.reflect.Method.invoke(Method.java:521) 04-05 17:42:02.171: E/AndroidRuntime(2631): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 04-05 17:42:02.171: E/AndroidRuntime(2631): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-05 17:42:02.171: E/AndroidRuntime(2631): at dalvik.system.NativeStart.main(Native Method)
해결책
You have to run the AlertDialogue
on current UI Thread
. Use runOnUiThread()
to start the UI Thread.Here is the example:
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder dialog = new AlertDialog.Builder(parentContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("More Info..",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent test = new Intent(parentContext,AboutActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity) parentContext;
parentActivity.startChildActivity("AboutActivity", test);
}
});
dialog.show();
}//run
});//runOnUIThred