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

发布于 2024-12-03 00:25:44 字数 6114 浏览 1 评论 0原文

我尝试启动 Place.Class 它显示每个地方的更多详细信息。

接下来我有 AboutMap 扩展了 MapActivity。我还使用按钮查看更多详细信息在这里创建AlertDialog。但这不起作用......当我运行程序时,单击查看更多详细信息按钮后它崩溃了。而且我没有得到应有的结果..我认为一定是在某个地方出错了..可能在这里

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

I try to start Place.Class which show more Detail of each place.

Following here I Have AboutMap extends MapActivity. I aslo create AlertDialog with Button See more detail here. But it's not work..When i run program, it crashes after Click See more detail Button. And i didn't get the exactly the result that it should be.. I think it must be wrong somewhere.. may be here

Intent i = new Intent(mContext, Place.class); //But i don't know how to fix it

Do you guys have any idea? Thanks you so much for your help

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2024-12-10 00:25:44

看起来您的 Cursor c 为空。在您的 PlaceItemizedOverlay 中,我看到您已经声明了一个私有变量 c 但您尚未在任何地方分配给它。 onClick 代码引用了这个变量,并且会像上面那样崩溃。

我猜您想从 AboutMap Activity 传递光标。也许向 PlaceItemizedOverlay 的构造函数添加另一个参数来共享光标?

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

        }

It looks like your Cursor c is null. In your PlaceItemizedOverlay I see you have declared a private variable c but you have not assigned to it anywhere. The onClick code refers to this variable and will crash as above.

I guess you want to pass your Cursor in from your AboutMap Activity. Maybe add another parameter to the constructor of PlaceItemizedOverlay to share the cursor?

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

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文