2013-10-24 100 views
1

我試圖從res中的原始文件夾中使用final Scanner input = new Scanner(new File(R.raw.xmlsource)).useDelimiter("[\\;]+");讀取文本文件,但由於資源ID在R.java文件中爲int,我的代碼在下面從res/raw文件夾中讀取文本文件

編輯做了一些改動下面我的代碼,仍然不能運行,但使用調試器,我可以告訴大家,它實際上讀取文件,這個問題似乎是在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. 
+0

你可以張貼從logcat的錯誤? –

+0

它沒有運行,所以沒有logcat –

+1

我在changinf代碼後添加了logcat ..請看一看 –

回答

0
InputStream inputStream = getResources().openRawResource(R.raw.xmlsource); 
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 
+0

我只是在'最後的掃描儀輸入=新的掃描儀(新文件(R.raw.xmlsource))。useDelimiter(「[\\;] +」)之上添加上面的內容:'因爲我遇到了輸入問題。 hasNext()'和'input.next()' –

+0

根據您的建議對代碼進行了一些更改 –

+0

它正在工作,還是當前的logcat是添加此代碼的結果? – nhgrif

相關問題