有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在Android 2.1上加载静态文件

我需要将text文件加载到内存中,该文件位于res/raw目录中。这是我的代码:

package com.ggd543.安卓;

import 安卓.app.Activity;
import 安卓.content.res.Resources;
import 安卓.os.Bundle;
import 安卓.widget.TextView;

import java.io.IOException;
import java.io.InputStream;

public class FileActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loadText();
    }

    private void loadText() {
//        InputStream is = Resources.getSystem().openRawResource(R.raw.text);
        InputStream is = getResources().openRawResource(R.raw.text);
        try {
            byte[] buf = new byte[is.available()];
            is.read(buf, 0, buf.length);
            ((TextView) findViewById(R.layout.main)).setText(new String(buf, "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

}

当我在emulator上部署.apk并启动它时,我得到了以下错误:

12-25 14:33:38.096: ERROR/AndroidRuntime(3077): Uncaught handler: thread main exiting due to uncaught exception
12-25 14:33:38.106: ERROR/AndroidRuntime(3077): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ggd543.安卓/com.ggd543.安卓.FileActivity}: 安卓.content.res.Resources$NotFoundException: Resource ID #0x7f030000
        at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
        at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
        at 安卓.app.ActivityThread.access$2200(ActivityThread.java:119)
        at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
        at 安卓.os.Handler.dispatchMessage(Handler.java:99)
        at 安卓.os.Looper.loop(Looper.java:123)
        at 安卓.app.ActivityThread.main(ActivityThread.java:4363)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:618)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: 安卓.content.res.Resources$NotFoundException: Resource ID #0x7f030000
        at 安卓.content.res.Resources.getValue(Resources.java:891)
        at 安卓.content.res.Resources.openRawResource(Resources.java:816)
        at 安卓.content.res.Resources.openRawResource(Resources.java:798)
        at com.ggd543.安卓.FileActivity.loadText(FileActivity.java:23)
        at com.ggd543.安卓.FileActivity.onCreate(FileActivity.java:19)
        at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
        ... 11 more

谁能给我提个建议吗

Thx


共 (1) 个答案

  1. # 1 楼答案

    将文件移动到“资源”文件夹

    然后像这样检索流InputStream is = getAssets().open("fileName");

    如果你有资产/mytxtfile。txt 然后fileName=“mytxtfile.txt”