Android GPS OnLocationChange崩溃

时间:2016-07-02 13:43:07

标签: java android gps android-gps

每当我尝试运行我的Android应用程序时,我都会继续收到此错误并崩溃。它应该检索当前的gps位置并更新数据活动上名为log的textview。

  

07-02 09:28:40.978 27682-27682 /? W / dalvikvm:threadid = 1:线程退出未捕获的异常(组= 0x416aeba8)   07-02 09:28:40.978 27682-27682 /? E / AndroidRuntime:致命异常:主要                                                      过程:com.novakinc.novak,PID:27682                                                      显示java.lang.NullPointerException                                                          at com.novakinc.novak.MainActivity $ 1.onLocationChanged(MainActivity.java:52)                                                          在android.location.LocationManager $ ListenerTransport._handleMessage(LocationManager.java:279)                                                          在android.location.LocationManager $ ListenerTransport.access $ 000(LocationManager.java:208)                                                          在android.location.LocationManager $ ListenerTransport $ 1.handleMessage(LocationManager.java:224)                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                          在android.os.Looper.loop(Looper.java:136)                                                          在android.app.ActivityThread.main(ActivityThread.java:5017)                                                          at java.lang.reflect.Method.invokeNative(Native Method)                                                          在java.lang.reflect.Method.invoke(Method.java:515)                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)                                                          在dalvik.system.NativeStart.main(本地方法)

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.novakinc.novak">

<uses-permission android:name="android.permission.ACCESS_COARSE_INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_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>
    <activity android:name=".help" />
    <activity android:name=".about" />
    <activity android:name=".data"></activity>
</application>

</manifest>

MainActivity.java:

package com.novakinc.novak;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.Manifest;
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.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private LocationManager locationManager;
private LocationListener listener;



@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);




    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            data.log.append("\n " + location.getLongitude() + " " +     location.getLatitude());
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
        }
    };

    if (ActivityCompat.checkSelfPermission(this,     Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED   && ActivityCompat.checkSelfPermission(this,   Manifest.permission.ACCESS_COARSE_LOCATION) !=   PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]  {Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
                    ,10);
        }
        return;
    }
    // this code won't execute IF permissions are not allowed, because in the line above there is return statement.


    locationManager.requestLocationUpdates("gps", 5000, 0, listener);


}


//---------------------------------------Menu---------------------------------------------------

public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.about:
            startActivity(new Intent(this, about.class));
            return true;
        case R.id.help:
            startActivity(new Intent(this, help.class));
            return true;
        case R.id.data:
            startActivity(new Intent(this, data.class));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

//---------------------------------------End Menu-----------------------------------------------

}

data.java:

package com.novakinc.novak;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


public class data extends AppCompatActivity {

    public static TextView log;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        log = (TextView) findViewById(R.id.textView);

    }
}

2 个答案:

答案 0 :(得分:0)

data.log是数据活动中的静态变量吗?你需要在向它添加内容之前为它指定一个引用

答案 1 :(得分:-1)

您正在获取Nullpointer异常,因为在LocationListener中,您从Location获取的值为null。始终检查(如果)空值。