这段代码给我带来了问题。问题是:dummyTextID
InputStream inputStream = getResources().openRawResource(dummyTextID);
我已经dummyTextID
在@override protected void下定义了,但是必须将读取方法放在单独的下面private String
,这会创建问题
我正在提供我的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word);
Uri uri = getIntent().getData();
Cursor cursor = managedQuery(uri, null, null, null, null);
if (cursor == null) {
finish();
} else {
cursor.moveToFirst();
TextView word = (TextView) findViewById(R.id.word);
word.setMovementMethod(new ScrollingMovementMethod());
TextView definition = (TextView) findViewById(R.id.definition);
definition.setMovementMethod(new ScrollingMovementMethod());
TextView details = (TextView) findViewById(R.id.details);
details.setText(readText());
int wIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_WORD);
int dIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DEFINITION);
int Index = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DETAILS);
int dummyTextID = getResources().
getIdentifier(cursor.getString(Index), "raw", getPackageName());
word.setText(cursor.getString(wIndex));
definition.setText(cursor.getString(dIndex));
definition.setText(Html.fromHtml(cursor.getString(dIndex)));
details.setText(cursor.getString(Index));
details.setText(Html.fromHtml(cursor.getString(Index)));
}
}
private String readText() {
InputStream inputStream = getResources().openRawResource(dummyTextID);
ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i= inputStream.read();
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
public void onBackPressed() {
onSearchRequested();
}