我想了解更多关于Java的知识。我对这件事一无所知。人们在说,如果我知道PHP,我就会认识Java。我不这么认为。 Java太多了,并且不允许任何例外。Android开发(Java) - 崩溃
我的应用程序在启动时立即崩溃。我已经为应用程序的权限添加了“FINE_LOCATION”和“COARSE_LOCATION”。
package either.no.me.myapplication2;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
TextView textLat;
TextView textLon;
Timer timer1 = new Timer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLat = (TextView) findViewById(R.id.textLat);
textLon = (TextView) findViewById(R.id.textLon);
GPSFinder gpsFinder = new GPSFinder();
gpsFinder.GetLocation();
}
}
class GPSFinder extends MainActivity {
public void GetLocation() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
textLat.setText(Double.toString(location.getLatitude()));
textLon.setText(Double.toString(location.getLongitude()));
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
// Register the listener with the Location Manager to receive location updates
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
我得到的logcat的错误:
07-31 01:36:45.611 1703-1703/either.no.me.myapplication2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: either.no.me.myapplication2, PID: 1703
java.lang.RuntimeException: Unable to start activity ComponentInfo{either.no.me.myapplication2/either.no.me.myapplication2.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4532)
at either.no.me.myapplication2.GPSFinder.GetLocation(MainActivity.java:37)
at either.no.me.myapplication2.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
07-31 01:36:48.875 1703-1703/either.no.me.myapplication2 I/Process: Sending signal. PID: 1703 SIG: 9
现在我删除了类,并使用了MainActivity中的代码,就像我可以像这样使用void:GetLocation();但现在的错误看起来是这样的:https://i.gyazo.com/35af174610f8c1e73f230c4c92207c0c.png –
我同意这种做法,他应该采取修复。但是ID说他的代码崩溃的原因是因为他扩展了一个Activity类,并且在其onCreate()之前调用了一个需要上下文的方法,因此被称为 –
因此,我该怎么做。我是Android开发人员的超级新手,我还没有弄明白。我可以在onCreate完成初始化后使用void来调用它吗?编辑:我把getLocation替换为getLocation,它开始工作,但我使用Nox作为模拟器进行调试,但它不包含“网络”,所以我得到这个:“java.lang.IllegalArgumentException:提供者不存在:网络“我必须解决它才能继续。谢谢你的朋友。 –