使用活动组时,无法从alertdialog onclicklistener启动新活动

时间:2012-04-05 11:23:51

标签: android android-tabhost android-alertdialog activitygroup

我只有两个标签,并使用Experience - Multiple Android Activities in a TabActivity作为参考 我的班级建筑是这样的:
           MainActivity 扩展了TabActivity
               1. TabGroup1Activity 扩展TabGroupActivity(从上面引用实现的TabGroupActivity类)
  1.i. Tab1Activity 扩展了MapActivity(具有多个标记)
2. TabGroup2Activity 扩展TabGroupActivity
2.i.的 Tab2Activity

在第二个标签(Tab2Activity)中,我显示了具有多个标记的谷歌地图。在点击标记时,我显示了带有更多信息选项的alertdialog 单击“更多信息”选项我必须在不丢失底部标签的情况下启动新活动。

错误在哪里我不知道?

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;

}
}

当我尝试上面的代码时,当我点击alertdialog的更多信息按钮时出现错误 当我删除alertdialog并编写以下代码时,它工作正常,在同一个选项卡中打开新活动:

@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;

}

当我点击alertdialog上的“更多信息”按钮时,如何才能打开新活动? 帮助!!

  

04-05 17:42:02.171:W / dalvikvm(2631):threadid = 1:线程退出,未捕获异常(group = 0x4001d800)   04-05 17:42:02.171:E / AndroidRuntime(2631):致命异常:主要   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):在android.os.Handler.dispatchMessage(Handler.java:99)   04-05 17:42:02.171:E / AndroidRuntime(2631):在android.os.Looper.loop(Looper.java:123)   04-05 17:42:02.171:E / AndroidRuntime(2631):在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)

1 个答案:

答案 0 :(得分:0)

您必须在AlertDialogue上运行current UI Thread。使用runOnUiThread()启动UI Thread.Here就是示例:

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