简体   繁体   English

如何在android中将xml文件中的TextView内容访问到java文件

[英]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 some1发布用于访问我的res文件夹中的.txt文件的代码

the code for which can be written in .java file!!! 可以在.java文件中编写的代码!!!


This is the LogCat output when i get that error!! 当我收到该错误时,这是​​LogCat的输出!!

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) 07-01 14:50:20.886: ERROR/AndroidRuntime(257): at a 07-01 14:50:20.886:错误/ AndroidRuntime(257):致命异常:主要07-01 14:50:20.886:错误/ AndroidRuntime(257):java.lang.RuntimeException:无法实例化活动ComponentInfo {de。 vogella.android.test / de.vogella.android.test.test}:java.lang.NullPointerException 07-01 14:50:20.886:ERROR / AndroidRuntime(257):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java :2585)07-01 14:50:20.886:ERROR / AndroidRuntime(257):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)07-01 14:50:20.886:ERROR / AndroidRuntime(257):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)07-01 14:50:20.886:ERROR / AndroidRuntime(257):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)07 -01 14:50:20.886:ERROR / AndroidRuntime(257):在android.os.Handler.dispatchMessage(Handler.java:99)07-01 14:50:20.886:ERROR / AndroidRuntime(257):在android.os .Looper.loop(Looper.java:123)07-01 14:50:20.886:错误/ AndroidRuntime(257):在 ndroid.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/And ndroid.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):位于java.lang.reflect.Method.invoke(Method.java:521)07-01 14:50:20.886:ERROR / AndroidRuntime(257):位于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):在dalvik.system.NativeStart.main(本地方法)07-01 14:50:20.886:ERROR / AndroidRuntime(257):原因: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) :在java.lang.Class.newInstanceImpl(本机方法)07-01 14:50:20.886:错误/ AndroidRuntime(257):在java.lang.Class.newInstance(Class.java:1429)07-01 14:50 :20.886:错误/ roidRuntime(257): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) roidRuntime(257):位于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. 您可以将文件放在资产文件夹中,然后将其打开以像在任何其他Java程序中一样进行读取。

Example here . 这里的例子。

try { InputStream raw = cntxt.getAssets().open("hello.text"); 尝试{InputStream raw = cntxt.getAssets()。open(“ hello.text”); Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8")); Reader是= new BufferedReader(new InputStreamReader(raw,“ UTF8”)); fileContent = is.toString(); fileContent = is.toString(); }catch(IOException e) { e.printStackTrace(); } 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. 我在.java文件的构造函数(扩展Activity的类)中编写了上面的代码,它并没有给出任何编译错误,但是当我尝试在模拟器上打开应用程序时,它崩溃了,它表明应用程序具有意外停止。 why so?? 为什么这样?? is there anthyng wic is nt defined smwhr n is null?? 是否有nt定义的smwhr n是否为null?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM