开发者

how to access TextView content in xml file to java file in Android

some1 post the code for accessing .txt file available in my res folder

the code for which can be written in .java file!!!


This is the LogCat output when i get that error!!

07-01 14:50:20.886: ERROR/AndroidRuntime(257): FATAL EXCEPTION: main 07-01 14:50:20.886: ERROR/AndroidRuntime(257): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{de.vogella.android.test/de.vogella.android.test.test}: java.lang.NullPointerException 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at android.os.Handler.dispatchMessage(Handler.java:99) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at android.os.Looper.loop(Looper.java:123开发者_JAVA技巧) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at android.app.ActivityThread.main(ActivityThread.java:4627) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at java.lang.reflect.Method.invokeNative(Native Method) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at java.lang.reflect.Method.invoke(Method.java:521) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at dalvik.system.NativeStart.main(Native Method) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): Caused by: java.lang.NullPointerException 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at de.vogella.android.test.test.(test.java:55) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at java.lang.Class.newInstanceImpl(Native Method) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at java.lang.Class.newInstance(Class.java:1429) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)


You can put the file in your assets folder and then open it to read like you would in any other Java program.

Example here.


try { InputStream raw = cntxt.getAssets().open("hello.text"); Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8")); fileContent = is.toString(); }catch(IOException e) { e.printStackTrace(); } i wrote the above piece of code in the constructor of my .java file (the class that is extending Activity) it did nt give any compile error but as soon as i try to open the app on my emulator it crashes it shows app has stopped unexpectedly. why so?? is there anthyng wic is nt defined smwhr n is null??


try { InputStream raw = cntxt.getAssets().open("hello.text"); Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8")); fileContent = is.toString(); }catch(IOException e) { e.printStackTrace(); }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜