如何在android服务类中获取上下文

发布于 2024-12-07 16:28:40 字数 4392 浏览 2 评论 0原文

当我尝试从内存读取 XML 文件并将其写入对象时,出现以下错误。似乎获取上下文有问题。谁能告诉我我的代码有什么问题吗?

代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不一样的天空 2024-12-14 16:28:40

您不应在构造函数中执行此操作,而应仅在(包括)onCreate() 和 onDestroy() 之间执行此操作。

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

半暖夏伤 2024-12-14 16:28:40

如果您使用的是服务,则应该在 onStartCommand,不在构造函数中

If you are using a service you are supposed to write that code in onStartCommand, not in the constructor

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