這裏不工作是我的應用程序的Java代碼:我的應用程序工作正常,在Android 2.3.3版本,但是在Android 4.2.2
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new GetUrl().execute(20);
}
private class GetUrl extends AsyncTask<Integer, Integer, BufferedReader> {
@Override
protected BufferedReader doInBackground(Integer... params) {
BufferedReader reader = null;
try {
URL url = null;
url = new URL("http://www.google.com");
URLConnection conn = url.openConnection();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
}
catch (Exception e) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Error");
}
return reader;
}
@Override
protected void onPostExecute(BufferedReader reader) {
TextView tv = (TextView) findViewById(R.id.textView1);
String line = null;
try {
line = reader.readLine().toString(); /*This line if removed removes the error. Whenever i try to work with the reader, it stops working for 4.2.2 but works fine for 2.3.3*/
tv.setText(line);
}
catch(Exception e) {
tv.setText(e.getMessage());
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
每當我試圖與讀寫器進行工作,它停止工作對於4.2.2而言,但對於2.3.3來說工作正常。我之前發佈過類似的問題,並被要求使用asyncTask解決問題,但這並沒有真正幫助我。
這是什麼意思'停止工作'?你觀察到任何異常嗎? – sandrstar
不,e.getMessage什麼也沒有顯示 –
@sandstar你能來我的團隊查看器並檢查一下自己嗎 –