的GetMap()中的onCreate(),从布局创建提供了NullPointerException异常 [英] getMap() in onCreate(), created from layout gives nullpointerexception

查看:178
本文介绍了的GetMap()中的onCreate(),从布局创建提供了NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

里面的onCreate()方法:(我只有这处的onCreate(),除了一个包创建和变量asignation,使用该软件包)

Inside onCreate() method:(I have only this at onCreate(), aside a bundle creation and variables asignation, using that bundle.)

FragmentManager myFM = getSupportFragmentManager();
SupportMapFragment myMAPF =(SupportMapFragment)myFM.
      findFragmentById(R.id.mapfragment);
map=myMAPF.getMap();//Exception at this line

布局:

<fragment
    android:id="@+id/mapfragment"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

清单:

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

我不得不提的是,当我生成的API密钥我复制了debug.keystore文件,并存储在一个更方便的位置,然后生成它。我再次做到了,在原来的位置,原来的文件,但返回相同SH1,所以我没有产生另一个API密钥。激活了断续的API访问页面。

I have to mention that when i generated the api key I copied the debug.keystore file and stored in a more convenient location, and then generated it. I did it again with the original file in the original location, and returned the same SH1, however, so I didnt generated another API key. Have activated the interruptor in the API Access page.

我已经看到了一些答案前,询问了同样的事情,我已经尝试了很多自己的解决方案...没有运气。

I have seen some answer asking the same thing before, and I have tried a lot of their solutions... without luck.

我运行了Android 4.0版本的设备上的应用程序,通过USB,从Eclipse中。

I am running the app on a device with Android version 4.0, via USB, from Eclipse.

任何人都可以帮助我找到在哪里错误?谢谢你。

Anyone can help me to find where is the error? Thank you.

修改

对不起,忘了堆栈跟踪

10-23 13:54:53.230: E/AndroidRuntime(6786): java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/mypackage.MapActivity}: java.lang.NullPointerException
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.os.Looper.loop(Looper.java:137)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at java.lang.reflect.Method.invoke(Method.java:511)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at dalvik.system.NativeStart.main(Native Method)
10-23 13:54:53.230: E/AndroidRuntime(6786): Caused by: java.lang.NullPointerException
10-23 13:54:53.230: E/AndroidRuntime(6786):     at mypackage.MapActivity.onCreate(MapActivity.java:73)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.Activity.performCreate(Activity.java:4470)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

我只注意到一个奇怪的事情,在堆栈跟踪的第一行把我的应用程序的完整包两次......为什么会这样?

I just noticed a strange thing, in the first line of the stacktrace puts twice the full package of my app...why is this happening?

推荐答案

您的地图尚未公布...你可以使用类似下面的方法来确定是否播放业务都可以利用它们(我称之为前它在onActivityCreated,而非的onCreate):

Your Map is not yet available... You can use something like the following method to determine if Play Services are available before making use of them (I would call it in onActivityCreated, rather than onCreate) :

protected boolean isPlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (status == ConnectionResult.SUCCESS) {
        return (true);
    } else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
        // deal with error
    } else {
            // maps is not available
    }

    return (false);
}

此外,您的片段需要实现由播放服务所需的回调(接口GooglePlayServicesClient.ConnectionCallbacks和GooglePlayServicesClient.OnConnectionFailedListener:

Also, your Fragment needs to implement the Callbacks required by the Play Services (interfaces GooglePlayServicesClient.ConnectionCallbacks and GooglePlayServicesClient.OnConnectionFailedListener :

    /*
     * Called by Location Services when the request to connect the
     * client finishes successfully. At this point, you can
     * request the current location or start periodic updates
     */
    @Override
    public void onConnected(Bundle dataBundle) {
        // Display the connection status
        Toast.makeText(context, "Connected to location services", Toast.LENGTH_SHORT).show();

    }

    /*
     * Called by Location Services if the connection to the
     * location client drops because of an error.
     */
    @Override
    public void onDisconnected() {
        // Display the connection status
        Toast.makeText(context, "Disconnected from location services. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

    /*
     * Called by Location Services if the attempt to
     * Location Services fails.
     */
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        /*
         * Google Play services can resolve some errors it detects.
         * If the error has a resolution, try sending an Intent to
         * start a Google Play services activity that can resolve
         * error.
         */
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(
                        SomeActivity,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
                /*
                 * Thrown if Google Play services canceled the original
                 * PendingIntent
                 */
            } catch (IntentSender.SendIntentException e) {
                // Log the error
                e.printStackTrace();
            }
        } else {
            /*
             * If no resolution is available, display a dialog to the
             * user with the error.
             */
            Toast.makeText(context, "Connection error", Toast.LENGTH_SHORT).show();
        }
    }
}

编辑: 这是我如何使用它在我的活动包含地图片段:

EDIT : This is how I use it in my activity containing the map fragment :

if (isPlayServicesAvailable()) {
    setContentView(R.layout.activity_with_map_fragment);
}

和在片段:

if (getMap() != null) {
    GoogleMap map = getMap();
    // do things with the map
}

这应该让你去; - )

This should get you going ;-)

这篇关于的GetMap()中的onCreate(),从布局创建提供了NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆