辅助类 - 错误无法启动活动ComponentInfo: [英] Helper Class - Error Unable to start activity ComponentInfo:

查看:237
本文介绍了辅助类 - 错误无法启动活动ComponentInfo:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误无法启动活动ComponentInfo:java.lang.IllegalStateException:之前的onCreate不向活动系统服务()

Error Unable to start activity ComponentInfo: java.lang.IllegalStateException: System services not available to Activities before onCreate()

I'm与分离该code和使用辅助类的实验。 (创建不同的Java文件)
我所做的是创建活动的Java文件是在清单registred我didn't注册这个下面的类(.java文件):

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;
                    }
    }

我删除了onCreate()方法,这是正确的?
我应该有registred的清单,如果是,怎么样?

I removed the OnCreate() method, is this correct? Should I have registred in the Manifest, if yes, how?

我收到以下错误而来自registred活动这样的呼吁:

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;}

错误:

E / AndroidRuntime(1912年):了java.lang.RuntimeException:无法启动
  活动
  ComponentInfo {}包:
  java.lang.IllegalStateException:不向系统服务
  之前的onCreate活动()

E/AndroidRuntime(1912): java.lang.RuntimeException: Unable to start activity ComponentInfo{package}: java.lang.IllegalStateException: System services not available to Activities before onCreate()

任何人都可以给我一些光在辅助类(I'm一种新的用Java / Android)和有任何线索什么错误可能会导致?我尝试添加的onCreate()方法,但它didn't帮助。

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.

非常感谢!

推荐答案

不这样做...

DeviceMonitor MyDevice = new DeviceMonitor();

设备监视器扩展活动,你不应该创建一个活动的一个实​​例使用。一个Android 活动是一种特殊情况下的阶级,不应该像一个正常的Java类进行处理。

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.

如果你想开始一个活动您需要使用做 startActivity ...)或之一其他'开始'的方法。

If you want to start an Activity you need to do it using startActivity...) or one of the other 'start' methods.

如果你想要一个助手类只是创建一个标准的Java类,不延伸任何东西。当您从主活动创建它的一个实例,传活动 上下文来它在其构造,然后用它来访问Android服务等实例...

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;
    }
}

编辑::要创建你的助手,并通过一个上下文从主活动做到这一点...

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);

这篇关于辅助类 - 错误无法启动活动ComponentInfo:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆