我有一个自定义的应用程序list adapter
。该列表包含 3 个部分:图像、文本和按钮。
- 单击一行时,我
activity
开始, - 当单击按钮时,我希望启动另一个活动,但它是一个
MapActivity
. - 这两项活动都必须携带通过意图中的附加信息传递的信息。
- 第一个(单击行)活动完美运行。
- 第二个仅在启动常规活动时有效。
- 当我尝试启动时
MapActivity
,应用程序崩溃。 - 下面的代码在自定义适配器类中,在方法中作为参数/参数接收的 View
OnClick
是一个按钮(这就是我使用 的原因getParent
)。
如果有任何不清楚的地方,请告诉我,我会尽力回答。
@Override
public void onClick(View view)
{
View parentView = (View) view.getParent();
switch (view.getId())
{
case R.id.boton_mapa_lugar:
Intent intent = new Intent("android.intent.action.GOOGLEMAPS");
String lat = ((TextView) parentView.findViewById(R.id.lat_lugar)).getText()
.toString();
String lon = ((TextView) parentView.findViewById(R.id.lon_lugar)).getText()
intent.putExtra(Lugares.DB_FIELD_LAT, lat);
intent.putExtra(Lugares.DB_FIELD_LON, lon);
parentView.getContext().startActivity(intent);
break;
default:
//Code for starting activity when row is clicked...
}
}
这是错误日志:
11-01 18:32:18.374: W/dalvikvm(14162): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
11-01 18:32:18.393: E/AndroidRuntime(14162): FATAL EXCEPTION: main
11-01 18:32:18.393: E/AndroidRuntime(14162): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.turiston/com.example.turiston.GoogleMaps}: java.lang.NullPointerException
11-01 18:32:18.393: E/AndroidRuntime(14162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-01 18:32:18.393: E/AndroidRuntime(14162): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 18:32:18.393: E/AndroidRuntime(14162): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 18:32:18.393: E/AndroidRuntime(14162): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 18:32:18.393: E/AndroidRuntime(14162): at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 18:32:18.393: E/AndroidRuntime(14162): at android.os.Looper.loop(Looper.java:123)
11-01 18:32:18.393: E/AndroidRuntime(14162): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 18:32:18.393: E/AndroidRuntime(14162): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 18:32:18.393: E/AndroidRuntime(14162): at java.lang.reflect.Method.invoke(Method.java:521)
11-01 18:32:18.393: E/AndroidRuntime(14162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-01 18:32:18.393: E/AndroidRuntime(14162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-01 18:32:18.393: E/AndroidRuntime(14162): at dalvik.system.NativeStart.main(Native Method)
11-01 18:32:18.393: E/AndroidRuntime(14162): Caused by: java.lang.NullPointerException
11-01 18:32:18.393: E/AndroidRuntime(14162): at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:301)
11-01 18:32:18.393: E/AndroidRuntime(14162): at java.lang.Float.parseFloat(Float.java:291)
11-01 18:32:18.393: E/AndroidRuntime(14162): at com.example.turiston.GoogleMaps.onCreate(GoogleMaps.java:37)
11-01 18:32:18.393: E/AndroidRuntime(14162): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 18:32:18.393: E/AndroidRuntime(14162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 18:32:18.393: E/AndroidRuntime(14162): ... 11 more
这是OnCreate
方法GoogleMaps.java
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_maps);
// Obtains the coordinates from the place from the previous Activity
Bundle extras = getIntent().getExtras();
float latitude; // = 19.2373f;
float longitude; // = 68.82634f;
// Variables to store the strings from the database
String lat_coordinate;
String lon_coordinate;
// Assigns the values from the previous Activity to the two variables
lat_coordinate = extras.getString("Latitude");
lon_coordinate = extras.getString("Longitude");
latitude = Float.parseFloat(lat_coordinate);
longitude = Float.parseFloat(lon_coordinate);
MapView mapView = (MapView) findViewById(R.id.mapview);
// Enables zoom controls for the map
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.androidmarker);
MapsItemizedOverlay itemizedoverlay = new MapsItemizedOverlay(drawable,
this);
// Creates a GeoPoint to be used for the location of the place on the
// map
GeoPoint point = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
// If you click on the Android guy it displays the following message
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!",
"I'm in San Juan!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}