找不到类'android.app.AppOpsManager'

时间:2017-03-18 15:06:02

标签: android google-play-services

我正在尝试从主要活动中打开Gps活动来检索位置,但是当我点击按钮并说无法打开活动时它会崩溃

更新::: 我改变了代码只是为了看看为什么gps现在返回null我只是想被定向到gps活动但是我不会尝试启用multidex并将其安装在应用程序类中但是它没有用完

P.S:我正在使用min 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

1 个答案:

答案 0 :(得分:0)

有很多方法可以解决这个问题,如post

所述

1.如果您不使用Application Class,则可以将此类声明为AndroidManifest.xml中的应用程序。

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

2.定义您的Application扩展MultiDexApplication类,如:

public class MyApplication extends MultiDexApplication {
    ...
}

3.Have your Application覆盖attachBaseContext:

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

您还可以尝试仅选择所需的Google Play服务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