[英]Android -Starting a MapActivity when button in a row from a custom list adapter is clicked
我有一个具有自定义list adapter
的应用程序。 该列表分为三部分: 图像 , 文本和按钮 。
activity
, MapActivity
。 MapActivity
,应用程序崩溃。 OnClick
方法中作为参数/参数接收的View是一个按钮(这就是为什么我使用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
这是GoogleMaps.java
的OnCreate
方法
@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);
}
请勿将“ android.intent.action.GOOGLEMAPS”用作操作名称。 创建一个带有您的程序包名称的程序包,这样它就不会与任何其他应用程序程序冲突。
好像坐标没有到达GoogleMaps活动。 在开始活动之前,将Log.d()放在第一个活动中,而在“纬度= Float.parseFloat(lat_coordinate);”之前,将Log.d()放入第二个活动中。
顺便说一句,您在第一个活动中使用Extras名称的常量,在第二个活动中使用字符串文字,这很容易出错。 始终使用常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.