知道我的代码有什么问题吗? 我只是想显示地图 但当我点击textview时,地图有强制关闭错误。 我通过添加
在XML布局中完成了必要的操作android:onClick="onClick"
android:clickable="true"
我检查了map.java,实际上它工作正常。 我也检查了onclick textview,它也正常工作。 但是,当我想将两者链接在一起时,意味着在onclick textview上显示map map.java。但它给了我错误。 有人可以告诉我我的代码有什么问题吗? 提前谢谢。
onclick textview代码: 我已经实现了onclicklistener
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
startActivity(new Intent(SingleMenuItemActivity.this,
map.class));
}
map.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>
logcat错误:
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
答案 0 :(得分:0)
可能是这一行
map = fm.getMap();
返回null,然后在null对象上调用一些东西 - &gt;例外