我正在android studio中使用master-flow详细信息模板,当我在Fragment类的onCreate()方法中调用以下方法时尝试运行我的代码时,我一直收到空指针错误:
private void setUpMapIfNeeded() {
fm = getActivity().getSupportFragmentManager();
if (mMap == null) {
mMap = ((SupportMapFragment) fm.findFragmentById(R.id.mapFragment)).getMap();
}
}
我的布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_weight="0.9"
tools:context=".CategoryDetailFragment"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/record_audio"
android:id="@+id/recordaudiobutton"
android:onClick="displayLongitudeLatitude"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/longitudeTester" />
</LinearLayout>
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mark.markmaptester2/com.example.mark.markmaptester2.CategoryDetailActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5323)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.mark.markmaptester2.CategoryDetailFragment.setUpMapIfNeeded(CategoryDetailFragment.java:177)
at com.example.mark.markmaptester2.CategoryDetailFragment.onCreate(CategoryDetailFragment.java:72)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1766)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
at android.app.Activity.performStart(Activity.java:5353)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
也许这是一个简单的修复,但我无法确定问题 感谢
答案 0 :(得分:0)
在Manifest.xml中添加此内容
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YourKey" />
<activity
android:name=".YourActivityClassName"
android:label="Class Name" >
<intent-filter>
<action android:name="package.name.YOURACTIVITYNAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
请检查:Maps Api Guide