我已经编写了以下java类来读取android中的xml文件内容。但打开它显示不幸它停止工作。代码如下 -
package com.radiobot.speedreaderv1_1;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class xmlViewer extends Activity {
TextView tvXml;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xmlviewer);
File dir = Environment.getExternalStorageDirectory();
// Bundle extras = getIntent().getExtras();
// String newloc = extras.getString("loc");
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("test.xml");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
String s = readTextFile(inputStream);
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(s);
}
private String readTextFile(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
return outputStream.toString();
}
}
08-27 20:17:41.401: I/ActivityManager(287): START u0 {cmp=com.radiobot.speedreaderv1_1/.xmlViewer (has extras)} from pid 970
08-27 20:17:41.431: W/WindowManager(287): Failure taking screenshot for (246x410) to layer 21015
08-27 20:17:41.503: I/Choreographer(970): Skipped 38 frames! The application may be doing too much work on its main thread.
08-27 20:17:42.301: E/tag(970): test.xml
08-27 20:17:42.322: D/AndroidRuntime(970): Shutting down VM
08-27 20:17:42.363: W/dalvikvm(970): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-27 20:17:42.541: E/AndroidRuntime(970): FATAL EXCEPTION: main
08-27 20:17:42.541: E/AndroidRuntime(970): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.radiobot.speedreaderv1_1/com.radiobot.speedreaderv1_1.xmlViewer}: java.lang.NullPointerException
08-27 20:17:42.541: E/AndroidRuntime(970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-27 20:17:42.541: E/AndroidRuntime(970): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-27 20:17:42.541: E/AndroidRuntime(970): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-27 20:17:42.541: E/AndroidRuntime(970): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-27 20:17:42.541: E/AndroidRuntime(970): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 20:17:42.541: E/AndroidRuntime(970): at android.os.Looper.loop(Looper.java:137)
08-27 20:17:42.541: E/AndroidRuntime(970): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-27 20:17:42.541: E/AndroidRuntime(970): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 20:17:42.541: E/AndroidRuntime(970): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 20:17:42.541: E/AndroidRuntime(970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-27 20:17:42.541: E/AndroidRuntime(970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-27 20:17:42.541: E/AndroidRuntime(970): at dalvik.system.NativeStart.main(Native Method)
08-27 20:17:42.541: E/AndroidRuntime(970): Caused by: java.lang.NullPointerException
08-27 20:17:42.541: E/AndroidRuntime(970): at com.radiobot.speedreaderv1_1.xmlViewer.readTextFile(xmlViewer.java:59)
08-27 20:17:42.541: E/AndroidRuntime(970): at com.radiobot.speedreaderv1_1.xmlViewer.onCreate(xmlViewer.java:45)
08-27 20:17:42.541: E/AndroidRuntime(970): at android.app.Activity.performCreate(Activity.java:5104)
08-27 20:17:42.541: E/AndroidRuntime(970): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-27 20:17:42.541: E/AndroidRuntime(970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-27 20:17:42.541: E/AndroidRuntime(970): ... 11 more
08-27 20:17:42.782: W/ActivityManager(287): Force finishing activity com.radiobot.speedreaderv1_1/.xmlViewer
08-27 20:17:42.841: W/ActivityManager(287): Force finishing activity com.radiobot.speedreaderv1_1/.FileManager
08-27 20:17:43.351: W/ActivityManager(287): Activity pause timeout for ActivityRecord{41048108 u0 com.radiobot.speedreaderv1_1/.xmlViewer}
08-27 20:17:43.441: I/Choreographer(287): Skipped 32 frames! The application may be doing too much work on its main thread.
08-27 20:17:43.881: I/Choreographer(287): Skipped 114 frames! The application may be doing too much work on its main thread.
08-27 20:17:43.881: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
08-27 20:17:46.231: I/Process(970): Sending signal. PID: 970 SIG: 9
08-27 20:17:46.362: I/ActivityManager(287): Process com.radiobot.speedreaderv1_1 (pid 970) has died.
08-27 20:17:46.371: W/InputDispatcher(287): channel '411a95f8 com.radiobot.speedreaderv1_1/com.radiobot.speedreaderv1_1.Home (server)' ~ Consumer closed input channel or an error occurred. events=0x9
08-27 20:17:46.371: E/InputDispatcher(287): channel '411a95f8 com.radiobot.speedreaderv1_1/com.radiobot.speedreaderv1_1.Home (server)' ~ Channel is unrecoverably broken and will be disposed!
08-27 20:17:46.401: W/InputDispatcher(287): channel '411b17d0 com.radiobot.speedreaderv1_1/com.radiobot.speedreaderv1_1.FileManager (server)' ~ Consumer closed input channel or an error occurred. events=0x9
08-27 20:17:46.401: E/InputDispatcher(287): channel '411b17d0 com.radiobot.speedreaderv1_1/com.radiobot.speedreaderv1_1.FileManager (server)' ~ Channel is unrecoverably broken and will be disposed!
08-27 20:17:46.441: I/WindowState(287): WIN DEATH: Window{411a95f8 u0 com.radiobot.speedreaderv1_1/com.radiobot.speedreaderv1_1.Home}
08-27 20:17:46.441: W/InputDispatcher(287): Attempted to unregister already unregistered input channel '411a95f8 com.radiobot.speedreaderv1_1/com.radiobot.speedreaderv1_1.Home (server)'
08-27 20:17:46.481: I/WindowState(287): WIN DEATH: Window{411b17d0 u0 com.radiobot.speedreaderv1_1/com.radiobot.speedreaderv1_1.FileManager}
08-27 20:17:46.481: W/InputDispatcher(287): Attempted to unregister already unregistered input channel '411b17d0 com.radiobot.speedreaderv1_1/com.radiobot.speedreaderv1_1.FileManager (server)'
08-27 20:17:46.601: D/dalvikvm(984): Not late-enabling CheckJNI (already on)
08-27 20:17:46.641: I/ActivityManager(287): Start proc com.radiobot.speedreaderv1_1 for activity com.radiobot.speedreaderv1_1/.Home: pid=984 uid=10046 gids={50046, 3003, 1028}
08-27 20:17:47.001: I/Choreographer(287): Skipped 108 frames! The application may be doing too much work on its main thread.
08-27 20:17:47.331: I/Choreographer(287): Skipped 50 frames! The application may be doing too much work on its main thread.
08-27 20:17:47.441: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
08-27 20:17:47.883: I/Choreographer(287): Skipped 118 frames! The application may be doing too much work on its main thread.
08-27 20:17:48.041: E/Trace(984): error opening trace file: No such file or directory (2)
08-27 20:17:48.282: I/Choreographer(287): Skipped 99 frames! The application may be doing too much work on its main thread.
08-27 20:17:48.881: D/dalvikvm(984): GC_FOR_ALLOC freed 55K, 7% free 2498K/2676K, paused 67ms, total 72ms
08-27 20:17:48.891: I/dalvikvm-heap(984): Grow heap (frag case) to 3.141MB for 614416-byte allocation
08-27 20:17:49.031: D/dalvikvm(984): GC_FOR_ALLOC freed 2K, 6% free 3096K/3280K, paused 131ms, total 131ms
08-27 20:17:49.173: D/dalvikvm(984): GC_CONCURRENT freed <1K, 6% free 3115K/3280K, paused 13ms+16ms, total 145ms
08-27 20:17:49.671: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
08-27 20:17:49.931: W/InputMethodManagerService(287): Got RemoteException sending setActive(false) notification to pid 970 uid 10046
08-27 20:17:49.992: I/Choreographer(984): Skipped 85 frames! The application may be doing too much work on its main thread.
08-27 20:17:50.091: D/gralloc_goldfish(984): Emulator without GPU emulation detected.
08-27 20:17:50.412: I/ActivityManager(287): Displayed com.radiobot.speedreaderv1_1/.Home: +3s876ms (total +8s843ms)
08-27 20:17:50.641: D/ExchangeService(654): Received deviceId from Email app: null
08-27 20:17:50.641: D/ExchangeService(654): !!! deviceId unknown; stopping self and retrying
08-27 20:17:55.723: D/ExchangeService(654): !!! EAS ExchangeService, onCreate
08-27 20:17:55.752: D/ExchangeService(654): !!! EAS ExchangeService, onStartCommand, startingUp = false, running = false
08-27 20:17:55.921: D/ExchangeService(654): !!! EAS ExchangeService, onStartCommand, startingUp = true, running = false
08-27 20:17:55.962: W/ActivityManager(287): Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
08-27 20:17:55.962: D/ExchangeService(654): !!! Email application not found; stopping self
08-27 20:17:56.042: W/ActivityManager(287): Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
08-27 20:17:56.082: E/ActivityThread(654): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d3ed80 that was originally bound here
08-27 20:17:56.082: E/ActivityThread(654): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d3ed80 that was originally bound here
08-27 20:17:56.082: E/ActivityThread(654): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
08-27 20:17:56.082: E/ActivityThread(654): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
08-27 20:17:56.082: E/ActivityThread(654): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
08-27 20:17:56.082: E/ActivityThread(654): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
08-27 20:17:56.082: E/ActivityThread(654): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
08-27 20:17:56.082: E/ActivityThread(654): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
08-27 20:17:56.082: E/ActivityThread(654): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
08-27 20:17:56.082: E/ActivityThread(654): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
08-27 20:17:56.082: E/ActivityThread(654): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
08-27 20:17:56.082: E/ActivityThread(654): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
08-27 20:17:56.082: E/ActivityThread(654): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
08-27 20:17:56.082: E/ActivityThread(654): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
08-27 20:17:56.082: E/ActivityThread(654): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-27 20:17:56.082: E/ActivityThread(654): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-27 20:17:56.082: E/ActivityThread(654): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-27 20:17:56.082: E/ActivityThread(654): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-27 20:17:56.082: E/ActivityThread(654): at java.lang.Thread.run(Thread.java:856)
08-27 20:17:56.111: E/StrictMode(654): null
08-27 20:17:56.111: E/StrictMode(654): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d3ed80 that was originally bound here
08-27 20:17:56.111: E/StrictMode(654): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
08-27 20:17:56.111: E/StrictMode(654): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
08-27 20:17:56.111: E/StrictMode(654): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
08-27 20:17:56.111: E/StrictMode(654): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
08-27 20:17:56.111: E/StrictMode(654): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
08-27 20:17:56.111: E/StrictMode(654): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
08-27 20:17:56.111: E/StrictMode(654): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
08-27 20:17:56.111: E/StrictMode(654): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
08-27 20:17:56.111: E/StrictMode(654): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
08-27 20:17:56.111: E/StrictMode(654): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
08-27 20:17:56.111: E/StrictMode(654): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
08-27 20:17:56.111: E/StrictMode(654): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
08-27 20:17:56.111: E/StrictMode(654): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-27 20:17:56.111: E/StrictMode(654): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-27 20:17:56.111: E/StrictMode(654): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-27 20:17:56.111: E/StrictMode(654): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-27 20:17:56.111: E/StrictMode(654): at java.lang.Thread.run(Thread.java:856)
08-27 20:17:56.151: W/ActivityManager(287): Unbind failed: could not find connection for android.os.BinderProxy@41000838
08-27 20:17:56.213: E/ActivityThread(654): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cdf3e8 that was originally bound here
08-27 20:17:56.213: E/ActivityThread(654): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cdf3e8 that was originally bound here
08-27 20:17:56.213: E/ActivityThread(654): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
08-27 20:17:56.213: E/ActivityThread(654): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
08-27 20:17:56.213: E/ActivityThread(654): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
08-27 20:17:56.213: E/ActivityThread(654): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
08-27 20:17:56.213: E/ActivityThread(654): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
08-27 20:17:56.213: E/ActivityThread(654): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
08-27 20:17:56.213: E/ActivityThread(654): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
08-27 20:17:56.213: E/ActivityThread(654): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
08-27 20:17:56.213: E/ActivityThread(654): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
08-27 20:17:56.213: E/ActivityThread(654): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
08-27 20:17:56.213: E/ActivityThread(654): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
08-27 20:17:56.213: E/ActivityThread(654): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-27 20:17:56.213: E/ActivityThread(654): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-27 20:17:56.213: E/ActivityThread(654): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-27 20:17:56.213: E/ActivityThread(654): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-27 20:17:56.213: E/ActivityThread(654): at java.lang.Thread.run(Thread.java:856)
08-27 20:17:56.261: E/StrictMode(654): null
08-27 20:17:56.261: E/StrictMode(654): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cdf3e8 that was originally bound here
08-27 20:17:56.261: E/StrictMode(654): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
08-27 20:17:56.261: E/StrictMode(654): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
08-27 20:17:56.261: E/StrictMode(654): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
08-27 20:17:56.261: E/StrictMode(654): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
08-27 20:17:56.261: E/StrictMode(654): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
08-27 20:17:56.261: E/StrictMode(654): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
08-27 20:17:56.261: E/StrictMode(654): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
08-27 20:17:56.261: E/StrictMode(654): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
08-27 20:17:56.261: E/StrictMode(654): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
08-27 20:17:56.261: E/StrictMode(654): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
08-27 20:17:56.261: E/StrictMode(654): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
08-27 20:17:56.261: E/StrictMode(654): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-27 20:17:56.261: E/StrictMode(654): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-27 20:17:56.261: E/StrictMode(654): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-27 20:17:56.261: E/StrictMode(654): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-27 20:17:56.261: E/StrictMode(654): at java.lang.Thread.run(Thread.java:856)
08-27 20:17:56.271: W/ActivityManager(287): Unbind failed: could not find connection for android.os.BinderProxy@411bda30
08-27 20:18:00.052: W/SystemClock(287): time going backwards: prev 1497368048379(ioctl) vs now 1497361133793(ioctl), tid=315
请注意我在mnt / sdcard目录下有xml文件。请帮我解决。
答案 0 :(得分:1)
尝试读取xml文件时有NullPointerException
。
要使用AssetManager
读取xml文件,您需要将xml文件放在项目的assets
文件夹中。
在问题中你说xml在mnt/sdcard
。它应该在yourproject/assets
文件夹中。
编辑:
只需替换您的AssetManager代码
即可 AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("test.xml");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
使用以下代码
File sdcard = Environment.getExternalStorageDirectory();
//Get the xml file
File file = new File(sdcard,"test.xml");
InputStream is = new FileInputStream(file);
另外,请确保您在清单上拥有以下权限
android.permission.READ_EXTERNAL_STORAGE
答案 1 :(得分:0)
错误在于:
try {
inputStream = assetManager.open("test.xml");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
String s = readTextFile(inputStream);
LogCat显示带有“tag”标记的邮件,这意味着您无法创建inputStream
,因此当它到达null
时它是readTextFile()
方法,导致NullPointerException