Error to start new Activity with Intent after click Button in AlertDialog
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(overla开发者_运维技巧y);
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
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;
}
精彩评论