在AlertDialog中单击按钮后,使用Intent启动新活动时出错

时间:2011-09-04 07:54:25

标签: android android-intent onclick

我尝试启动Place.Class,其中显示了每个地方的详细信息。

在此处,我AboutMap延长了MapActivity。我在这里用Button See more detail创建AlertDialog。但它不起作用..当我运行程序时,它会在单击See more detail按钮后崩溃。而且我没有得到它应该是的确切结果......我认为某处肯定是错的......可能在这里

Intent i = new Intent(mContext, Place.class); //但我不知道如何解决它

你们有什么想法吗?非常感谢你的帮助

PlaceItemizedOverlay

    public class PlaceItemizedOverlay extends ItemizedOverlay<OverlayItem> {

        private Context mContext;

        private Cursor c;

        private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

        public PlaceItemizedOverlay(Drawable defaultMarker, Context context)
        {
            super(boundCenterBottom(defaultMarker));
            mContext = context;

        }
        public PlaceItemizedOverlay(Context context)
        {
            super(boundCenterBottom(null));
            mContext = context;

        }
        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) {

    //Create AlertDialog
          final OverlayItem oi = mOverlays.get(index);
          AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
          dialog.setTitle(oi.getTitle());
          dialog.setMessage(oi.getSnippet());
          dialog.setNegativeButton("Back", null);
  //Create See more detail Button
          dialog.setPositiveButton("See More Detail", new DialogInterface.OnClickListener() {

            @Override 
            public void onClick(DialogInterface dialog, int id) {
        //Line 87: Logcat tell that there are some problem around here
                    Intent i = new Intent(mContext, Place.class); 
                    i.putExtra(Constants.KEY_ID, c.getInt(
                            c.getColumnIndex(Constants.KEY_ID)));
                    i.putExtra(Constants.COL_TITLE, c.getString(
                            c.getColumnIndex(Constants.COL_TITLE)));
                    i.putExtra(Constants.COL_ADDRESS, c.getString(
                            c.getColumnIndex(Constants.COL_ADDRESS)));
                    i.putExtra(Constants.COL_CONTENT, c.getString(
                            c.getColumnIndex(Constants.COL_CONTENT)));
                    mContext.startActivity(i);
            }});
          dialog.show();
          return true;

        }


    }

AboutMap.class

public class AboutMap extends MapActivity {

    MapView mapView;
    MapController mapController;
    private static MyDB mDbHelper;
    private Cursor c;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aboutcm);

        mDbHelper = new MyDB(this);
        mDbHelper.createDatabase();
        mDbHelper.open();
        c = mDbHelper.getAttraction();



        mapView = (MapView) findViewById(R.id.mapview);
        mapController = mapView.getController();
        Drawable drawable = this.getResources().getDrawable(R.drawable.map_pin_3);
        List<Overlay> mapOverlays = mapView.getOverlays();
        PlaceItemizedOverlay itemizedoverlay = new PlaceItemizedOverlay(drawable, this);

        mapController.setZoom(13); 
        mapView.setBuiltInZoomControls(true);


        c.moveToFirst(); 
        do {
            String title = c.getString(c
                    .getColumnIndex(Constants.COL_TITLE));
            String address = c.getString(c
                    .getColumnIndex(Constants.COL_ADDRESS));
            int latitude = (int) (c.getDouble(c
                    .getColumnIndex(Constants.COL_LA)) * 1E6);
            int longitude = (int) (c.getDouble(c
                    .getColumnIndex(Constants.COL_LONG)) * 1E6);

            itemizedoverlay.addOverlay(new OverlayItem(new GeoPoint(latitude, longitude), title,
                    address));

            mapOverlays.add(itemizedoverlay);


        } while (c.moveToNext());


    }


    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }



}

Logcat

09-04 14:31:29.019: DEBUG/AndroidRuntime(975): Shutting down VM
09-04 14:31:29.029: WARN/dalvikvm(975): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-04 14:31:29.029: ERROR/AndroidRuntime(975): Uncaught handler: thread main exiting due to uncaught exception
09-04 14:31:29.069: ERROR/AndroidRuntime(975): java.lang.NullPointerException
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at com.ctg.PlaceItemizedOverlay$1.onClick(PlaceItemizedOverlay.java:87)
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at android.os.Looper.loop(Looper.java:123)
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at android.app.ActivityThread.main(ActivityThread.java:4363)
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at java.lang.reflect.Method.invoke(Method.java:521)
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-04 14:31:29.069: ERROR/AndroidRuntime(975):     at dalvik.system.NativeStart.main(Native Method)
09-04 14:31:29.099: INFO/Process(53): Sending signal. PID: 975 SIG: 3

1 个答案:

答案 0 :(得分:1)

您的Cursor c似乎为空。在PlaceItemizedOverlay我看到您已声明私有变量c,但您尚未在任何地方分配它。 onClick代码引用此变量并将如上所述崩溃。

我想你想从你的AboutMap活动中传递你的光标。也许在PlaceItemizedOverlay的构造函数中添加另一个参数来共享游标?

public PlaceItemizedOverlay(Drawable defaultMarker, Context context, Cursor cursor)
        {
            super(boundCenterBottom(defaultMarker));
            mContext = context;
            c = cursor;

        }