大家好,我尝试在这里读取尽可能多的 GPS 数据。响应。我想确定位置。我现在有了几乎所有的建议,但这里没有任何内容。getLastKnownLocation() 总是返回 null。应用程序崩溃。有人可以帮我吗?这是我的代码。
我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.xxx.xxxbrokenstream"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="de.xxx.xxxbrokenstream.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>
主要活动
package de.xxx.xxxbrokenstream;
import de.xxx.xxxbrokenstream.R;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContextWrapper;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public abstract class MainActivity extends Activity implements LocationListener {
ContextWrapper context;
double longitude;
double latitude;
private LocationManager locationManager;
private boolean isGps, isNetwork;
private double currentLat, currentLong;
private Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setDefaultButton();
locationManager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
isGps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetwork = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, this);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 0,
this);
getCurrentLocation();
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
};
public void setDefaultButton() {
Button buttonstream1 = (Button) findViewById(R.id.btn_stream1);
buttonstream1 .setVisibility(View.VISIBLE);
buttonstream1 .setOnClickListener(stream1Listener);
Button buttonstream2 = (Button) findViewById(R.id.btn_stream2);
buttonstream2 .setVisibility(View.VISIBLE);
buttonstream2 .setOnClickListener(stream2Listener);
Button buttonstream3 = (Button) findViewById(R.id.btn_stream3);
buttonstream3 .setVisibility(View.VISIBLE);
buttonstream3 .setOnClickListener(stream3Listener);
};
// Listener
LocationListener locationListener = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
public void onLocationChanged(Location location) {
try {
if (location != null) {
currentLat = location.getLatitude();
currentLong = location.getLongitude();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
private OnClickListener stream1Listener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
private OnClickListener stream2Listener = new OnClickListener() {
public void onClick(View v) {
}
};
private OnClickListener stream3Listener = new OnClickListener() {
public void onClick(View v) {
}
};
private void destroyAll() {
if (locationManager != null)
locationManager.removeUpdates(this);
}
@Override
public void onDestroy() {
super.onDestroy();
destroyAll();
}
/**
* Get the current location..
*/
public void getCurrentLocation() {
if (isNetwork) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} else if (isGps) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (location != null) {
currentLong = location.getLongitude();
currentLat = location.getLatitude();
} else {
currentLong = 0.0;
currentLat = 0.0;
}
}
public double getCurrentLat() {
return currentLat;
}
public void setCurrentLat(double currentLat) {
this.currentLat = currentLat;
}
public double getCurrentLong() {
return currentLong;
}
public void setCurrentLong(double currentLong) {
this.currentLong = currentLong;
}
}
编辑:
你好谢谢。我已经更改了我的代码。不幸的是,我得到了相同的结果。应用程序崩溃了。这是来自 eclipse 日志的日志:
致命异常:主要 java.lang.RuntimeException:无法实例化活动 ComponentInfo{de.xxx.xxxbrokenstream/de.xxx.xxxbrokenstream.MainActivity}:java.lang.InstantiationException:无法实例化类 de.xxx.xxxbrokenstream.MainActivity android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 在 android.app。 ActivityThread$H.handleMessage(ActivityThread.java:1234) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main (ActivityThread.java:5041) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 在 dalvik.system.NativeStart.main (Native Method) 原因:java.lang.InstantiationException: can't instantiate class de.xxx.xxxbrokenstream.MainActivity at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java: 1319) 在 android.app.Instrumentation.newActivity(Instrumentation.java:1054) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)xxx.xxxbrokenstream.MainActivity 在 java.lang.Class.newInstanceImpl(Native Method) 在 java.lang.Class.newInstance(Class.java:1319) 在 android.app.Instrumentation.newActivity(Instrumentation.java:1054) 在 android。 app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)xxx.xxxbrokenstream.MainActivity 在 java.lang.Class.newInstanceImpl(Native Method) 在 java.lang.Class.newInstance(Class.java:1319) 在 android.app.Instrumentation.newActivity(Instrumentation.java:1054) 在 android。 app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)