0

知道我的代码有什么问题吗?我只想显示地图,但是当我单击文本视图时,地图出现强制关闭错误。我在 XML 布局中做了必要的事情,添加

android:onClick="onClick"
android:clickable="true"

我检查了 map.java,实际上,它工作正常。我也检查了 onclick textview,它也工作正常。但是,当我想将两者链接在一起时,这意味着在 onclick 文本视图上显示 map.java。但是它给了我错误。有人可以告诉我我的代码有什么问题吗?先感谢您。

onclick textview 代码:我已经实现了 onclicklistener

     @Override
public void onClick(View v) {
         //TODO Auto-generated method stub
    startActivity(new Intent(SingleMenuItemActivity.this,
                map.class));

}

地图.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class map extends FragmentActivity {
    private GoogleMap map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

           SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
            map = fm.getMap(); 
        map.animateCamera(CameraUpdateFactory.zoomIn());

    }


}

map_shown.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"  
android:layout_width="match_parent"
android:layout_height="match_parent"/>


</RelativeLayout>

日志错误:

   08-13 01:54:05.850: E/AndroidRuntime(4183): FATAL EXCEPTION: main
08-13 01:54:05.850: E/AndroidRuntime(4183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fambond/com.example.fambond.map}: java.lang.NullPointerException
08-13 01:54:05.850: E/AndroidRuntime(4183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at android.os.Looper.loop(Looper.java:130)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at android.app.ActivityThread.main(ActivityThread.java:3691)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at java.lang.reflect.Method.invoke(Method.java:507)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at dalvik.system.NativeStart.main(Native Method)
08-13 01:54:05.850: E/AndroidRuntime(4183): Caused by: java.lang.NullPointerException
08-13 01:54:05.850: E/AndroidRuntime(4183):     at com.example.fambond.map.onCreate(map.java:18)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
08-13 01:54:05.850: E/AndroidRuntime(4183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-13 01:54:05.850: E/AndroidRuntime(4183):     ... 11 more
4

1 回答 1

0

大概是这条线

 map = fm.getMap(); 

返回 null,然后在 null 对象上调用某些东西 -> 异常

于 2013-08-16T11:14:34.333 回答