首页 文章

Android - 单击自定义列表适配器连续按钮时启动MapActivity

提问于
浏览
1

我有一个自定义 list adapter 的应用程序 . 该列表包含3个部分: imagetextbutton .

  • 当点击一行时,我开始 activity

  • 并且当单击按钮时,我想要启动另一个活动,但它是 MapActivity .

  • 这两项活动必须随身携带通过意图中的额外内容传递的信息 .

  • 第一个(点击行)活动完美无缺 .

  • 第二个仅在启动常规活动时起作用 .

  • 当我尝试启动 MapActivity 时,应用程序崩溃了 .

  • 下面的代码在自定义适配器类中,并且在 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

这是 GoogleMaps.javaOnCreate 方法

@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);
}

1 回答

  • 0

    不要使用“android.intent.action.GOOGLEMAPS”作为操作名称 . 使用您的包名创建您自己的包,因此它永远不会与任何其他应用程序冲突 .

    似乎坐标未达到GoogleMaps活动 . 将Log.d()放在开始活动之前的第一个活动中,并放在“latitude = Float.parseFloat(lat_coordinate);”之前的第二个活动中 .

    顺便说一句,你在第一个活动中使用常量名称,在第二个活动中使用字符串文字,非常容易出错 . 始终使用常量 .

相关问题