0
我已经看到了这个问题很普遍,但在搜索我不能找出解决我的问题后:我们的目标是当一个按钮,从资产看一个简单的文本文件被点击。我已经按照这个教程,适合我的项目,但单击按钮时,没有任何反应,虽然该文件是在正确的地方。这里是提前的代码和感谢:无法读取与按钮资产文本文件,然后单击
public class ResultActivity extends Activity implements View.OnClickListener {
Button restart;
Button answers;
TextView ler;
TextView msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
msg = (TextView) findViewById(R.id.msg);
ler=(TextView) findViewById(R.id.ler);
answers=(Button) findViewById(R.id.answers);
answers.setOnClickListener(this);
restart=(Button) findViewById(R.id.restartQuiz);
restart.setOnClickListener(this);
msg.setText("Correct Answers: " + QuizActivity.correct + "Wrong Answers: "
+ QuizActivity.wrong + " Your Final Score is " + QuizActivity.score);
}
@Override
public void onClick(View view) {
if (view == restart) {
QuizActivity.score=0;
QuizActivity.correct=0;
QuizActivity.wrong=0;
Intent a = new Intent(this, MainActivity.class);
startActivity(a);
}
if(view==answers){
String text="";
try{
InputStream is= getAssets().open("file.txt");
int size=is.available();
byte [] buffer=new byte[size];
is.read(buffer);
is.close();
}catch (IOException e){
e.printStackTrace();
}
ler.setText(text);
}
}
public void onBackPressed() {
}
}
文本从不分配,不是吗?在'is.read'之后''text = new String(buffer,0,size)'可以帮助 – Blackbelt
@Blackbelt,那么它也没有工作......我不知道我做错了什么 – glassraven
'file.txt'存在'资产/'?抛出异常了吗? – Blackbelt