【问题标题】:how to access TextView content in xml file to java file in Android如何将xml文件中的TextView内容访问到Android中的java文件
【发布时间】:2010-06-30 11:02:06
【问题描述】:

some1 发布访问我的 res 文件夹中可用的 .txt 文件的代码

可以写成.java文件的代码!!!


这是我收到该错误时的 LogCat 输出!!

07-01 14:50:20.886: 错误/AndroidRuntime(257): 致命异常: main 07-01 14:50:20.886: 错误/AndroidRuntime(257): java.lang.RuntimeException: 无法实例化活动 ComponentInfo{de.vogella.android.test/de.vogella.android.test.test}: java.lang 。空指针异常 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 android.os.Looper.loop(Looper.java:123) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 android.app.ActivityThread.main(ActivityThread.java:4627) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 java.lang.reflect.Method.invoke(Method.java:521) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-01 14:50:20.886:错误/AndroidRuntime(257):在 dalvik.system.NativeStart.main(本机方法) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 由: java.lang.NullPointerException 引起 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 de.vogella.android.test.test.(test.java:55) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 java.lang.Class.newInstanceImpl(Native Method) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 java.lang.Class.newInstance(Class.java:1429) 07-01 14:50:20.886: 错误/AndroidRuntime(257): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021)

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以将文件放在资产文件夹中,然后像在任何其他 Java 程序中一样打开它进行阅读。

    例如here

    【讨论】:

      【解决方案2】:

      try { InputStream raw = cntxt.getAssets().open("hello.text"); Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));文件内容 = is.toString(); }catch(IOException e) { e.printStackTrace();我在我的 .java 文件(扩展 Activity 的类)的构造函数中编写了上面的代码,它没有给出任何编译错误,但是当我尝试在我的模拟器上打开应用程序时它崩溃它显示应用程序有意外停了下来。为什么这样??是否有 anthyng wic 未定义 smwhr n 为空?

      【讨论】:

        【解决方案3】:

        试试 { InputStream raw = cntxt.getAssets().open("hello.text"); Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8")); 文件内容 = is.toString(); }catch(IOException e) { e.printStackTrace(); }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-09
          • 2021-01-04
          相关资源
          最近更新 更多