2017-02-23 287 views
-4

我想在GPS位置上顯示google map帶標記,並在我的應用程序中顯示latitudelongitude。但是當我運行我的應用程序在logcat產生以下例外,我的應用程序崩潰,java.lang.RuntimeException:無法啓動活動ComponentInfo android

FATAL EXCEPTION: main 
Process: com.example.accurat.myapp, PID: 12208 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accurat.myapp/com.example.accurat.myapp.MainActivity}: android.view.InflateException: Binary XML file line #61: Binary XML file line #61: Error inflating class fragment 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2442) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5491) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
                     Caused by: android.view.InflateException: Binary XML file line #61: Binary XML file line #61: Error inflating class fragment 
at android.view.LayoutInflater.inflate(LayoutInflater.java:539) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374  
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.example.accurat.myapp.MainActivity.onCreate(MainActivity.java:112) 
at android.app.Activity.performCreate(Activity.java:6270) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5491)  
                      at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)  
                     Caused by: android.view.InflateException: Binary XML file line #61: Error inflating class fragment 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)  
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  
at com.example.accurat.myapp.MainActivity.onCreate(MainActivity.java:112)  
at android.app.Activity.performCreate(Activity.java:6270)  
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)  
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)  
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5491)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)  
Caused by: java.lang.RuntimeException: API key not found. Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml 
at com.google.maps.api.android.lib6.drd.r.b(:com.google.android.gms.DynamiteModulesB:115) 
at com.google.maps.api.android.lib6.auth.e.a(:com.google.android.gms.DynamiteModulesB:155) 
at com.google.android.gms.maps.internal.CreatorImpl.b(:com.google.android.gms.DynamiteModulesB:6074) 
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.DynamiteModulesB:72) 
at com.google.android.gms.maps.internal.i.onTransact(:com.google.android.gms.DynamiteModulesB:62) 
at android.os.Binder.transact(Binder.java:387) 
                      at uqw.newMapFragmentDelegate(:com.google.android.gms:179) 
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms:99) 
at uqv.onTransact(:com.google.android.gms:62) 
at android.os.Binder.transact(Binder.java:387) 
at com.google.android.gms.maps.internal.zzc$zza$zza.zzH(Unknown Source) 
at com.google.android.gms.maps.SupportMapFragment$zzb.zzIL(Unknown Source) 
at com.google.android.gms.maps.SupportMapFragment$zzb.zza(Unknown Source) 
at com.google.android.gms.dynamic.zza.zza(Unknown Source) 
at com.google.android.gms.dynamic.zza.onInflate(Unknown Source) 
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source) 
at android.support.v4.app.Fragment.onInflate(Fragment.java:1184) 
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3412) 
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120) 
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378) 

Manifest

<supports-screens 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:xlargeScreens="true" 
    /> 


<permission android:name="com.example.accurat.myapp.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature"/> 

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="com.example.accurat.myapp.permission.MAPS_RECEIVE"/> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.hardware.camera"/> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 


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

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

佈局

<Spinner 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/spinner" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:id="@+id/textView" 

    android:textAppearance="@style/TextAppearance.AppCompat.Display1" 
    android:layout_below="@+id/spinner" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:text="Get Coordinates" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="13dp" 
    android:id="@+id/button" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="14dp" 
    android:id="@+id/latitude" 
    android:text="34343" 
    android:layout_below="@+id/button" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginLeft="26dp" 
    android:layout_marginStart="26dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/longitude" 
    android:text="dsadasd" 
    android:layout_alignBaseline="@+id/latitude" 
    android:layout_alignBottom="@+id/latitude" 
    android:layout_toRightOf="@+id/button" 
    android:layout_toEndOf="@+id/button" 
    android:layout_marginLeft="26dp" 
    android:layout_marginStart="26dp" /> 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/map" 
    tools:context="com.example.app.MainActivity" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_below="@+id/longitude" 
    android:layout_above="@+id/btn_save" /> 

<Button 
    android:text="Save Data" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/btn_save" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"/

最後我MainActivity

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

    _latitude = (TextView)findViewById(R.id.latitude); 
    _longitude = (TextView)findViewById(R.id.longitude); 
    btn_get_coordinates = (Button)findViewById(R.id.button); 
    btn_save_data = (Button)findViewById(R.id.btn_save); 
    mapFrag = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 

btn_get_coordinates.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mapFrag.getMapAsync(MainActivity.this); 

      /* new TedPermission(MainActivity.this) 
        .setPermissionListener(permissionlistener) 
        .setRationaleMessage("This app needs Permission to find your location") 
        .setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION) 
        .check();*/ 

     } 
    }); 
} 


@Override 
public void onMapReady(GoogleMap googleMap) { 

    mGoogleMap=googleMap; 
    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

    //Initialize Google Play Services 
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      //Location Permission already granted 
      buildGoogleApiClient(); 
      mGoogleMap.setMyLocationEnabled(true); 
     } else { 
      //Request Location Permission 
      checkLocationPermission(); 
     } 
    } 
    else { 
     buildGoogleApiClient(); 
     mGoogleMap.setMyLocationEnabled(true); 
    } 

} 
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 
private void checkLocationPermission() { 


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 

      // Show an explanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 
      new android.app.AlertDialog.Builder(this) 
        .setTitle("Location Permission Needed") 
        .setMessage("This app needs the Location permission, please accept to use location functionality") 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          //Prompt the user once explanation has been shown 
          ActivityCompat.requestPermissions(MainActivity.this, 
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
            MY_PERMISSIONS_REQUEST_LOCATION); 
         } 
        }) 
        .create() 
        .show(); 


     } else { 
      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
    } 

} 
protected synchronized void buildGoogleApiClient() { 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 

    if (mGoogleApiClient != null) { 
     mGoogleApiClient.connect(); 
    } else 
     Toast.makeText(this, "Not Connected!", Toast.LENGTH_SHORT).show(); 

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

    /*super.onRequestPermissionsResult(requestCode, permissions, grantResults);*/ 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       // permission was granted, yay! Do the 
       // location-related task you need to do. 
       if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) 
       { 
        if(mGoogleApiClient == null) 
        { 
         buildGoogleApiClient(); 
        } 
        mGoogleMap.setMyLocationEnabled(true); 
       } 

      } else { 
       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
       //finish(); 
       Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show(); 
      } 
      return; 
     } 
     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

public void settingRequest() { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(10000); // 10 seconds, in milliseconds 
    mLocationRequest.setFastestInterval(1000); // 1 second, in milliseconds 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
      .addLocationRequest(mLocationRequest); 

    PendingResult<LocationSettingsResult> result = 
      LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 
        builder.build()); 

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 

     @Override 
     public void onResult(@NonNull LocationSettingsResult result) { 
      final Status status = result.getStatus(); 
      final LocationSettingsStates state = result.getLocationSettingsStates(); 
      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: 
        // All location settings are satisfied. The client can 
        // initialize location requests here. 
        getLocation(); 
        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        // Location settings are not satisfied, but this can be fixed 
        // by showing the user a dialog. 
        try { 
         // Show the dialog by calling startResolutionForResult(), 
         // and check the result in onActivityResult(). 
         status.startResolutionForResult(MainActivity.this, 1000); 
        } catch (IntentSender.SendIntentException e) { 
         // Ignore the error. 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        // Location settings are not satisfied. However, we have no way 
        // to fix the settings so we won't show the dialog. 
        break; 
      } 
     } 

    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    final LocationSettingsStates states = LocationSettingsStates.fromIntent(data); 
    switch (requestCode) { 
     case 1000: 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        // All required changes were successfully made 
        getLocation(); 
        break; 
       case Activity.RESULT_CANCELED: 
        // The user was asked to change settings, but chose not to 
        Toast.makeText(this, "Location Service not Enabled", Toast.LENGTH_SHORT).show(); 
        break; 
       default: 
        break; 
      } 
      break; 
    } 
} 


public void getLocation() { 

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } else { 
     /*Getting the location after aquiring location service*/ 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 

     if (mLastLocation != null) { 
      // _progressBar.setVisibility(View.INVISIBLE); 
      _latitude.setText("Latitude: " + String.valueOf(mLastLocation.getLatitude())); 
      _longitude.setText("Longitude: " + String.valueOf(mLastLocation.getLongitude())); 
      Latitude = String.valueOf(mLastLocation.getLatitude()); 
      Longitude = String.valueOf(mLastLocation.getLongitude()); 

      Calendar c = Calendar.getInstance(); 

      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
      DateTime = df.format(c.getTime()); 
     } else { 
      /*if there is no last known location. Which means the device has no data for the loction currently. 
      * So we will get the current location. 
      * For this we'll implement Location Listener and override onLocationChanged*/ 
      Log.i("Current Location", "No data for location found"); 

      if (!mGoogleApiClient.isConnected()) 
       mGoogleApiClient.connect(); 

      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, MainActivity.this); 
     } 
    } 
} 
@Override 
public void onLocationChanged(Location location) { 

    double lattitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 

    mLastLocation = location; 

    if (mCurrLocationMarker != null) { 
     mCurrLocationMarker.remove(); 
    } 
    //Place current location marker 
    LatLng latLng = new LatLng(lattitude, longitude); 
    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(latLng); 
    markerOptions.title("I am here"); 
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)); 
    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions); 
    // _progressBar.setVisibility(View.INVISIBLE); 
    _latitude.setText("Latitude: " + String.valueOf(mLastLocation.getLatitude())); 
    _longitude.setText("Longitude: " + String.valueOf(mLastLocation.getLongitude())); 

    //move map camera 
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 

    //stop location updates 
    if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 

} 

運行我的應用程序後,它讓我看到一個白色的屏幕,然後應用程序崩潰和上面提到的例外,顯示了logcat在點setContentView(R.layout.activity_main);

而且打在佈局看到我使用的是fragment和我用它,因爲我已經在努力。

我試圖找出問題,但無法做到這一點。 任何幫助將不勝感激。

+2

有你的API密鑰,如錯誤說,你需要添加'元data'標記爲你的'Android-Manifest.xml'中的API_KEY:'' – antonio

+0

哦,是的,我忘了:/ – faisal1208

+0

什麼是不明確的消息'引發:java.lang.RuntimeException:找不到API密鑰。檢查是否在AndroidManifest.xml'的元素中? –

回答

相關問題