Java Can';t在Android 2.1上加载静态文件
我需要将Java Can';t在Android 2.1上加载静态文件,java,android,Java,Android,我需要将text文件加载到内存中,该文件位于res/raw目录中。这是我的密码: package com.ggd543.android; import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.widget.TextView; import java.io.IOException; import java.io.InputStream
text
文件加载到内存中,该文件位于res/raw
目录中。这是我的密码:
package com.ggd543.android;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.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.android/com.ggd543.android.FileActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030000
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$2200(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030000
at android.content.res.Resources.getValue(Resources.java:891)
at android.content.res.Resources.openRawResource(Resources.java:816)
at android.content.res.Resources.openRawResource(Resources.java:798)
at com.ggd543.android.FileActivity.loadText(FileActivity.java:23)
at com.ggd543.android.FileActivity.onCreate(FileActivity.java:19)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
... 11 more
谁能给我提个建议吗
Thx将文件移动到资产文件夹 然后像这样检索流
InputStream is=getAssets().open(“fileName”)代码>
如果您有资产/mytxtfile.txt
然后fileName=“mytxtfile.txt”文件名是什么?因为根据odcs,如果您在/res/raw中有一个名为'abcd.txt'的文件,您应该使用资源ID“R.raw.abcd”打开它,即不包括3位数的扩展名。我的文件名是text
放入res/raw
目录,您可以重命名您的文件以具有扩展名,比如'text.txt'之类的吗,然后再试一次?奇怪的是,text
和text.txt
放在res/raw
目录下都能正常工作,但为什么android系统不能在res/raw
目录下找到它呢@爱国者 尝试添加包名:getResources().openRawResource(com.YourProject.R.raw.text)代码>