jeudi 5 mars 2015

Android: Why does BufferedReader reading a URL cause my app to crash?



My app crashes at line 38 from the log cat I get:



java.lang.IllegalStateException: Could not execute method of the activity



I followed the same instruction from this link http://ift.tt/1iJUvvH so I don't know why line 38 causes the app to crash.



public class MainActivity extends Activity {

private TextView mInputVal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInputVal = (TextView) findViewById(R.id.user_text);
}


public void enter_handler(View view){
String url = mInputVal.getText().toString();
boolean is_word_there = false;
try{
URL readingWeb = new URL(url);
try{
BufferedReader in = new BufferedReader(new InputStreamReader(readingWeb.openStream()));//this line crashes
String line;
while ((line = in.readLine()) != null) {
if(line.contains("tiger")){
is_word_there = true;
break;
}
}
}catch(IOException e){
e.printStackTrace();
}
}
catch(MalformedURLException e){
e.printStackTrace();
}
String text = "";
if(is_word_there == true){
text = "true!";
}
else{
text = "false";
}
makeToast(text);
}

public void makeToast(CharSequence text){
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context,text,duration);
toast.show();
}

}



Aucun commentaire:

Enregistrer un commentaire