Android - 单击自定义列表适配器中的一行中的按钮时启动MapActivity
我有一个具有自定义列表适配器的应用程序。该列表包含3个部分:图片,文本和按钮。Android - 单击自定义列表适配器中的一行中的按钮时启动MapActivity
- 当单击某一行,我让一个活动开始,
- ,并单击按钮时,我想另一个活动启动,但它是一个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的onCreate方法(包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()置于开始活动之前的第一个活动和第二个活动之前的“latitude = Float.parseFloat(lat_coordinate);”
顺便说一句,你使用常量的第一个活动中的额外名称和第二个字符串文字,非常容易出错。始终使用常量。
谢谢。 我终于修好了。这是一个简单但很容易失误的错误。字符串“lat_coordinates”和“lon_coordinates”都是空的,因为我用来传递额外信息的标签是不同的。我使用“latitud”和“longitudinal”(常量DB_FIELD_LAT和DB_FIELD_LON),而负责其他类(GoogleMaps.java)的队友使用“纬度”和“经度”(有趣的是他们是相同的不同语言)。 – Veris
我刚刚将代码中的标记名称更改为GoogleMaps.java正在接收的标记名称,如下所示: String LAT =“Latitude”; String LON =“Longitude”; 并将putExtras更改为: intent.putExtra(LAT,lat); intent.putExtra(LON,lon); 最后,我写了这样的意图: Intent intent = new Intent(parentView.getContext(),GoogleMaps.class); 谢谢大家! P.S.由于声誉我还不能回答我自己的问题,但我想让你知道问题解决了。 – Veris
正如我所说,常量是你的朋友;-) – rgrocha
请为我们提供错误日志。 –
错误发生在文件GoogleMaps.java的第37行 您可以发布GoogleMaps.java onCreate方法吗? 'at com.example.turiston.GoogleMaps.onCreate(GoogleMaps.java:37)' –
当我从常规Activity打开GoogleMaps.java时,它可以工作。我不知道它是否与适配器或列表视图有关。 – Veris