Android LocationListener返回空值



Java主文件:

public class MainActivity extends Activity implements LocationListener {
    TextView tv;
    TextView tv2;
    TextView tv3;
    LocationListener locationListener;
    static final String tag="********MAIN********";
    LocationManager lm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    }
    @Override
    protected void onResume() {
        super.onResume();
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
    }
    @Override
    public void onLocationChanged(Location location) {
        Log.i(tag,"Location Changed Method Called");
        String lat= String.valueOf(location.getLatitude());
        String log= String.valueOf(location.getLongitude());
        tv.setText(lat+ " " + log);
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="p2p.boysjoys.com.alpha" >
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".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>
    </application>
</manifest>
错误日志:

05-21 18:51:53.970 22842-22842/p2p.boysjoys.com.alpha E/AndroidRuntime: FATAL EXCEPTION: mainjava.lang.RuntimeException:无法恢复活动{p2p.boysjoys.com.alpha/p2p.boysjoys.com.alpha.MainActivity}: java.lang.IllegalArgumentException: listener==nullandroid.app.ActivityThread.performResumeActivity (ActivityThread.java: 2596)android.app.ActivityThread.handleResumeActivity (ActivityThread.java: 2624)android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2101)android.app.ActivityThread.access 600美元(ActivityThread.java: 138)android.app.ActivityThread H.handleMessage美元(ActivityThread.java: 1207)android.os.Handler.dispatchMessage (Handler.java: 99)android.os.Looper.loop (Looper.java: 213)android.app.ActivityThread.main (ActivityThread.java: 4787)在java.lang.reflect.Method。invokeNative(本地方法)java.lang.reflect.Method.invoke (Method.java: 511)com.android.internal.os.ZygoteInit MethodAndArgsCaller.run美元(ZygoteInit.java: 789)com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 556)在dalvik.system.NativeStart。主要(本地方法)原因:java.lang.IllegalArgumentException: listener==nullandroid.location.LocationManager.requestLocationUpdates (LocationManager.java: 510)在p2p.boysjoys.com.alpha.MainActivity.onResume (MainActivity.java: 38)android.app.Instrumentation.callActivityOnResume (Instrumentation.java: 1184)android.app.Activity.performResume (Activity.java: 5082)android.app.ActivityThread.performResumeActivity (ActivityThread.java: 2586)android.app.ActivityThread.handleResumeActivity (ActivityThread.java: 2624)android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2101)android.app.ActivityThread.access 600美元(ActivityThread.java: 138)android.app.ActivityThread H.handleMessage美元(ActivityThread.java: 1207)android.os.Handler.dispatchMessage (Handler.java: 99)android.os.Looper.loop (Looper.java: 213)android.app.ActivityThread.main (ActivityThread.java: 4787)在java.lang.reflect.Method。invokeNative(本地方法)java.lang.reflect.Method.invoke (Method.java: 511)com.android.internal.os.ZygoteInit MethodAndArgsCaller.run美元(ZygoteInit.java: 789)com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 556)在dalvik.system.NativeStart。主要(本地方法)

我试图获得location 一旦应用程序打开,它将设置为TextView,但我得到line no.38上的错误,指出LogCat

那一行是:

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);

我在phoneemulator上尝试了gpsnetwork,但它仍然给出相同的错误java.lang.IllegalArgumentException: listener==null

变化:

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
与这个:

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this);
你的类已经实现了LocationListener。就用它吧。(但是,在您的代码中,您尝试使用locationListener而不初始化它)

代替:

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);

使用:

 lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this);

相关内容

  • 没有找到相关文章

最新更新