markerinfowindowadapter在用户从菜单屏幕返回应用程序时导致崩溃

jgwigjjp  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(265)

我是一个android新手和工作的项目是包含谷歌Mapv2和标记。它显示了谷歌Map上的一些位置及其详细信息。如果用户点击Map上的任何标记,它会像往常一样显示带有点击标记详细信息的markerinfowindow。
当我第一次运行这个项目的时候,一切都很好。当我在我的应用程序中按下菜单按钮(设备菜单按钮)时,android退出我的应用程序并像往常一样返回主屏幕。
但是,当我继续我的应用程序(点击菜单屏幕上的应用程序图标)并点击Map上的任何标记时,我的应用程序就会崩溃。给了我一个错误。
如果我按后退按钮(设备后退按钮),直到回到主屏幕退出应用程序,当我像往常一样用图标恢复应用程序时,一切正常,没有崩溃或任何错误。
我的申请有什么问题?为什么我的应用程序崩溃时,用户退出我的应用程序与菜单按钮,为什么不粉碎时,用户退出与后退按钮?
我知道我的英语不好。但是,我尽力了。谢谢你的理解。
这是我的错误;

01-15 13:18:05.678    3090-3090/com.xxx.xxxapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62e3288)
01-15 13:18:05.678    3090-3090/com.xxx.xxxapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.xxx.xxxapp.MarkerInfoWindowAdapter.getInfoContents(MarkerInfoWindowAdapter.java:25)
            at com.google.android.gms.maps.GoogleMap$13.g(Unknown Source)
            at com.google.android.gms.maps.internal.d$a.onTransact(Unknown Source)
            at android.os.Binder.transact(Binder.java:326)
            at com.google.android.gms.maps.internal.p.b(SourceFile:112)
            at com.google.maps.api.android.lib6.c.h.a(Unknown Source)
            at com.google.maps.api.android.lib6.c.h.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.c.h.b(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.c.j.c(Unknown Source)
            at com.google.maps.api.android.lib6.c.al.c(Unknown Source)
            at com.google.maps.api.android.lib6.c.aj.g(Unknown Source)
            at com.google.maps.api.android.lib6.c.al.h(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.c.h.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.aw.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.bf.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.be.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.bu.d(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.ak.onSingleTapConfirmed(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.i.g.onSingleTapConfirmed(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.i.i.handleMessage(Unknown Source)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            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:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

这是我的主要活动课;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    FragmentManager manager;

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

        manager = getSupportFragmentManager();

        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
}

有我的自定义标记信息类;

package com.xxx.xxxapp;

public class Markers {

    public String mAdi;
    public String mAdres;
    public String mTelefon;
    public String mFace;
    public String mWeb;
    public boolean mGeceAcik;
    public boolean mMember;
    public boolean mBarinak;
    public Double mLatitude;
    public Double mLongtitude;

    public Markers(String mAdi, String mAdres, String mTelefon, String mFace, String mWeb, boolean mBarinak, boolean mGeceAcik, boolean mMember, Double mLatitude, Double mLongtitude) {
        this.mAdi = mAdi;
        this.mAdres = mAdres;
        this.mTelefon = mTelefon;
        this.mFace = mFace;
        this.mWeb = mWeb;
        this.mGeceAcik = mGeceAcik;
        this.mMember = mMember;
        this.mLatitude = mLatitude;
        this.mLongtitude = mLongtitude;
        this.mBarinak = mBarinak;
    }

    public boolean ismBarinak() {
        return mBarinak;
    }

    public void setmBarinak(boolean mBarinak) {
        this.mBarinak = mBarinak;
    }

    public String getmAdi() {
        return mAdi;
    }

    public void setmAdi(String mAdi) {
        this.mAdi = mAdi;
    }

    public String getmAdres() {
        return mAdres;
    }

    public void setmAdres(String mAdres) {
        this.mAdres = mAdres;
    }

    public String getmTelefon() {
        return mTelefon;
    }

    public void setmTelefon(String mTelefon) {
        this.mTelefon = mTelefon;
    }

    public String getmFace() {
        return mFace;
    }

    public void setmFace(String mFace) {
        this.mFace = mFace;
    }

    public String getmWeb() {
        return mWeb;
    }

    public void setmWeb(String mWeb) {
        this.mWeb = mWeb;
    }

    public boolean ismGeceAcik() {
        return mGeceAcik;
    }

    public void setmGeceAcik(boolean mGeceAcik) {
        this.mGeceAcik = mGeceAcik;
    }

    public boolean ismMember() {
        return mMember;
    }

    public void setmMember(boolean mMember) {
        this.mMember = mMember;
    }

    public Double getmLatitude() {
        return mLatitude;
    }

    public void setmLatitude(Double mLatitude) {
        this.mLatitude = mLatitude;
    }

    public Double getmLongtitude() {
        return mLongtitude;
    }

    public void setmLongtitude(Double mLongtitude) {
        this.mLongtitude = mLongtitude;
    }
}

这是我的mapfragment类;

package com.xxx.xxxapp;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class HaritaFragment extends Fragment implements LocationListener {

    private static GoogleMap mGoogleMap;
    public static HashMap<Marker, Markers> mMarkersHashMap;
    private ArrayList<Markers> mMyMarkersArray = new ArrayList<Markers>();
    private Map<Marker, Markers> allMarkersMap = new HashMap<Marker, Markers>();

    private LatLng latLng;
    private Double lat;
    private Double lng;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_harita, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();

        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity().getBaseContext());

        if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), requestCode);
            dialog.show();

        } else { // Google Play Services are available

            setsVetMarkers();
            setUpMap();
            plotMarkers(mMyMarkersArray);

        }
    }

    private void setsVetMarkers(){

        // Initialize the HashMap for Markers and MyMarker object
        mMarkersHashMap = new HashMap<Marker, Markers>();

        //public Markers(String mAdi, String mAdres, String mTelefon, String mFace, String mWeb, boolean mBarinak, boolean mGeceAcik, boolean mMember, Double mLatitude, Double mLongtitude)

        mMyMarkersArray.add(new Markers(
                "PetVetam",
                "Emirhan cad.",
                "0532-555-5555",
                "https://www.facebook.com",
                "http://www.xxxx.com",
                false,
                true,
                true,
                Double.parseDouble("41.05484176"),
                Double.parseDouble("29.00497913")));

        mMyMarkersArray.add(new Markers("marina", "Muallim Naci Cad. N:49 D:3 Ortaköy Mah.", "0212-265-0000","YOK" ,
                "YOK", true, false, true, Double.parseDouble("41.048846"), Double.parseDouble("29.027924")));

        mMyMarkersArray.add(new Markers("Animate Vetamats", "Duygu Sk No:2 Levent", "0212-000000","https://www.facebook.com/pages/Animate-Veteriner/562263330470083" ,
                "http://www.animate.com.tr", false, false, false, Double.parseDouble("41.094460"), Double.parseDouble("28.998754")));

    }

    private void plotMarkers(ArrayList<Markers> markers)
    {
        if(markers.size() > 0)
        {
            for (Markers myMarker : markers)
            {

                // Create user marker with custom icon and other options
                MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongtitude()));

                if (myMarker.ismGeceAcik()){

                    markerOption.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            }
                else if (myMarker.ismBarinak()){

                    markerOption.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
                }

                Marker currentMarker = mGoogleMap.addMarker(markerOption);
                mMarkersHashMap.put(currentMarker, myMarker);
                allMarkersMap.put(currentMarker, myMarker);

                mGoogleMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());

            }
        }
    }

    private void  setUpMap(){

        mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
        mGoogleMap.setMyLocationEnabled(true);

        // Get the location manager
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the location provider -> use
        // default

        //Criteria criteria = new Criteria();
        //String provider = locationManager.getBestProvider(criteria, false);

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        // Initialize the location fields

        //Location eğer varsa mapi usercurrent location ile yaratıyor.
        if (location != null) {

            onLocationChanged(location);
            latLng = new LatLng(lat, lng);
            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));

            //Location yoksa default value ve zoom value ile yaratıyor.
        }else{

            latLng = new LatLng(41.048846, 29.027924);
            Toast.makeText(getActivity(),getActivity().getString(R.string.location_error_message),Toast.LENGTH_LONG).show();
            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
        }

        //OnClick Info Window Listener...
        mGoogleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {

                //Set your InfoWindow Click Events...

                Markers selectedMarkerInfo = allMarkersMap.get(marker);

                Intent intent = new Intent(getActivity(),DetailActivity.class);

                intent.putExtra("adi",selectedMarkerInfo.getmAdi());
                intent.putExtra("adres",selectedMarkerInfo.getmAdres());
                intent.putExtra("web",selectedMarkerInfo.getmWeb());
                intent.putExtra("face",selectedMarkerInfo.getmFace());
                intent.putExtra("tel",selectedMarkerInfo.getmTelefon());
                intent.putExtra("lat",selectedMarkerInfo.getmLatitude().toString());
                intent.putExtra("lng",selectedMarkerInfo.getmLongtitude().toString());
                intent.putExtra("gece",selectedMarkerInfo.ismGeceAcik());
                intent.putExtra("member",selectedMarkerInfo.ismMember());
                intent.putExtra("barinak",selectedMarkerInfo.ismBarinak());

                startActivity(intent);

            }
        });

    }

    @Override
    public void onLocationChanged(Location location) {

            lat = (Double) (location.getLatitude());
            lng = (Double) (location.getLongitude());
    }
}

还有markerinfowindowadapter类;

package com.xxx.xxxapp;

import android.view.View;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter
{
    public MarkerInfoWindowAdapter()
    {

    }

    @Override
    public View getInfoWindow(Marker marker)
    {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker)
    {
        Markers myMarker = HaritaFragment.mMarkersHashMap.get(marker);
        marker.setTitle(myMarker.getmAdi());
        marker.setSnippet(myMarker.getmAdres());

        return null;
    }
}

有androidmanifest.xml;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.xxxapp" >

    <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" />
    <!--
         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-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.INTERNET" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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="@string/API_KEY" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".DetailActivity"
            android:label="@string/title_activity_detail"
            android:screenOrientation="portrait" >
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.xxx.xxxapp.MainActivity" />
        </activity>
        <activity
            android:name=".ListVets"
            android:label="@string/title_activity_list_vets"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.xxx.xxxapp.MainActivity" />
        </activity>

    </application>

</manifest>
xjreopfe

xjreopfe1#

好 啊。我解决了这个问题。
我试图初始化setsvetmarkers()方法中markers和mymarker对象的hashmap,当我初始化hashmap时,上面的问题就解决了。
这是一种很好的方法;

// Initialize the HashMap for Markers and MyMarker object
    mMarkersHashMap = new HashMap<Marker, Markers>();

我重新创建并在顶部初始化;

public static HashMap<Marker, Markers> mMarkersHashMap = new HashMap<Marker, Markers>();

问题解决了。我不想删除这个问题,也许它对某人有帮助。但这是一个非常具体的问题。如果有管理员想删除这个问题,我没问题。

相关问题