【问题标题】:How to fix onStart error when adding a mapView to a fragment [duplicate]将mapView添加到片段时如何修复onStart错误[重复]
【发布时间】:2019-05-01 13:05:32
【问题描述】:

我正在尝试将 mapView 放入片段中。 onStart 有问题,但是,当我从 onStart 方法中删除 mapView.onStart(); 时,30 秒后地图开始缓慢加载?

尝试调用虚方法'void com.google.android.gms.maps.MapView.onStart()' 在空对象上 参考 在 HomeActivity.onStart(HomeActivity.java:501)

(在这种情况下,第 501 行是“mapView.onStart();”)

我尝试了不同的教程并重新格式化。我也尝试过使用不同的 google api 服务。我认为问题在于我使用的是碎片化视图。

HomeActivity.java

public class HomeActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, MyRecyclerViewAdapter.ItemClickListener, OnMapReadyCallback {


    private MapView mapView;
    private GoogleMap gmap;
    private static final String MAP_VIEW_BUNDLE_KEY = "MapViewBundleKey";


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

        session = new SessionHandler(getApplicationContext());
        final User user = session.getUserDetails();



        final View background = findViewById(R.id.home_background_view);
        final ViewPager viewPager = (ViewPager) findViewById(R.id.home_view_pager);
        MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.am_tab_layout);
        tabLayout.setupWithViewPager(viewPager);


        loadLocations();


        viewPager.setCurrentItem(1);





            @Override
            public void onPageSelected(int position) {
                if(position == 0)
                {
                    //code for first screen

                }if(position == 2){
                    //code for when screen is on the Map View

                    Bundle mapViewBundle = null;
                    if (savedInstanceState != null) {
                        mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
                    }

                    mapView = findViewById(R.id.mapView);
                    mapView.onCreate(mapViewBundle);
                    mapView.getMapAsync(HomeActivity.this);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        Bundle mapViewBundle = outState.getBundle(MAP_VIEW_BUNDLE_KEY);
        if (mapViewBundle == null) {
            mapViewBundle = new Bundle();
            outState.putBundle(MAP_VIEW_BUNDLE_KEY, mapViewBundle);
        }

        mapView.onSaveInstanceState(mapViewBundle);
    }


    @Override
    protected void onPause() {
        mapView.onPause();
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        mapView.onDestroy();
        super.onDestroy();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        gmap = googleMap;
        //gmap.setMinZoomPreference(12);
        LatLng ny = new LatLng(40.7143528, -74.0059731);
        gmap.moveCamera(CameraUpdateFactory.newLatLng(ny));
    }


}

fragment_map.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:background="@color/CafeSeaBlue"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="80dp"
        android:background="@drawable/card_background" />

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/latText1"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:layout_below="@+id/longText"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="50dp"
        android:background="@color/White"
        android:text="Map Screen"
        android:textAlignment="center"/>

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" />
</FrameLayout>

Logcat

2019-05-01 14:15:33.397 20032-20032/im.craig.locateio E/AndroidRuntime: FATAL EXCEPTION: main
    Process: im.craig.locateio, PID: 20032
    java.lang.RuntimeException: Unable to start activity ComponentInfo{im.craig.locateio/im.craig.locateio.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapView.onStart()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapView.onStart()' on a null object reference
        at im.craig.locateio.HomeActivity.onStart(HomeActivity.java:501)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
        at android.app.Activity.performStart(Activity.java:7029)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

【问题讨论】:

  • 发布日志
  • 将其添加到帖子中

标签: java android google-maps android-fragments


【解决方案1】:

尝试在 Oncreate 方法中初始化 MapView,而不是在 onPageSelected 方法中。从 onPageSelected 方法中删除它并在 OnCreate 方法中执行。

mapView = findViewById(R.id.mapView);

低于这条线会更好

viewPager.setCurrentItem(1);

所以它看起来像这样。

viewPager.setCurrentItem(1);
mapView = findViewById(R.id.mapView);

【讨论】:

  • 谢谢,我已尝试将其移至 onCreate 中,但似乎不起作用。然后我尝试将该部分中的所有内容移动到 onCreate 中,我得到了这个错误原因:java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapView.onCreate(android.os. Bundle)' 在 im.craig.locateio.HomeActivity.onCreate(HomeActivity.java:166) 的空对象引用上第 166 行是“mapView.onCreate(mapViewBundle);”
【解决方案2】:

您需要调用 onDestroy 作为 Fragment#OnDestroyView 的一部分,而不是 Fragment#onDestroy。挂钩到正确的生命周期将解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多