【问题标题】:Runtime error in GoogleMap appGoogleMap 应用程序中的运行时错误
【发布时间】:2015-03-23 14:55:28
【问题描述】:

我正在使用 GoogleMaps 编写应用程序。我现在在模拟器上运行它。 下面的布局适用于 app。地图画得很好。 我想在带有地图的屏幕上添加一些额外的 TextView 和按钮。 但是如果我将第二个 TextView 或按钮添加到我的 XML 中,则会出现运行时错误。 如果我将它们添加到一些 LinearLayout 中,我也会得到错误。

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@color/backNormal"
          android:orientation="vertical"
    >
<TextView
        android:id="@+id/mytext2"
        android:gravity="center"
        android:text='My Text'
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/text_style_title"
        />
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="match_parent"
              android:background="@color/backNormal"
              android:orientation="vertical"
        >
    <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
</LinearLayout>
</LinearLayout>

错误文字如下:

3459-3459/moorlakgames.packet1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: moorlakgames.packet1, PID: 3459
    java.lang.RuntimeException: Unable to start activity ComponentInfo{moorlakgames.packet1/moorlakgames.packet1.MyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference

正如我所说,它适用于布局中的一个 TextView,但不适用于两个 TextView。 有任何想法吗?谢谢!

【问题讨论】:

    标签: android google-maps fragment google-maps-android-api-2


    【解决方案1】:

    这样试试

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@color/backNormal"
              android:orientation="vertical"
        >
    <TextView
            android:id="@+id/mytext2"
            android:gravity="center"
            android:text='My Text'
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/text_style_title"/>
       <LinearLayout 
            android:id="@+id/myLayout"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="@color/backNormal"
            android:orientation="vertical">
        <fragment
              android:id="@+id/map"
              android:name="com.google.android.gms.maps.MapFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
      </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    • 不幸的是,如果我在下面的第一个或 LineraLayout 内部添加第二个 TextView,它不起作用。
    • 发布您的 layout.xml 文件
    • 它在我上面的开始消息中。我怀疑布局中带有片段的其他元素是被禁止的。
    【解决方案2】:

    对于未来的情况,使用缩进,它会让你的生活更轻松。

    如果上面的 XML 是您的整个布局,您还没有关闭外部 LinearLayout,这可能会导致 LayoutInflater 膨胀一个空的 View,从而导致您在执行时得到 NullPointerException

    ((MapFragment)findViewById(R.id.map)).getMap();
    

    【讨论】:

    • 抱歉,不明白。 findViewById 不起作用。我使用 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();正如我所说,它适用于一个 TextView,而不是两个以上的元素。
    • 我怀疑在fragment的布局中禁止使用按钮和其他元素。
    • 他们是允许的,这不是问题。您是否通过ActivitysetContentView(int resourceId) 方法扩展此布局?
    • 是的。当然我首先使用这一行: setContentView(R.layout.page_one);然后 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();奇怪的是,只有在布局中的 textView 上一切正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多