试图访问mapfragment时,应用程序崩溃 [英] App crash when trying to access mapfragment

查看:109
本文介绍了试图访问mapfragment时,应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了学校与mapfragment和一个按钮的项目,但是当我尝试访问我的地图,它崩溃完全 这是我的code:

 包com.helloandroid.mapview;

进口android.app.FragmentManager;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v7.app.ActionBarActivity;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;

进口com.google.android.gms.maps *。
进口com.google.android.gms.maps.model *。


公共类MainActivity扩展ActionBarActivity {
    GoogleMap的MYMAP;
    按钮btnShow;
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

    如果(savedInstanceState == NULL){
        getSupportFragmentManager()的BeginTransaction()
                。新增(R.id.container,新PlaceholderFragment())
                。承诺();
    }
    btnShow =(按钮)findViewById(R.id.btnShow);
 

这是它崩溃的行:

  SupportMapFragment mapFrag =(SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。

    MYMAP = mapFrag.getMap();



}




/ **
 *包含一个简单的视图的占位符片段。
 * /
公共静态类PlaceholderFragment扩展片段{

    公共PlaceholderFragment(){
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
        返回rootView;
    }
}
 

}

这是我的logcat的结果:

  

  01-09 20:19:57.475 28744-28744 /? E / AndroidRuntime:致命异常:主要
    java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.helloandroid.mapview / com.helloandroid.mapview.MainActivity}:显示java.lang.NullPointerException
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
            在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
            在android.app.ActivityThread.access $ 600(ActivityThread.java:156)
            在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1340)
            在android.os.Handler.dispatchMessage(Handler.java:99)
            在android.os.Looper.loop(Looper.java:153)
            在android.app.ActivityThread.main(ActivityThread.java:5297)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:511)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            在dalvik.system.NativeStart.main(本机方法)
     显示java.lang.NullPointerException:产生的原因
            在com.helloandroid.mapview.MainActivity.onCreate(MainActivity.java:34)
            在android.app.Activity.performCreate(Activity.java:5122)
            在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
在android.app.ActivityThread.access $ 600(ActivityThread.java:156)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1340)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:153)
在android.app.ActivityThread.main(ActivityThread.java:5297)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
在dalvik.system.NativeStart.main(本机方法)
 

这是mapfragment     

 <片段
        机器人:ID =@ + ID /图
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:名称=com.google.android.gms.maps.MapFragment
        机器人:layout_below =@ + ID / btnShow/>

    <按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=秀
        机器人:ID =@ + ID / btnShow
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真/>

< / RelativeLayout的>
 

有谁知道我可以解决这个问题?

在此先感谢

解决方案

正确的设置

首先,你需要检查设备是否具有正确的谷歌播放服务,如果它没有得到它设置正确,你将无法使用地图都没有。

 如果(GooglePlayServicesUtil.isGooglePlayServicesAvailable(这一点)!= ConnectionResult.SUCCESS){
        //在这里处理情况
    }
 

二,你没有得到任何保证,地图将准备,所以你需要尝试建立地图多次,例如:调用此方法,无论是从的OnCreate(..) onResume(..)

 私人无效setUpMapIfNeeded(){
    //做一个空检查确认,我们还没有实例化的地图。
    如果(MMAP == NULL){
        //尝试获取来自SupportMapFragment地图。
        MMAP =((SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。)的GetMap()。
        //检查如果我们成功取得地图。
        如果(MMAP!= NULL){
            //这里你可以设置映射你想要的方式
        }
    }
}
 

你的错误

您已经看到了 NullPointerException异常当您访问SupportMapFragment?检查以下内容:

  • 您确认您已经包含了Android的支持,v4.jar支持库?
  • 你肯定R.id.map存在?
  • 你有谷歌play业务的lib正确包含在你的项目?

如果您仍然遇到问题,还应该包括XML的地图碎片。

修改

您的XML的设置应该有这种

类=com.google.android.gms.maps.SupportMapFragment,即删除安卓名称从片段的属性,并添加类。

i am creating a project for school with a mapfragment and a button, but when i try to access my map, it crashes completely this is my code:

package com.helloandroid.mapview;

import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;


public class MainActivity extends ActionBarActivity {
    GoogleMap myMap;
    Button btnShow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    btnShow = (Button) findViewById(R.id.btnShow);

this is the line where it crashes:

SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    myMap = mapFrag.getMap();



}




/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

this is my logcat result:

01-09 20:19:57.475  28744-28744/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.helloandroid.mapview/com.helloandroid.mapview.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
            at android.app.ActivityThread.access$600(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5297)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.helloandroid.mapview.MainActivity.onCreate(MainActivity.java:34)
            at android.app.Activity.performCreate(Activity.java:5122)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
            at android.app.ActivityThread.access$600(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5297)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

this is the mapfragment

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show"
        android:id="@+id/btnShow"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

does anybody know how i can fix this?

thanks in advance

解决方案

Correct setup

First off you need to check if the device has the correct Google Play services, if it hasn't got it properly set up, you will not be able to use the map at all.

    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) != ConnectionResult.SUCCESS) {
        // Handle the case here
    }

Second, you don't get any guarantee that the map will be ready, therefore you will need to try to set up the map multiple times, e.g. call this method both from OnCreate(..) and onResume(..).

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            // here you can setup the map the way you want
        }
    }
}

Your error

You are getting a NullPointerException when you access the SupportMapFragment? Check the following:

  • Are you sure that you have included the android-support-v4.jar support library?
  • Are you sure that R.id.map exists?
  • Do you have the google play services lib correctly included in your project?

If you still run into problems you should include the xml for the map fragment.

EDIT

Your XML setup should have this

class="com.google.android.gms.maps.SupportMapFragment", i.e. remove the android:name attribute from fragment and add the class.

这篇关于试图访问mapfragment时,应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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