帮助程序类 - 错误无法启动活动 ComponentInfo:

发布于 2024-12-23 17:24:49 字数 1468 浏览 2 评论 0 原文

错误无法启动活动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃→可爱长大的 2024-12-30 17:24:49

不要这样做...

DeviceMonitor MyDevice = new DeviceMonitor();

DeviceMonitor 扩展 Activity 并且您永远不应该使用 new 创建 Activity 的实例。 Android Activity 是一种特殊情况的类,不应将其视为普通的 Java 类。

如果您想启动 Activity,则需要使用 startActivity...) 或其他“启动”方法之一。

如果您想要一个“帮助程序”类,只需创建一个不扩展任何内容的标准 Java 类。当您从主 Activity 创建它的实例时,将 Activity Context 在其构造函数中传递给它,然后使用它来访问 Android 服务等示例...

public class DeviceMonitor {

    Context mContext = null;

    public DeviceMonitor (Context context) {
        mContext = context;
    }
}

编辑:要创建助手并从主Activity传递Context,请执行以下操作...

// An Activity IS a Context so pass 'this'
DeviceMonitor MyDevice = new DeviceMonitor(this);

Do NOT do this...

DeviceMonitor MyDevice = new DeviceMonitor();

DeviceMonitor extends Activity and you should never create an instance of an Activity using new. An Android Activity 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 using startActivity...) 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 the Activity Context to it in its constructor then use that to access Android services etc. Example...

public class DeviceMonitor {

    Context mContext = null;

    public DeviceMonitor (Context context) {
        mContext = context;
    }
}

EDIT: To create your helper and pass a Context from your main Activity do this...

// An Activity IS a Context so pass 'this'
DeviceMonitor MyDevice = new DeviceMonitor(this);
给我一枪 2024-12-30 17:24:49

您需要

boolean GPSEnabled 

在类或服务中调用 onCreate() 方法后声明您的方法。

You need to declare your

boolean GPSEnabled 

after onCreate() method is callded in your class or service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文