仅限物理设备 - 谷歌地图 - 空对象参考

时间:2017-03-02 05:37:08

标签: java android nullpointerexception

我正在运行我的代码并在移动设备上使用应用时遇到此错误,但在模拟器上它正在运行。

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.nirbhaym.Indoor, PID: 17543
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nirbhaym.Indoor/com.example.nirbhaym.indoor.MapsActivity}: android.view.InflateException: Binary XML file line #62: Error inflating class fragment
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
                  at android.app.ActivityThread.access$800(ActivityThread.java:151)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5268)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
               Caused by: android.view.InflateException: Binary XML file line #62: Error inflating class fragment
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                  at com.example.nirbhaym.indoor.MapsActivity.onCreate(MapsActivity.java:50)
                  at android.app.Activity.performCreate(Activity.java:6033)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5268) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.mkdir()' on a null object reference
                  at com.google.maps.api.android.lib6.gmm6.m.ad.a(Unknown Source)
                  at com.google.maps.api.android.lib6.gmm6.c.h.a(Unknown Source)
                  at com.google.maps.api.android.lib6.gmm6.c.y.a(Unknown Source)
                  at com.google.maps.api.android.lib6.e.bd.a(Unknown Source)
                  at com.google.maps.api.android.lib6.e.ev.a(Unknown Source)
                  at com.google.maps.api.android.lib6.e.z.a(Unknown Source)
                  at com.google.maps.api.android.lib6.e.y.a(Unknown Source)
                  at com.google.android.gms.maps.internal.u.onTransact(SourceFile:107)
                  at android.os.Binder.transact(Binder.java:380)
                  at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onCreateView(Unknown Source)
                  at com.google.android.gms.maps.SupportMapFragment$zza.onCreateView(Unknown Source)
                  at com.google.android.gms.dynamic.zza$4.zzb(Unknown Source)
                  at com.google.android.gms.dynamic.zza.zza(Unknown Source)
                  at com.google.android.gms.dynamic.zza.onCreateView(Unknown Source)
                  at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1226)
                  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1328)
                  at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2284)
                  at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
                  at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
                  at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                  at com.example.nirbhaym.indoor.MapsActivity.onCreate(MapsActivity.java:50) 
                  at android.app.Activity.performCreate(Activity.java:6033) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5268) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 

I /处理:发送信号。 PID:17543 SIG:9 申请已终止。

主要活动代码

package com.example.nirbhaym.indoor;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlay;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import java.util.ArrayList;
import java.util.List;

/ **  *这显示了如何向地图添加地面叠加层。  * / 公共类MapsActivity扩展了AppCompatActivity         实现OnSeekBarChangeListener,OnMapReadyCallback,         GoogleMap.OnGroundOverlayClickListener {

private static final int TRANSPARENCY_MAX = 100;

private static final LatLng NEWARK = new LatLng(28.544594, 77.272486);

private static final LatLng NEAR_NEWARK =
        new LatLng(NEWARK.latitude - 0.001, NEWARK.longitude - 0.025);

private final List<BitmapDescriptor> mImages = new ArrayList<BitmapDescriptor>();

private GroundOverlay mGroundOverlay;

private GroundOverlay mGroundOverlayRotated;

private SeekBar mTransparencyBar;

private int mCurrentEntry = 0;

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

    mTransparencyBar = (SeekBar) findViewById(R.id.transparencySeekBar);
    mTransparencyBar.setMax(TRANSPARENCY_MAX);
    mTransparencyBar.setProgress(0);

    SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
    // Register a listener to respond to clicks on GroundOverlays.
    map.setOnGroundOverlayClickListener(this);

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11));

    mImages.clear();
    mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.fourth_floor_app));
    //mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_prudential_sunny));

    // Add a small, rotated overlay that is clickable by default
    // (set by the initial state of the checkbox.)
    mGroundOverlayRotated = map.addGroundOverlay(new GroundOverlayOptions()
            .image(mImages.get(0)).anchor(0, 1)
            .position(NEAR_NEWARK, 4300f, 3025f)
            .bearing(30)
            .clickable(((CheckBox) findViewById(R.id.toggleClickability)).isChecked()));

    // Add a large overlay at Newark on top of the smaller overlay.
    mGroundOverlay = map.addGroundOverlay(new GroundOverlayOptions()
            .image(mImages.get(mCurrentEntry)).anchor(0, 1)
            .position(NEWARK, 8600f, 6500f));

    mTransparencyBar.setOnSeekBarChangeListener(this);

    // Override the default content description on the view, for accessibility mode.
    // Ideally this string would be localised.
    map.setContentDescription("Google Map with ground overlay.");
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (mGroundOverlay != null) {
        mGroundOverlay.setTransparency((float) progress / (float) TRANSPARENCY_MAX);
    }
}

public void switchImage(View view) {
    mCurrentEntry = (mCurrentEntry + 1) % mImages.size();
    mGroundOverlay.setImage(mImages.get(mCurrentEntry));
}

/**
 * Toggles the visibility between 100% and 50% when a {@link GroundOverlay} is clicked.
 */
@Override
public void onGroundOverlayClick(GroundOverlay groundOverlay) {
    // Toggle transparency value between 0.0f and 0.5f. Initial default value is 0.0f.
    mGroundOverlayRotated.setTransparency(0.5f - mGroundOverlayRotated.getTransparency());
}

/**
 * Toggles the clickability of the smaller, rotated overlay based on the state of the View that
 * triggered this call.
 * This callback is defined on the CheckBox in the layout for this Activity.
 */
public void toggleClickability(View view) {
    if (mGroundOverlayRotated != null) {
        mGroundOverlayRotated.setClickable(((CheckBox) view).isChecked());
    }
}

}

布局文件

 <?xml version="1.0" encoding="utf-8"?><!--

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/fourth_floor_app"
    android:padding="5dp">

    <TextView
        android:id="@+id/transparency_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="string/transparency" />

    <SeekBar
        android:id="@+id/transparencySeekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/transparency_text"
        android:layout_toRightOf="@+id/transparency_text" />

    <Button
        android:id="@+id/switchImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/transparencySeekBar"
        android:onClick="switchImage"
        android:text="string/switch_image" />

    <CheckBox
        android:id="@+id/toggleClickability"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/switchImage"
        android:layout_toEndOf="@+id/switchImage"
        android:layout_toRightOf="@+id/switchImage"
        android:checked="true"
        android:onClick="toggleClickability"
        android:text="string/clickable" />

</RelativeLayout>

<fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

 FragmentManager fmanager = getSupportFragmentManager();
        Fragment fragment = fmanager.findFragmentById(R.id.map);
        SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
        GoogleMap supportMap = supportmapfragment.getMap();

试试这段代码。为此,本教程是here希望这对您有所帮助。问题只在片段中。您还需要在上面的教程中检查权限。希望它能帮到你

http://www.truiton.com/2013/05/android-supportmapfragment-example/

编辑:

检查外部存储的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />