2017-03-18 84 views
0

我試圖從主要活動打開GPS活動檢索位置,但是當我按一下按鈕,並說無法打開活動找不到類「android.app.AppOpsManager」

更新它崩潰::: 我改變了代碼,看看爲什麼gps現在返回null現在我只想定向到gps活動,但它不會嘗試啓用multidex並將其安裝在應用程序類中,但它沒有解決

PS:我使用的SDK分鐘17 和目標SDK 25 和GPS類工作正常單獨

主要業務:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button two = (Button) findViewById(R.id.button); 
     Recieve = (TextView) findViewById(R.id.textView2); 
     two.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       sendMessage(); 
      } 

     }); 
    } 

    public void sendMessage() { 
     Intent intent = new Intent(this, Activity2.class); 
     startActivity(intent); 

     // startActivityForResult(intent, REQUEST_CODE); 
    } 

    /* @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { 
      Response = data.getStringExtra("key"); 

      Recieve.setText("latitude is :" + Response); 

     } 
    }*/ 

} 

GPS活動:

public void onConnected(Bundle connectionHint) { 
     createLocationRequest(); 
     startLocationUpdates(); 
     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; 
     } 
     Location Location = LocationServices.FusedLocationApi.getLastLocation(GAC); 
     if (Location != null) { 
      Double Lat = Location.getLatitude(); 
      Double lon = Location.getLongitude(); 
      x = (TextView) findViewById(R.id.textView); 
      y = (TextView) findViewById(R.id.textView2); 
      x.setText("Ltitude is " + String.valueOf(Lat)); 
      y.setText("Longitude is " + String.valueOf(lon)); 
     } 

    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     if (GAC != null) { 
      GAC.connect(); 
     } 
    } 

    @Override 

    protected void onStop() { 
     GAC.disconnect(); 
     super.onStop(); 
    } 

    @Override 
    public void onConnectionSuspended(int cause) { 
     Toast.makeText(this, "the connection suspended", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 

     Toast.makeText(this, "the connection failed", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     x = (TextView) findViewById(R.id.textView); 
     y = (TextView) findViewById(R.id.textView2); 
     x.setText("latitude is " + String.valueOf(location.getLatitude())); 
     y.setText("longitude is " + String.valueOf(location.getLongitude())); 
    } 

    protected void createLocationRequest() { 
     LR = new LocationRequest(); 
     LR.setInterval(5000); 
     LR.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    } 

    protected void startLocationUpdates() { 

     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; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(
       GAC, LR, (this)); 

    } 



    protected void build_GAC(){ 
     GAC =new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 

    } 

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

    } 

} 

這是錯誤我得到:

E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.internal.zzacw.zzg 

回答

0

有很多方法來解決這個在本post

說明

1.如果你不使用應用程序類,你可以d在您的AndroidManifest.xml中將這個類作爲應用程序使用。

public class MyApplication extends MultiDexApplication { 
    ... 
} 

3.Have您的應用覆蓋attachBaseContext:

public class MyApplication extends Application { 
    @Override 
    protected void attachBaseContext(Context base) { 
    super.attachBaseContext(base); 
    MultiDex.install(this);} 
} 

您也可以嘗試只選擇所需要的谷歌

<application android:name="android.support.multidex.MultiDexApplication"> 

您的應用程序擴展MultiDexApplication類,像2.Define播放服務API來解決您的問題或禁用Instant Run

gradle這個之前: -

compile 'com.google.android.gms:play-services:8.4.0' 

gradle這個後: -

compile 'com.google.android.gms:play-services-analytics:8.4.0' 
compile 'com.google.android.gms:play-services-gcm:8.4.0' 
compile 'com.google.android.gms:play-services-location:8.4 
相關問題