Android地图应用程序报告致命错误:主要

时间:2018-03-23 14:06:05

标签: java android google-maps android-studio

我正在编辑一个Android应用程序,该应用程序在谷歌地图中显示用户的当前位置。但是,当我尝试通过意图调用活动时,它崩溃并关闭并显示错误“不幸的是,应用程序已停止”。我不知道为什么一切似乎都很完美。但是旧的应用程序在没有任何错误的情况下工作,现在编辑和编译的应用程序正在崩溃。请帮我摆脱这个问题。

这是我的Button XML代码: -

 <Button
        android:id="@+id/button1"
        android:layout_width="180dp"
        android:layout_height="40dp"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="41dp"
        android:background="@drawable/hot"
        android:onClick="MyPlace"
        android:text="MyPlace" />

然后我的java代码调用Activity: -

public void MyPlace(View v){
    Intent ii= new Intent(Dashboard.this,Myplace.class);
    startActivity(ii);
}

Myplace.java

package com.example.arunj.neighbour.nearestneighbor;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.example.arunj.neighbour.R;
import com.example.arunj.neighbour.emergencybreakdown.DatabaseHelper;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Myplace extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
DatabaseHelper dbh;
String lat, lan;

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

    dbh = new DatabaseHelper(Myplace.this);
    //show error dialog if GoolglePlayServices not available
    if (!isGooglePlayServicesAvailable()) {
        finish();
    }

    SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
    googleMap = supportMapFragment.getMap();
    googleMap.setMyLocationEnabled(true);
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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 = locationManager.getLastKnownLocation(bestProvider);
    Toast.makeText(getApplicationContext(), (CharSequence) location,Toast.LENGTH_SHORT).show();
    if (location != null) {
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}

@Override
public void onLocationChanged(Location location) {
    TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    lat=String.valueOf(latitude);
    lan=String.valueOf(longitude);
    dbh.open();
    dbh.register(lat,lan);
    LatLng latLng = new LatLng(latitude, longitude);
    googleMap.addMarker(new MarkerOptions().position(latLng));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);

    Toast.makeText(getApplicationContext(),"latitude"+lat,Toast.LENGTH_SHORT).show();
    Toast.makeText(getApplicationContext(),"longitude"+lan,Toast.LENGTH_SHORT).show();
    Toast.makeText(getApplicationContext(),"Location Registration",Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}

private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
        return false;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.myplace, menu);
    return true;
   }
}

AndroidManifiest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.arunj.neighbour">
    <uses-permission android:name="com.example.emergencybreakdown.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature android:name="android.hardware.camera" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.arunj.neighbour.nearestneighbor.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.arunj.neighbour.nearestneighbor.Dashboard"
            android:label="@string/title_activity_dashboard" >
        </activity>
        <activity
            android:name="com.example.arunj.neighbour.nearestneighbor.Showmaps"
            android:label="@string/title_activity_showmaps" >
        </activity>
        <activity
            android:name="com.example.arunj.neighbour.nearestneighbor.Myplace"
            android:label="@string/title_activity_myplace" >
        </activity>

        <!--<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />-->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBMLBq4wORxPZf7y2CpOzxLJ3UpEOUbjbs" />

        <activity
            android:name="com.example.arunj.neighbour.nearestneighbor.Power"
            android:label="@string/title_activity_power" >
        </activity>
        <!--<activity
            android:name="com.example.arunj.neighbour.emergencybreakdown.Places"
            android:label="@string/title_activity_places" >
        </activity>-->
        <activity
            android:name="com.example.arunj.neighbour.emergencybreakdown.CurrentPlace"
            android:label="@string/title_activity_current_place" >
        </activity>
        <activity
            android:name="com.example.arunj.neighbour.emergencybreakdown.Admin"
            android:label="@string/title_activity_admin" >
        </activity>
        <activity
            android:name="com.example.arunj.neighbour.emergencybreakdown.AdminDash"
            android:label="@string/title_activity_admin_dash" >
        </activity>
    </application>
</manifest>

Logcat消息: -

03-23 19:10:17.302 28073-28073/com.example.arunj.neighbour E/AndroidRuntime: 

FATAL EXCEPTION: main
Process: com.example.arunj.neighbour, PID: 28073
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/R$styleable;
at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at android.support.v4.app.Fragment.onInflate(Fragment.java:1306)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3672)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:435)
at android.app.Activity.setContentView(Activity.java:2211)
at com.example.arunj.neighbour.nearestneighbor.Myplace.onCreate(Myplace.java:36)
at android.app.Activity.performCreate(Activity.java:6583)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.R$styleable" on path: DexPathList[[zip file "/data/app/com.example.arunj.neighbour-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.arunj.neighbour-2/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at android.support.v4.app.Fragment.onInflate(Fragment.java:1306)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3672)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:435)
at android.app.Activity.setContentView(Activity.java:2211)
at com.example.arunj.neighbour.nearestneighbor.Myplace.onCreate(Myplace.java:36)
at android.app.Activity.performCreate(Activity.java:6583)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.R$styleable
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 31 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

使用一些旧版Android Studio创建应用程序还有一件事,但现在我正在使用Android Studio 3.0进行编辑。

1 个答案:

答案 0 :(得分:0)

我通过在项目的build.gradle文件中添加这些依赖项来解决了这个问题。

dependencies {  
   compile 'com.google.android.gms:play-services-base:6.5.87'    
   compile 'com.google.android.gms:play-services-maps:6.5.87'  
}