こんにちは、私はここでできるだけ多くの 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ログからのログです:
致命的な例外: main 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(ネイティブ メソッド) で 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 で(ネイティブ メソッド) 原因: java.lang.InstantiationException: 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)