无法通过按钮单击从资源中读取文本文件

时间:2016-11-10 16:35:09

标签: android assets file-read

我已经看到这个问题很常见,但在搜索之后我无法找出解决问题的方法:目标是在点击按钮时从资源中读取一个简单的文本文件。我已经按照本教程,适应了我的项目,但是当点击按钮时,没有任何反应,尽管文件位于正确的位置。这是代码并提前感谢:

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() {

  }
}

2 个答案:

答案 0 :(得分:0)

到了BlackBelt的尖端,这是解决方案:

 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);

       text = new String(buffer, 0, size); //this line was missing

        is.close();
    }catch (IOException e){
        e.printStackTrace();
    }
    ler.setText(text);
  }
}

public void onBackPressed() {

 }
}

答案 1 :(得分:0)

  

这段代码可以帮助你提供实现的类   Android系统。它允许访问特定于应用程序的资源   和课程。返回应用程序的AssetManager实例   封装

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{
AssetManager text = myContext.getAssets();
    InputStream is = text.open("file.txt");
        InputStream is= getAssets().open("file.txt");
        int size=is.available();
        byte [] buffer=new byte[size];
        is.read(buffer);

       text = new String(buffer, 0, size); //this line was missing

        is.close();
    }catch (IOException e){
        e.printStackTrace();
    }
    ler.setText(text);
  }
}

public void onBackPressed() {

 }
}