单击按钮无法从资产中读取文本文件
Can't read text file from assets with button click
我发现这个问题很常见,但在搜索之后我无法找出问题的解决方案:目标是在单击按钮时从资产中读取一个简单的文本文件。我已经按照本教程进行了调整,以适应我的项目,但是当单击按钮时,尽管文件位于正确的位置,但什么也没有发生。这是代码并提前致谢:
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() {
}
}
感谢 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() {
}
}
This is code may help you a class whose implementation is provided by
the Android system. It allows access to application-specific resources
and classes. Return an AssetManager instance for your application's
package.
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() {
}
}
我发现这个问题很常见,但在搜索之后我无法找出问题的解决方案:目标是在单击按钮时从资产中读取一个简单的文本文件。我已经按照本教程进行了调整,以适应我的项目,但是当单击按钮时,尽管文件位于正确的位置,但什么也没有发生。这是代码并提前致谢:
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() {
}
}
感谢 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() {
}
}
This is code may help you a class whose implementation is provided by the Android system. It allows access to application-specific resources and classes. Return an AssetManager instance for your application's package.
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() {
}
}