需要获取当前gps位置的应用每次都会崩溃

时间:2017-11-05 16:51:29

标签: android google-location-services

我正在关注this教程。我试过关注最终失败的youtube视频。 当我注释掉实现locationlistener,locationcallbacks以及为获取位置而运行的所有代码的代码时,该应用程序正常工作。

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tc.elicz.roadpal">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<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>

</manifest>

这是我的main_activity.java文件:

package tc.elicz.roadpal;
import android.Manifest;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.common.api.GoogleApi;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import org.xml.sax.helpers.LocatorImpl;
import java.util.Map;
import static com.google.android.gms.location.LocationServices.FusedLocationApi;

public abstract class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private Button button;
private TextView textView1;
private LocationManager locationManager;
private LocationListener locationListener;
private GoogleApiClient mapo = null;
private Location lastloc = null;
private String lat = null;
private String lon = null;
    private LocationRequest locreq= new LocationRequest();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("sosll");
    setContentView(R.layout.activity_main);
    textView1 = (TextView) findViewById(R.id.textView);

    if (mapo == null) {
        mapo = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
}

protected void onStart() {
    super.onStart();
    mapo.connect();

}

protected void onStop() {
    super.onStop();
    mapo.disconnect();

}

@Override
public void onConnected(Bundle connectionHint) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        textView1.append("No permission");
        System.out.println("No perm");
        return;
    }
    lastloc = LocationServices.FusedLocationApi.getLastLocation(mapo);
    if (lastloc != null) {
        lat=(String.valueOf(lastloc.getLatitude()));
        lon=(String.valueOf(lastloc.getLongitude()));
        textView1.append("Lat:" + lat +"lon:" +lon);
    }
}
}

我还根据教程对gradle文件进行了一些更改:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    defaultConfig {
        applicationId "tc.elicz.roadpal"
        minSdkVersion 22
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
})
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.google.android.gms:play-services:11.0.4'
}

这是我的日志文件:

11-05 23:00:41.825 16961-16961/? I/art: Late-enabling -Xcheck:jni
11-05 23:00:41.891 16961-16961/tc.elicz.roadpal W/System:ClassLoader 
referenced unknown path: /data/app/tc.elicz.roadpal-1/lib/arm
11-05 23:00:41.906 16961-16961/tc.elicz.roadpal I/InstantRun: Instant Run
Runtime started. Android package is tc.elicz.roadpal, real application class 
is null.
11-05 23:00:42.049 16961-16961/tc.elicz.roadpal W/System: ClassLoader 
referenced unknown path: /data/app/tc.elicz.roadpal-1/lib/arm
11-05 23:00:42.091 16961-16961/tc.elicz.roadpal I/FirebaseInitProvider:
FirebaseApp initialization unsuccessful
11-05 23:00:42.119 16961-16961/tc.elicz.roadpal D/AndroidRuntime: Shutting 
down VM
11-05 23:00:42.119 16961-16961/tc.elicz.roadpal E/AndroidRuntime: FATAL 
EXCEPTION: main
                                                              Process:     
tc.elicz.roadpal, PID: 16961

java.lang.RuntimeException: Unable to instantiate activity 
ComponentInfo{tc.elicz.roadpal/tc.elicz.roadpal.MainActivity}: 
java.lang.InstantiationException: 
java.lang.Class<tc.elicz.roadpal.MainActivity> cannot be instantiated
                                                                  at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2601)
                                                                  at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2766)
                                                                  at 
android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                  at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)
                                                                  at 
android.os.Handler.dispatchMessage(Handler.java:102)
                                                                  at 
android.os.Looper.loop(Looper.java:154)
                                                                  at 
android.app.ActivityThread.main(ActivityThread.java:6236)
at 
java.lang.reflect.Method.invoke(Native Method)           
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
(ZygoteInit.java:891)
                                                                  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
                                                               Caused by: 
java.lang.InstantiationException: 
java.lang.Class<tc.elicz.roadpal.MainActivity> cannot be instantiated
                                                                  at 
java.lang.Class.newInstance(Native Method)
                                                                  at 
android.app.Instrumentation.newActivity(Instrumentation.java:1079)
                                                                  at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2591)
                                                                  at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2766) 
                                                                  at 
android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                  at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507) 
                                                                  at 
android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                  at 
android.os.Looper.loop(Looper.java:154) 
                                                                  at 
android.app.ActivityThread.main(ActivityThread.java:6236) 
                                                                  at 
java.lang.reflect.Method.invoke(Native Method) 
                                                                  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
(ZygoteInit.java:891) 
                                                                  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) 

1 个答案:

答案 0 :(得分:0)

我不能说这会起作用,但过去它对我有用

我认为您需要在主活动类中添加无参数构造函数。

在主要活动中,

MainActivity(){
    super();
}

如果这样做有用,请尝试删除该行(仅作为最终手段进行测试):

private LocationRequest locreq= new LocationRequest();