java.lang.NullPointerException:未初始化IBitmapDescriptorFactory

时间:2016-05-17 10:14:31

标签: android

我在任何预棒棒糖设备上运行该应用时出现此错误,除非该应用在> = 5.0版本中完美运行。

这就是我得到的:

                                                                                   Process: com.hashrail.newyorkcityguide, PID: 31045
                                                                                   java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
                                                                                       at com.google.android.gms.common.internal.zzx.zzb(Unknown Source)
                                                                                       at com.google.android.gms.maps.model.BitmapDescriptorFactory.zzAi(Unknown Source)
                                                                                       at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromBitmap(Unknown Source)
                                                                                       at com.hashrail.newyorkcityguide.fragment.FragmentNearby.onActivityCreated(FragmentNearby.java:160)
                                                                                       at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1983)
                                                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
                                                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                                       at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                                       at com.hashrail.newyorkcityguide.MainActivity.showScreen(MainActivity.java:335)
                                                                                       at com.hashrail.newyorkcityguide.MainActivity.showNearbyFragment(MainActivity.java:249)
                                                                                       at com.hashrail.newyorkcityguide.fragment.FragmentListPlaces$OnMapClickListener$1.run(FragmentListPlaces.java:243)
                                                                                       at android.os.Handler.handleCallback(Handler.java:808)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                                       at android.os.Looper.loop(Looper.java:193)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                                       at dalvik.system.NativeStart.main(Native Method)
    05-18 11:31:27.932 31045-31145/com.hashrail.newyorkcityguide E/GAv4: Successfully bound to service but never got onServiceConnected callback

MainActivity

    public class MainActivity extends FragmentActivity{

        public static float density;
        private static final int PERMISSION_REQUEST_CODE_LOCATION = 1;
        public static Context context;

        private GPSTracker gps;
        private double latitudeGet, longitudeGet;

        public static String getApiKey() {
            return context.getString(R.string.google_api_key);
        }

        private String cityGet, countryGet;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            MapsInitializer.initialize(getApplicationContext());
            try {
                context = this;
                ThemeUtil.setTranslucentTheme(this);

                density = getResources().getDisplayMetrics().density;

                setContentView(R.layout.activity_main);

               /* if (android.os.Build.VERSION.SDK_INT > 9 || android.os.Build.VERSION.SDK_INT < 21 ) {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                            .permitAll().build();
                    StrictMode.setThreadPolicy(policy);
                }*/

                DatabaseManager.initWithContext(this);
                ImageLoader.getInstance().init(
                        ImageOptionsBuilder.createImageLoaderConfiguration(this));

                Constants.DEFAULT_PHOTO_WIDTH = getResources().getDisplayMetrics().widthPixels;

                showHomeFragment();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        NearbyPlacesManager.getInstance();
                    }
                }, 1000);

                new AboutDataLoader().loadData(this);
                if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,getApplicationContext(),MainActivity.this)) {
                    getCurrentArea();
                }
                else
                {
                    requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,PERMISSION_REQUEST_CODE_LOCATION,getApplicationContext(),MainActivity.this);
                }
                // initAdmob();

                initAppoDeal();
               /* if (NetConnectivity.isOnline(getApplicationContext())) {
                } else {
                }*/
                //Fabric.with(this, new Crashlytics());


            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private void initAppoDeal() {

            String appKey = "703f62f01e41eecc435236577c2e5500d1f291d2acd9fab3";
            Appodeal.initialize(this, appKey, Appodeal.BANNER);
            Appodeal.show(this, Appodeal.BANNER);

            /*
            Appodeal.setNativeCallbacks(new NativeCallbacks() {
                @Override
                public void onNativeLoaded(List<NativeAd> nativeAds) {
                    Log.d("Appodeal", "onNativeLoaded");
                }
                @Override
                public void onNativeFailedToLoad() {
                    Log.d("Appodeal", "onNativeFailedToLoad");
                }

                @Override
                public void onNativeShown(NativeAd nativeAd) {
                    Log.d("Appodeal", "onNativeShown");
                }

                @Override
                public void onNativeClicked(NativeAd nativeAd) {
                    Log.d("Appodeal", "onNativeClicked");
                    Toast.makeText(MainActivity.this, "Your Clicked", Toast.LENGTH_LONG).show();
                }
            });*/


        }
        public static void requestPermission(String strPermission,int perCode,Context _c,Activity _a){

            if (ActivityCompat.shouldShowRequestPermissionRationale(_a,strPermission)){
                //Toast.makeText(getApplicationContext(),"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
            } else {

                ActivityCompat.requestPermissions(_a,new String[]{strPermission},perCode);
            }
        }

        public static boolean checkPermission(String strPermission,Context _c,Activity _a){
            int result = ContextCompat.checkSelfPermission(_c, strPermission);
            if (result == PackageManager.PERMISSION_GRANTED){

                return true;

            } else {

                return false;

            }
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
            switch (requestCode) {

                case PERMISSION_REQUEST_CODE_LOCATION:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                        getCurrentArea();

                    } else {

                        Toast.makeText(getApplicationContext(),"Permission Denied, You cannot access location data.",Toast.LENGTH_LONG).show();

                    }
                    break;

            }
        }
        private void getCurrentArea() {

            gps = new GPSTracker(MainActivity.this);

            // check if GPS enabled
            if (gps.canGetLocation()) {

                latitudeGet = gps.getLatitude();
                longitudeGet = gps.getLongitude();

                Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(latitudeGet, longitudeGet, 1);
                    //List<Address> addresses = geoCoder.getFromLocation(17.4126274,78.2679594, 1);


                    if (addresses.size() > 0) {
                        System.out.println(addresses.get(0).getLocality());
                        System.out.println(addresses.get(0).getCountryName());
                        cityGet = addresses.get(0).getLocality();
                        countryGet = addresses.get(0).getCountryName();
                        AppSettings.LATITUDE = latitudeGet;
                        AppSettings.LONGITUDE = longitudeGet;
                        // AppSettings.LATITUDE = 17.4126274;
                        // AppSettings.LONGITUDE= 78.2679594;
                        AppSettings.TOWN = cityGet;
                        AppSettings.COUNTRY = countryGet;

                    }

                    //showToastMessage(add);
                    // Toast.makeText(MainActivity.this, "Your Location is -  " + cityGet + " " + countryGet, Toast.LENGTH_LONG).show();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                // \n is for new line
                // Toast.makeText(MainActivity.this, "Your Location is - \nLat: " + latitudeGet + "\nLong: " + longitudeGet, Toast.LENGTH_LONG).show();
            } else {
                // can't get location
                // GPS or Network is not enabled
                // Ask user to enable GPS/network in settings
                gps.showSettingsAlert();
            }
        }

        public void showNearbyFragment(Constants.PLACE_TYPES type, Place placeDetail, boolean showCurrentLocation) {
            showScreen(FragmentNearby.newInstance(type, placeDetail, showCurrentLocation), FragmentNearby.TAG, true, false);
        }

        public boolean isNearbyFragmentShowed() {
            return getSupportFragmentManager().findFragmentByTag(FragmentNearby.TAG) != null;
        }

        public void showListFragment(Constants.PLACE_TYPES type) {
            showScreen(FragmentListPlaces.newInstance(type), FragmentListPlaces.TAG, true, false);
        }

        private void showHomeFragment() {
            showScreen(FragmentHome.newInstance(), FragmentHome.TAG, false, false);
        }

        public void showForecastFragment() {

            showScreen(FragmentForecast.newInstance(), FragmentForecast.TAG, true, false);
        }

        public void showAboutFragment() {

            showScreen(FragmentAbout.newInstance(), FragmentAbout.TAG, true, false);
        }

        public void showLoadingDialog() {
            DialogFragmentLoading.newInstance().show(getSupportFragmentManager(), DialogFragmentLoading.TAG);
        }

        public void showPhotoDialog(Photo photo) {
            DialogFragmentPhoto.newInstance(photo).show(getSupportFragmentManager(), DialogFragmentPhoto.TAG);
        }


        public void showPlaceDetailFragment(Place place, Constants.PLACE_TYPES type) {
            FragmentPlaceDetail fragmentPlaceDetail = FragmentPlaceDetail.newInstance(place, type);

            showScreen(fragmentPlaceDetail, FragmentPlaceDetail.TAG, true, false);
        }

        public void showReviewsScreen(PlaceDetails placeDetails, Constants.PLACE_TYPES type) {
            FragmentReviews fragmentReviews = FragmentReviews.newInstance(placeDetails, type);

            showScreen(fragmentReviews, FragmentReviews.TAG, true, false);
        }


        public void hideLoadingDialog() {

            Fragment loadingFragment = getSupportFragmentManager().findFragmentByTag(DialogFragmentLoading.TAG);

            if (loadingFragment != null) {
                ((DialogFragmentLoading) loadingFragment).dismiss();
            }
        }


        private void showScreen(Fragment content,
                                String contentTag, boolean addToBackStack, boolean clearBackStack) {

            if (!NetworkFetcher.isNetworkConnected(MainActivity.this) && !contentTag.equalsIgnoreCase(FragmentHome.TAG)) {
                Toast.makeText(this, getString(R.string.no_internet_connection), Toast.LENGTH_LONG).show();

                return;
            }

            FragmentManager fm = getSupportFragmentManager();

            FragmentTransaction ft = fm.beginTransaction();
            ft.setCustomAnimations(R.anim.left_slide_in, R.anim.left_slide_out,
                    R.anim.right_slide_in, R.anim.right_slide_out);


            ft.replace(R.id.activity_main_content, content, contentTag);


            if (clearBackStack) {
                fm.popBackStackImmediate(null,
                        FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }

            if (addToBackStack) {
                ft.addToBackStack(String.valueOf(System.identityHashCode(content)));
            }

            ft.commitAllowingStateLoss();
            fm.executePendingTransactions();
        }


    }

0 个答案:

没有答案