0

我正在尝试在运行时将 Google Map 添加到特定布局。

mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(android.R.id.content, mMapFragment, MAP_FRAGMENT_TAG);
mainLayout.addView(mMapFragment);

在添加mainLayout.addView(mMapFragment)时,它显示空指针错误。

错误

(19:34) amitsharma.cssoft: 09-30 19:23:40.457: E/AndroidRuntime(16595): FATAL EXCEPTION: main
09-30 19:23:40.457: E/AndroidRuntime(16595): java.lang.NullPointerException
09-30 19:23:40.457: E/AndroidRuntime(16595):    at android.view.ViewGroup.addView(ViewGroup.java:1821)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at android.view.ViewGroup.addView(ViewGroup.java:1808)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at com.example.project_katchup.Settings.ShowCurrentLocation(Settings.java:222)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at com.example.project_katchup.Settings.access$0(Settings.java:211)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at com.example.project_katchup.Settings$1.onClick(Settings.java:151)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at android.view.View.performClick(View.java:2538)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at android.view.View$PerformClick.run(View.java:9152)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at android.os.Handler.handleCallback(Handler.java:587)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at android.os.Looper.loop(Looper.java:130)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at android.app.ActivityThread.main(ActivityThread.java:3687)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at java.lang.reflect.Method.invokeNative(Native Method)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at java.lang.reflect.Method.invoke(Method.java:507)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-30 19:23:40.457: E/AndroidRuntime(16595):    at dalvik.system.NativeStart.main(Native Method)
09-30 19:28:46.757: I/Process(16595): Sending signal. PID: 16595 SIG: 9
4

1 回答 1

0

删除线

mainLayout.addView(mMapFragment);

在您调用mainLayout的相对布局中,您应该有一个名为 android.R.id.content 的 FrameLayout。也许是这样的:

<FrameLayout
    android:id="@android:id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

或:使用android:id="@+id/container_map"并替换android.R.id.contentR.id.container_map


没有 xml:

删除线

mainLayout.addView(mMapFragment);

开头:

mainLayout.setId(R.id.container_map);

替换android.R.id.contentR.id.container_map

在 res/values 中创建文件 ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="container_map" type="id" />
</resources>
于 2013-09-30T14:10:02.970 回答