帮助程序类 - 错误无法启动活动 ComponentInfo:
错误无法启动活动 ComponentInfo: java.lang.IllegalStateException: onCreate() 之前的活动无法使用系统服务
我正在尝试分离代码和使用辅助类。 (创建不同的Java文件) 我所做的是创建一个在清单中注册的 Activity Java 文件,但我没有注册以下类(Java 文件):
import android.app.Activity;
import android.location.LocationManager;
import android.net.ConnectivityManager;
....
public class DeviceMonitor extends Activity {
boolean laag=false;
int level=-1;
double batterylevel=-1;
public boolean GPSEnabled() {
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { // NO GPS ENABLED
//ErrorMessage.append(R.string.enablegps);
//ErrorMessage.append("\n");
Log.d("StartPrepare","GPS DISABLED");
return false;
} else {
Log.d("StartPrepare","GPS ENABLED");
return true;
}
}
我删除了 OnCreate() 方法,这是正确的吗? 我应该在清单中注册吗?如果是,如何注册?
我在从注册的活动调用时收到以下错误,如下所示:
DeviceMonitor MyDevice = new DeviceMonitor();
if (MyDevice.GPSEnabled()){gpsenabled=true;}else{gpsenabled=false;fout=true;}
错误:
E/AndroidRuntime(1912): java.lang.RuntimeException: 无法启动 活动 组件信息{包}: java.lang.IllegalStateException:系统服务不可用 onCreate() 之前的活动
有人可以给我一些关于帮助器类的信息(我是 Java/Android 的新手),并且知道错误可能导致的原因吗?我尝试添加 OnCreate() 方法,但没有帮助。
多谢!
Error Unable to start activity ComponentInfo: java.lang.IllegalStateException: System services not available to Activities before onCreate()
I´m experimenting with seperating the code and the use of a helper class. (Created different Java files)
What I did is created an Activity Java file that is registred in the Manifest and I didn´t register this following class (Java file):
import android.app.Activity;
import android.location.LocationManager;
import android.net.ConnectivityManager;
....
public class DeviceMonitor extends Activity {
boolean laag=false;
int level=-1;
double batterylevel=-1;
public boolean GPSEnabled() {
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { // NO GPS ENABLED
//ErrorMessage.append(R.string.enablegps);
//ErrorMessage.append("\n");
Log.d("StartPrepare","GPS DISABLED");
return false;
} else {
Log.d("StartPrepare","GPS ENABLED");
return true;
}
}
I removed the OnCreate() method, is this correct?
Should I have registred in the Manifest, if yes, how?
I received the following error while calling from the registred Activity like this:
DeviceMonitor MyDevice = new DeviceMonitor();
if (MyDevice.GPSEnabled()){gpsenabled=true;}else{gpsenabled=false;fout=true;}
Error:
E/AndroidRuntime(1912): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{package}:
java.lang.IllegalStateException: System services not available to
Activities before onCreate()
Anybody that can give me some light on the helper classes (I´m kind of new in Java/Android) and has any clue what the error can cause? I tried to add the OnCreate() method, but it didn´t help.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要这样做...
DeviceMonitor
扩展Activity
并且您永远不应该使用new
创建Activity
的实例。 AndroidActivity
是一种特殊情况的类,不应将其视为普通的 Java 类。如果您想启动
Activity
,则需要使用startActivity...)
或其他“启动”方法之一。如果您想要一个“帮助程序”类,只需创建一个不扩展任何内容的标准 Java 类。当您从主
Activity
创建它的实例时,将Activity
Context
在其构造函数中传递给它,然后使用它来访问 Android 服务等示例...编辑:要创建助手并从主
Activity
传递Context
,请执行以下操作...Do NOT do this...
DeviceMonitor
extendsActivity
and you should never create an instance of anActivity
usingnew
. An AndroidActivity
is a special-case class and shouldn't be treated like a normal Java class.If you want to start an
Activity
you need to do it usingstartActivity...)
or one of the other 'start' methods.If you want a 'helper' class just create a standard Java class which doesn't extend anything. When you create an instance of it from your main
Activity
, pass theActivity
Context
to it in its constructor then use that to access Android services etc. Example...EDIT: To create your helper and pass a
Context
from your mainActivity
do this...您需要
在类或服务中调用 onCreate() 方法后声明您的方法。
You need to declare your
after onCreate() method is callded in your class or service.