2016-09-24 2 views
1

Google지도 API를 사용하는 응용 프로그램을 개발 중입니다. 내 응용 프로그램에서 Google지도를 표시하고 현재 위치에서 GPS에서 내 장치의 위도와 경도를 가져와 마커를 설정하려고합니다. 처음에는 잘 작동하는 코드를 구현할 때 장치에서 테스트했지만 모바일 장치에 15-20 번 설치 한 후에는 해당 작업에 아무 것도하지 않았지만 자동으로 충돌이 시작됩니다. 나에게 안드로이드 스튜디오를 보이고있다활동을 시작할 수 없습니다. ComponentInfo {Activity} : android.view.InflateException : 이진 XML 파일 줄 12 번 : 클래스 조각을 부 풀리는 중 오류가 발생했습니다.

로그는 다음과 같다

로그

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

NoActionBar 테마를 지정하여 매니페스트
<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 : null 배열의 길이를 얻으려고 시도했습니다 .' –

답변

1

편집 : 사용자는이 버그는 7.0 장치도 일어나고 있음을 지적했다, 그래서 당신은 appropiate SDK 버전에 android:maxSdkVersion="22" 변경 될 수 있다는 점에 유의하고있다 .

이 문제는 Resurrection Remix와 같은 사용자 지정 ROM이있는 5.1.1 장치에서만 발생합니다. 그 이유는 모르지만 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

제거 작업 표시 줄

+0

이 답변에 대해 자세히 설명해 주시겠습니까? –

+0

활동 테마 변경 android : theme = "@ style/AppTheme.NoActionBar" –

+0

물론 레이아웃을 어떻게 팽창시킬 수 있습니까? –

관련 문제