1

我试图从 res using 的原始文件夹中读取文本文件,final Scanner input = new Scanner(new File(R.raw.xmlsource)).useDelimiter("[\\;]+");但由于资源 id 在 R.java 文件中是 int ,所以它没有读取,我的代码如下

编辑在下面对我的代码进行了一些更改,但仍然无法运行,但使用调试器我可以看出它实际上读取了文件,问题似乎出在String[] RssLinksArray = readLine.split("[\\;]+");代码终止的地方,我一生都无法弄清楚为什么。我也附上了logcat。

任何帮助将不胜感激。

package com.simplerssreader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
import java.util.List;
import java.util.Scanner;

import org.xmlpull.v1.XmlPullParserException;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;

public class RssService extends IntentService 
{
public static final String ITEMS = "items";
public static final String RECEIVER = "receiver";

public RssService() 
{
    super("RssService");
}

@Override
protected void onHandleIntent(Intent intent) 
{   
    InputStream is = getResources().openRawResource(R.raw.xmlsource);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String readLine = null;

    try {
        while ((readLine = br.readLine()) != null) 
        {


        }
    } catch (IOException e) 
    {
        e.printStackTrace();
    }

    String[] RssLinksArray = readLine.split("[\\;]+");
    final String RSS_LINK = RssLinksArray[0];

    Log.d(Constants.TAG, "Service started");
    List<RssItem> rssItems = null;
    try 
    {
        XMLRssParser parser = new XMLRssParser();
        rssItems = parser.parse(getInputStream(RSS_LINK));
    } 
    catch (XmlPullParserException e) 
    {
        Log.w(e.getMessage(), e);
    } 
    catch (IOException e) 
    {
        Log.w(e.getMessage(), e);
    }
    Bundle bundle = new Bundle();
    bundle.putSerializable(ITEMS, (Serializable) rssItems);
    ResultReceiver receiver = intent.getParcelableExtra(RECEIVER);
    receiver.send(0, bundle);
}

public InputStream getInputStream(String link) 
{
    try 
    {
        URL url = new URL(link);
        return url.openConnection().getInputStream();
    } catch (IOException e) 
    {
        Log.w(Constants.TAG, "Exception while retrieving the input stream", e);
        return null;
    }
}
}

LOGCAT

10-24 23:07:49.908: D/dalvikvm(1189): GC_FOR_ALLOC freed 101K, 9% free 2778K/3040K, paused 68ms, total 72ms
10-24 23:07:49.938: I/dalvikvm-heap(1189): Grow heap (frag case) to 3.939MB for 1127536-byte allocation
10-24 23:07:50.089: D/dalvikvm(1189): GC_FOR_ALLOC freed 2K, 7% free 3877K/4144K, paused 149ms, total 149ms
10-24 23:07:50.461: W/dalvikvm(1189): threadid=11: thread exiting with uncaught exception (group=0x41465700)
10-24 23:07:50.504: E/AndroidRuntime(1189): FATAL EXCEPTION: IntentService[RssService]
10-24 23:07:50.504: E/AndroidRuntime(1189): java.lang.NullPointerException
10-24 23:07:50.504: E/AndroidRuntime(1189):     at com.simplerssreader.RssService.onHandleIntent(RssService.java:48)
10-24 23:07:50.504: E/AndroidRuntime(1189):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-24 23:07:50.504: E/AndroidRuntime(1189):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 23:07:50.504: E/AndroidRuntime(1189):     at android.os.Looper.loop(Looper.java:137)
10-24 23:07:50.504: E/AndroidRuntime(1189):     at android.os.HandlerThread.run(HandlerThread.java:61)
10-24 23:07:50.899: D/libEGL(1189): loaded /system/lib/egl/libEGL_emulation.so
10-24 23:07:50.940: D/(1189): HostConnection::get() New Host Connection established 0x2a1db628, tid 1189
10-24 23:07:51.078: D/libEGL(1189): loaded /system/lib/egl/libGLESv1_CM_emulation.so
10-24 23:07:51.279: D/libEGL(1189): loaded /system/lib/egl/libGLESv2_emulation.so
10-24 23:07:51.699: W/EGL_emulation(1189): eglSurfaceAttrib not implemented
10-24 23:07:51.729: D/OpenGLRenderer(1189): Enabling debug mode 0
10-24 23:07:51.769: I/Choreographer(1189): Skipped 85 frames!  The application may be doing too much work on its main thread.
10-24 23:07:56.358: I/Choreographer(1189): Skipped 265 frames!  The application may be doing too much work on its main thread.
4

1 回答 1

0
InputStream inputStream = getResources().openRawResource(R.raw.xmlsource);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
于 2013-10-24T23:55:57.097 回答