从活动切换到MapActivity

时间:2010-08-30 09:30:15

标签: java android google-maps map android-activity

        Button showmapButton = (Button)findViewById(R.id.showmap);
    showmapButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    Intent intent = new Intent(HomePage.this,
      ShowMap.class);
    intent.putExtra("username", value);
    startActivity(intent);
   }
        });

在我的主菜单中,我有一个按钮'ShowMap' 每次我点击showmap我的地图都会遇到空指针异常,导致程序崩溃。 'ShowMap'适用于fi 如果我直接启动该页面而无需点击按钮来调用该活动。

我尝试将所有文​​件更改为“扩展MapActivity”而不是常规活动,但只要我点击“显示地图”,程序仍会崩溃


D/AndroidRuntime(  347): Shutting down VM
W/dalvikvm(  347): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  347): FATAL EXCEPTION: main
E/AndroidRuntime(  347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mp/com.mp.ShowMap}: java.lang.NullPointerException
E/AndroidRuntime(  347):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  347):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  347):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  347):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  347):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  347):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  347):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  347):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  347):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  347):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  347):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  347):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  347): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  347):    at com.mp.ShowMap.onCreate(ShowMap.java:46)
E/AndroidRuntime(  347):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  347):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  347):    ... 11 more

2 个答案:

答案 0 :(得分:0)

好吧,伙计们,我找到了我的NPE!

(前)

public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

(后)

public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.map);

因为我们正试图整合作业,所以有些粗心的错误!

谢谢大家:D

答案 1 :(得分:0)

Intent intent = new Intent();
intent.setClass(AndroidGPSTrackingActivity.this, mapslayout.class);
startActivity(intent);



 <uses-library
            android:name="com.google.android.maps"
            android:required="true" />**