2016-09-24 56 views
1

我正在開發一個使用google map API的應用程序。在我的應用程序中,我希望通過從GPS獲取設備經緯度來顯示Google地圖並在當前位置設置標記。在開始時,我實現了下面的代碼運行良好,我測試它在我的設備上,但在我的移動設備上安裝15或20次後,它會自動崩潰,雖然我沒有做任何事情。無法啓動活動ComponentInfo {Activity}:android.view.InflateException:二進制XML文件行#12:錯誤的擴展類片段

這是顯示我的Android工作室的日誌以下

日誌

09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: FATAL EXCEPTION: main 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: Process: com.example.nabeelfarooq.mapsnavapplication, PID: 9110 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nabeelfarooq.mapsnavapplication/com.example.nabeelfarooq.mapsnavapplication.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread.access$800(ActivityThread.java:162) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:106) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:189) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5529) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at com.example.nabeelfarooq.mapsnavapplication.MainActivity.onCreate(MainActivity.java:59) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.Activity.performCreate(Activity.java:5966) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread.access$800(ActivityThread.java:162)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:106)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:189)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5529)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)  
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to get length of null array 

MainActivity

import android.Manifest; 
import android.annotation.TargetApi; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.FragmentManager; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.util.Log; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.location.LocationSettingsRequest; 
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.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

import java.io.Console; 

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener, GoogleApiClient.OnConnectionFailedListener { 
    SupportMapFragment supportMapFragment = SupportMapFragment.newInstance(); 
    private GoogleMap mMap; 
    private GoogleApiClient mGoogleApiClient; 
    private Location mLastLocation; 
    public static double latitude; 
    public static double longitude; 
    private Button button; 
    private LatlngAdapter databasedata = null; 
    final int REQUEST_CODE_ASK_PERMISSIONS = 123; 

    @TargetApi(Build.VERSION_CODES.M) 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(getApplicationContext(), SearchActivity.class); 
       i.putExtra("latitude", latitude); 
       i.putExtra("longitude", longitude); 
       startActivity(i); 
      } 
     }); 

     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 
     if (status != ConnectionResult.SUCCESS) { 
      int requestCode = 10; 

      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
      dialog.show(); 
     } else { 
      SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
      mMap = fm.getMap(); 

      if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE_ASK_PERMISSIONS); 
       return; 

      } 
      else { 
       if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 
        mMap.setMyLocationEnabled(true); 
       } 
      } 

      mMap.setMyLocationEnabled(true); 
      mMap.setOnMyLocationChangeListener(this); 
     } 
     buildGoogleApiClient(); 
     onMapReady(mMap); 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     supportMapFragment.getMapAsync(this); 

    } 
    public void turnGPSOn() { 
     LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
     boolean enabled = service 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 
     if (!enabled) { 
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        this); 
      alertDialogBuilder 
        .setMessage("GPS is disabled in your device. Enable it?") 
        .setCancelable(false) 
        .setPositiveButton("yes", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
                int id) { 
/** Here it's leading to GPS setting options*/ 
            Intent callGPSSettingIntent = new Intent(
              android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
            startActivity(callGPSSettingIntent); 
           } 
          }); 
      alertDialogBuilder.setNegativeButton("No", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = alertDialogBuilder.create(); 
      alert.show(); 
     } 
    } 
    protected void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 
    protected void onPause(){ 
     super.onPause(); 
     mGoogleApiClient.disconnect(); 
    } 
    //obtain the supportMapFragment and get notified when the map is ready to be used 
    protected synchronized void buildGoogleApiClient(){ 

     if(mGoogleApiClient==null){ 
      mGoogleApiClient=new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); 
     } 
    } 


    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.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); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     FragmentManager fragmentManager=getFragmentManager(); 
     android.support.v4.app.FragmentManager fragmentManager1=getSupportFragmentManager(); 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if(supportMapFragment.isAdded()) { 
      fragmentManager1.beginTransaction().hide(supportMapFragment).commit(); 
     } 

     if (id == R.id.nav_home) { 
       //fragmentManager1.beginTransaction().add(R.id.map,supportMapFragment).commit(); 
      // Handle the home action 
     } else if (id == R.id.nav_about_us) { 
       Intent intent=new Intent(this,About_us_Activity.class); 
       startActivity(intent); 

     } else if (id == R.id.nav_contact_us){ 
      Intent intent=new Intent(this,Contact_us_Activity.class); 
      startActivity(intent); 

     } else if (id == R.id.nav_Rate_app) { 
      Intent intent=new Intent(this,TipsActivity.class); 
      startActivity(intent); 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap =googleMap; 
     LatLng lahore=new LatLng(latitude,longitude); 
     //mMap.addMarker(new MarkerOptions().title("lahore").snippet("lahore HQ").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position(sydney)); 
     //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 

    @Override 
    public void onMyLocationChange(Location location) { 
     latitude=location.getLatitude(); 
     longitude=location.getLongitude(); 
     LatLng latLng=new LatLng(latitude,longitude); 
     //mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     //mMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
     LatLng lahore=new LatLng(latitude,longitude); 
     //mMap.addMarker(new MarkerOptions().title("You are here").snippet("lahore").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position(lahore)); 
     // mMap.moveCamera(CameraUpdateFactory.newLatLng(lahore)); 
    } 
    public void onConnected(Bundle bundle){ 
     Log.d("CONNECTED", "I am connected"); 
     mLastLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if(mLastLocation!=null){ 
      Log.d("Latitude",String.valueOf(mLastLocation.getLatitude())); 
      Log.d("Longitude",String.valueOf(mLastLocation.getLongitude())); 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 
    static String latString =Double.toString(latitude); 
    static String lonString =Double.toString(longitude); 
} 

content_main.xml

在清單
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/app_bar_main" 
    tools:context=".MainActivity"> 


     <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/map" 
      android:layout_alignParentBottom="true" 
      tools:context=".MapsActivity" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      tools:layout="@layout/abc_action_bar_up_container"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Get Me Somewhere" 
     android:id="@+id/button" 
     android:background="#5368ed" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="31dp" 
     /> 

</RelativeLayout> 
+0

的可能的複製[錯誤充氣類片段](http://stackoverflow.com/questions/6424853/error-inflating-class-fragment) –

+0

檢查它在[此處](http://stackoverflow.com/questions/6424853/error-inflating-class-fragment) –

+0

你真正的錯誤...'NullPointerException:試圖獲得空數組的長度' –

回答

1

編輯:用戶已指出,這個錯誤也發生在7.0的設備,所以請記住,你可能會改變android:maxSdkVersion="22"到一個適當的SDK版本。

這個問題只發生在5.1.1帶有自定義ROM(如Resurrection Remix)的設備上。我不知道原因,但是你可以解決它,把它添加到你的清單中,如https://stackoverflow.com/a/39696819/4208583所示。

<uses-permission 
     android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
     android:maxSdkVersion="22" /> 
<uses-permission 
     android:name="android.permission.READ_EXTERNAL_STORAGE" 
     android:maxSdkVersion="22" /> 
+0

[Fabric](https://fabric.io/)[Crashlytics](https://fabric.io/kits/android/crashlytics)表示我的應用程序已經在Galaxy S5(OS:5.1.1)的用戶中崩潰了! –

0

刪除操作欄通過指定NoActionBar主題

+0

你能更詳細地解釋這個答案嗎? –

+0

改變你的活動主題 android:theme =「@ style/AppTheme.NoActionBar」 –

+0

當然,這是如何讓佈局膨脹的? –

相關問題