R.raw.content1_1是一个int。 有一个文本来存储所有内容的路径。当我使用缓冲读取器读取路径时,它无法工作。 但是如果我不将String解析为int,它就会很好。 当我想显示内容时,我发现路径是从数据库中获取的字符串“R.raw.content1_1”。
public void addContent(){
new Thread(new Runnable() {
@Override
public void run() {
try {
SQLiteDatabase db = dbHelper.getWritableDatabase();
BufferedReader localBufferReader = new BufferedReader(
new InputStreamReader(getResources().openRawResource(
R.raw.title1)));
BufferedReader localBufferReader1 = new BufferedReader(
new InputStreamReader(getResources().openRawResource(
R.raw.content_path1)));
BufferedReader localBufferReader2 = new BufferedReader(
new InputStreamReader(getResources().openRawResource(
R.raw.video_path1)));
for (int i = 1; i < 97; i++) {
String title = localBufferReader.readLine();
// int content_path =Integer.parseInt(localBufferReader1.readLine());
String content_path1 = localBufferReader1.readLine();
//the content_path1 cannot parse to int.
int content_path = Integer.parseInt(content_path1);
String video_path = localBufferReader2.readLine();
if (title == null) {
localBufferReader.close();
localBufferReader1.close();
localBufferReader2.close();
break;
}
ContentValues cv = new ContentValues();
cv.put("id", i);
cv.put("title", title);
cv.put("content", content_path);
cv.put("path", video_path);
db.insert("book1", null, cv);
}
Log.v("successful","777hhaa");
db.close();
}catch (Exception e){
e.printStackTrace();
}
}
如果我存储的路径是数据库中的字符串,openRawResource(int)...无法打开原始资源
private void displayContent(){
String course = mainActivity.getCourse();
book = mainActivity.getBook();
String columns = "content";
Cursor cursor = db.query(book, new String[] {columns}, "title=?", new String[] {course}, null, null, null, null);
while (cursor.moveToNext()) {
cursor.getString(cursor.getColumnIndex("content"));
content = cursor.getString(0);
//content = Integer.parseInt(content1);
int content1 = Integer.parseInt(content,16);
textView.setText(content1);
}
cursor.close();
/*
try{
InputStream inputStream = getResources().openRawResource(content);
InputStreamReader inputReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputReader);
textView.setText(bufferedReader.readLine());
}catch (Exception e){
e.printStackTrace();
}*/
}
所有错误消息:
AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.iuuu.listenstudy, PID: 2721
java.lang.NumberFormatException: Invalid int: "R.raw.content1_1"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at com.example.iuuu.listenstudy.ContentFragment.addContent(ContentFragment.java:58)
at com.example.iuuu.listenstudy.ContentFragment.onCreateView(ContentFragment.java:42)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
R.raw.content1_1,可能是一个int,但“R.raw.content1_1”肯定不是,并且没有办法解析它来说它是资源1或资源n。您应该将资源ID存储在原始文件中,而不是“资源名称”。
您可以使用资源而不是您在那里执行的操作来保存资源的完整路径并通过...路径而非ID来访问它
答案 1 :(得分:0)
问题在于您尝试将 R.raw.content1_1 解析为int
,我认为您想要做的是打开任何资源 R.raw .content1_1 指向并将 解析为int
。
由于您从文件中获取原始资源的路径,因此您将无法使用与代码中相同的语法,因为您将获得String
containsint R.raw。 content1_1 而不是指向资源ID的Java代码。
相反,您应该将 content1_1 放入您的文件中,然后使用Resources.getIdentifier()中的this answer:
String title = localBufferReader.readLine();
String content_path = localBufferReader1.readLine();
// Get the resource ID
int contentResourceId = getResources().getIdentifier("raw/" + content_path, null, this.getPackage());
// Open the resource as a buffered reader
BufferedReader contentBufferReader = new BufferedReader(new InputStreamReader(getResources().openRawResource(contentResourceId)));
// Parse the contents as an int
int content = Integer.parseInt(contentBufferReader.readLine());