为什么这行代码会使我的应用程序崩溃

时间:2014-04-16 22:05:07

标签: android google-maps

我在这个网站上看到过很多其他类似的问题,并试图更改我的代码以匹配,但它从未解决问题。

我的应用加载了一些按钮来推送。其中一个打开一个类文件,在屏幕上打开一个地图。该类的代码是:

public class FuelMap extends Activity 
{   

private final LatLng locBurb = new LatLng(49.27645, -122.917587);
private final LatLng locSura = new LatLng(49.187500, -122.849000);  
private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fuel_map);

    //map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map )).getMap();
    // Show the Up button in the action bar.
    setupActionBar();
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() 
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fuel_map, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void OnClickCity(View C)
{
    //CameraUpdate update = CameraUpdateFactory.newLatLng(locBurb);
    //map.animateCamera(update);
}

public void OnClickBuraby(View B)
{

}

public void OnClickSurray(View S)
{

}

}

但如果我取消注释该行://map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map )).getMap();

然后应用程序将运行,直到我尝试将其切换到地图。

就像我在开头所说的那样,我已经搜索并经常在这个网站上找到与我的问题相关的答案,但没有一个给我任何结果。我不相信这个问题是由于我可以加载地图而安装Android应用程序所需的谷歌地图的错误。所以任何人都知道可能是什么原因?

-LogCat info

04-16 17:16:10.747: D/AndroidRuntime(11672): Shutting down VM
04-16 17:16:10.747: W/dalvikvm(11672): threadid=1: thread exiting with uncaught exception (group=0x41543300)
04-16 17:16:10.752: E/AndroidRuntime(11672): FATAL EXCEPTION: main
04-16 17:16:10.752: E/AndroidRuntime(11672): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.fuelapptesting/com.testing.fuelapptesting.FuelMap}: java.lang.NullPointerException
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.os.Looper.loop(Looper.java:137)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.main(ActivityThread.java:4745)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at dalvik.system.NativeStart.main(Native Method)
04-16 17:16:10.752: E/AndroidRuntime(11672): Caused by: java.lang.NullPointerException
04-16 17:16:10.752: E/AndroidRuntime(11672):    at com.testing.fuelapptesting.FuelMap.onCreate(FuelMap.java:31)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.Activity.performCreate(Activity.java:5008)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-16 17:16:10.752: E/AndroidRuntime(11672):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-16 17:16:10.752: E/AndroidRuntime(11672):    ... 11 more
04-16 17:16:13.580: I/Process(11672): Sending signal. PID: 11672 SIG: 9

- activity_Fuel_Map布局

    <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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/maps"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/busCity" />

    <TextView
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/maps"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="33dp"
        android:ems="10"
        android:text="Map" >

        <requestFocus />
    </TextView>

    <Button
        android:id="@+id/buttBuraby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/map"
        android:layout_toRightOf="@+id/buttCity"
        android:onClick="OnClickBuraby"
        android:text="Buraby" />

    <Button
        android:id="@+id/buttSurray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/map"
        android:layout_toRightOf="@+id/buttBuraby"
        android:onClick="OnClickSurray"
        android:text="Surray" />

    <Button
        android:id="@+id/buttCity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttBuraby"
        android:layout_alignBottom="@+id/buttBuraby"
        android:layout_alignLeft="@+id/maps"
        android:onClick="OnClickCity"
        android:text="City" />


</RelativeLayout>

- 清单文件

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testing.fuelapptesting"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <!-- The following two permissions are not required to use
        Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>


    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.testing.fuelapptesting.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.testing.fuelapptesting.News"
            android:label="@string/title_activity_news"
            android:parentActivityName="com.testing.fuelapptesting.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.testing.fuelapptesting.MainActivity" />
        </activity>
        <activity
            android:name="com.testing.fuelapptesting.Messages"
            android:label="@string/title_activity_messages" >
        </activity>
        <activity
            android:name="com.testing.fuelapptesting.FuelMap"
            android:label="@string/title_activity_fuel_map"
            android:parentActivityName="com.testing.fuelapptesting.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.testing.fuelapptesting.MainActivity" />
        </activity>
        <activity
            android:name="com.testing.fuelapptesting.Scanner"
            android:label="@string/title_activity_scanner"
            android:parentActivityName="com.testing.fuelapptesting.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.testing.fuelapptesting.MainActivity" />
        </activity>
        <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyC8hlQR9rdDGYMEXQCqpMD2PxzQRrpprPg"/>

    </application>

</manifest>

3 个答案:

答案 0 :(得分:1)

试试此链接:in google maps V2 ... fragment.getMap() returns null

在onCreateView()中,您尝试加载的地图片段尚未实例化。 希望这能解决问题。您可能需要在地图加载后发布到处理程序..本质上是一个回调。

答案 1 :(得分:0)

我似乎在使用Maps V2,因此您在xml中的声明不正确。

Android Developer

中提到的片段不是FragmentLayout
    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

答案 2 :(得分:0)

非常感谢所有帮助人员。我最终弄明白了这个问题。这是字母&#39;。

在这一行://map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map )).getMap();我正在拉动名为map的对象。如果查看我的activity_Fuel_Map布局,则地图对象是TextView。

`    <TextView
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/maps"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="33dp"
        android:ems="10"
        android:text="Map" >`

所以我不得不将(R.id.map)更改为(R.id.maps)来拉取我的地图片段。

<fragment
    android:id="@+id/maps"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/busCity" />

再次感谢所有帮助人员。