2016-11-10 51 views
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() { 

    } 
} 
+2

文本从不分配,不是吗?在'is.read'之后''text = new String(buffer,0,size)'可以帮助 – Blackbelt

+0

@Blackbelt,那么它也没有工作......我不知道我做错了什么 – glassraven

+1

'file.txt'存在'资产/'?抛出异常了吗? – Blackbelt

回答

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

} 
} 
0

thaks到黑带的顶端,这是解决方案:

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

} 
} 
相关问题