2015-09-20 11 views
7

Ich bin neu in dieser Community, aber ich habe schon viel von Ihnen gelernt! Eigentlich habe ich angefangen, auf Android Studio zu programmieren, aber jetzt steckte ich irgendwo fest. Ich bin sicher, es ist etwas Einfaches, das du schnell herausfinden wirst.LocationManager: java.lang.NoSuchMethodError

In der Emulator Nexus 5 API 23 (die bereits in Android Studio installiert) funktioniert es, aber auf meinem Handy Android 5.1.1 API 22 es nicht starten.

Fehler: 09-20 19:15:11.526 17776-17776/com.example.stefano.aiutomamma01 E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.stefano.aiutomamma01, PID: 17776 java.lang.NoSuchMethodError: No virtual method checkSelfPermission(Ljava/lang/String;)I in class Lcom/example/stefano/aiutomamma01/MainActivity; or its super classes (declaration of 'com.example.stefano.aiutomamma01.MainActivity' appears in /data/app/com.example.stefano.aiutomamma01-2/base.apk) at com.example.stefano.aiutomamma01.MainActivity.onCreate(MainActivity.java:28) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) at android.app.ActivityThread.access$800(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5373) 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:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

JAVA-Code:

package com.example.stefano.aiutomamma01; 

import android.Manifest; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity implements LocationListener { 

    //GPS 
    private LocationManager locationManager; 

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

     //GPS 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     //CATCHED 
     if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
      // 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 Activity#requestPermissions for more details. 
      return; 
     } 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

    } 

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


    //GPS 
    @Override 
    public void onLocationChanged(Location location) { 

     String str = "Latitude: " + location.getLatitude() + "Longitude: " + location.getLongitude(); 

     Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show(); 

    } 

    //GPS 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    //GPS 
    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    //GPS 
    @Override 
    public void onProviderDisabled(String provider) { 

    } 

} 

Sorry für die schlechte Post. Btw: ich könnte nicht Lösung im Web finden ...

Grüße

+0

Laufzeitberechtigungen wurden in API 23 genauso wie die Methoden eingeführt, die für ihre Handhabung verwendet werden. Verwenden Sie die Klassenmethoden ['ActivityCompat'] (http://developer.android.com/reference/android/support/v4/app/ActivityCompat.html), um dies rückwärtskompatibel zu behandeln. –

Antwort

18

Runtime-Berechtigungen in API 23 gleichen wie die Methoden eingeführt wurden, die für ihre Behandlung verwendet werden. Verwenden Sie die Klassenmethoden ActivityCompat, um dies rückwärtskompatibel zu handhaben.

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
    && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
    ... 
} 

Und so weiter.

+0

Danke! Jetzt geht es. Aber die Anwendung überprüft nicht den Standort wie in Google Maps. Weißt du, warum? –

+0

Haben Sie GPS eingeschaltet? Bist du draußen? Der Code scheint in Ordnung zu sein. –

+0

Ich bin drin. Aber mit Google Maps funktioniert es. Ich denke, es wird nicht nach dem Ort gesucht. Ich werde es draußen versuchen. –

Verwandte Themen