本文介绍了如何获取上下文中的机器人服务类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误,当我尝试从内存到对象读取XML文件。好像问题得到上下文。谁能告诉我什么是错我的code?

code:

 公共类WifiScanning延伸服务{

私有静态最后字符串变量=WifiScanning;
私人定时器定时;
公众诠释的刷新率;

公共WifiScanning(){
    超();
    配置配置=新配置();
    的SAXParserFactory SPF = SAXParserFactory.newInstance();
    SAXParser的SP;
    XMLReader的XR = NULL;
    的DataHandler DataHandler的= NULL;
    尝试 {
        SP = spf.newSAXParser();
        XR = sp.getXMLReader();
        DataHandler的=新的DataHandler();
        xr.setContentHandler(DataHandler的);


        xr.parse(新的InputSource(this.openFileInput(config.xml文件)));
    }赶上(的ParserConfigurationException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(的SAXException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }

    配置= dataHandler.getConfig();
    刷新率=的Integer.parseInt(co​​nfig.getMa prefreshRate());
    // TODO自动生成构造函数存根
}

私人TimerTask的updateTask =新的TimerTask(){
    @覆盖
    公共无效的run(){
        Log.i(TAG,定时任务做工作);
    }
};

@覆盖
公众的IBinder onBind(意向意图){
    // TODO自动生成方法存根
    返回null;
}

@覆盖
公共无效的onCreate(){
    super.onCreate();
    Log.i(TAG,服务创造);

    定时器=新的定时器(TweetCollectorTimer);
    Log.i(TAG,刷新率+将String.valueOf(刷新率));
    timer.schedule(updateTask,0,3000L);

}

@覆盖
公共无效的onDestroy(){
    super.onDestroy();
    Log.i(TAG,服务破坏);

    如果(定时器!= NULL){
        timer.cancel();
        timer.purge();
        定时器= NULL;
    }
}

公共无效的onStop(){
    Log.i(TAG,服务破坏);
    如果(定时器!= NULL){
        timer.cancel();
        timer.purge();
        定时器= NULL;
    }
}
}
 

LogCat中:

  11月一十号日至1日:08:49.804:ERROR / AndroidRuntime(21514):java.lang.RuntimeException的:无法实例化服务android.wps.WifiScanning:显示java.lang.NullPointerException
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在android.app.ActivityThread.handleCreateService(ActivityThread.java:2764)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在android.app.ActivityThread.access $ 3200(ActivityThread.java:119)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1917)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在android.os.Handler.dispatchMessage(Handler.java:99)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在android.os.Looper.loop(Looper.java:123)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在android.app.ActivityThread.main(ActivityThread.java:4363)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在java.lang.reflect.Method.invokeNative(本机方法)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在java.lang.reflect.Method.invoke(Method.java:521)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在dalvik.system.NativeStart.main(本机方法)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):由:显示java.lang.NullPointerException
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在android.wps.WifiScanning< INIT>(WifiScanning.java:52)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在java.lang.Class.newInstanceImpl(本机方法)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在java.lang.Class.newInstance(Class.java:1479)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):在android.app.ActivityThread.handleCreateService(ActivityThread.java:2761)
十一月10日至一号:08:49.804:ERROR / AndroidRuntime(21514):10 ...更多
 

解决方案

您应该这样做不是在构造函数中,但只之间(含)的onCreate()和的onDestroy()。

I am getting the following error when i try to read a XML file from the memory and into an object. Seems like problem with getting the Context. Can anyone tell me whats wrong with my code?

Code:

public class WifiScanning extends Service {

private static final String TAG = "WifiScanning";
private Timer timer;
public int refreshRate;

public WifiScanning() {
    super();
    Configuration config = new Configuration();
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp;
    XMLReader xr = null;
    DataHandler dataHandler = null;
    try {
        sp = spf.newSAXParser();
        xr = sp.getXMLReader();
        dataHandler = new DataHandler();
        xr.setContentHandler(dataHandler);


        xr.parse(new InputSource(this.openFileInput("config.xml")));
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    config = dataHandler.getConfig();
    refreshRate = Integer.parseInt(config.getMapRefreshRate());
    // TODO Auto-generated constructor stub
}

private TimerTask updateTask = new TimerTask() {
    @Override
    public void run() {
        Log.i(TAG, "Timer task doing work");
    }
};

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "Service creating");

    timer = new Timer("TweetCollectorTimer");
    Log.i(TAG, "Refresh Rate: "+ String.valueOf(refreshRate));
    timer.schedule(updateTask, 0, 3000L);

}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "Service destroying");

    if (timer != null){
        timer.cancel();
        timer.purge();
        timer = null;
    }
}

public void onStop() {
    Log.i(TAG, "Service destroying");
    if (timer != null){
        timer.cancel();
        timer.purge();
        timer = null;
    }
}
}

LogCat:

10-01 11:08:49.804: ERROR/AndroidRuntime(21514): java.lang.RuntimeException: Unable to instantiate service android.wps.WifiScanning: java.lang.NullPointerException
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2764)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at android.app.ActivityThread.access$3200(ActivityThread.java:119)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1917)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at android.os.Looper.loop(Looper.java:123)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at java.lang.reflect.Method.invoke(Method.java:521)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at dalvik.system.NativeStart.main(Native Method)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514): Caused by: java.lang.NullPointerException
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at android.wps.WifiScanning.<init>(WifiScanning.java:52)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at java.lang.Class.newInstanceImpl(Native Method)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at java.lang.Class.newInstance(Class.java:1479)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2761)
10-01 11:08:49.804: ERROR/AndroidRuntime(21514):     ... 10 more
解决方案

You should do this not in the constructor, but only between (inclusive) onCreate() and onDestroy().

这篇关于如何获取上下文中的机器人服务类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 05:21
查看更多