我最近开始进行android开发,面对不幸的应用程序停止的错误。应用程序非常简单,它在按下按钮时从twitter获取提要并将其显示在文本框中。多次按下按钮时发生错误。这是我的代码:
package com.example.newsfeed;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class NewsFeed extends Activity {
AsyncTask<Void, Void, Void> mTask;
String jsonString;
String url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=50cent&count=3";
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed);
b = (Button) findViewById(R.id.getFeedBtn);
final TextView tv = (TextView) findViewById(R.id.newsfeedEdit);
mTask = new AsyncTask<Void, Void, Void> () {
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try {
jsonString = getJsonFromServer(url);
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
JSONArray jArray = new JSONArray(jsonString);
String text="";
for( int i=0; i<jArray.length(); i++ ) {
JSONObject e = (JSONObject) jArray.getJSONObject(i);
JSONObject f = (JSONObject) jArray.getJSONObject(i).get("user");
text = text + "User : "+f.getString("name")+"\n";
text = text + "ID : "+e.getString("id")+"\n";
text = text + "SOURCE: "+e.getString("source")+"\n";
text = text + "TEXT : "+e.getString("text")+"\n";
}
tv.setText(text);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mTask.execute();
}
});
}
public static String getJsonFromServer(String url) throws IOException {
BufferedReader inputStream = null;
URL jsonUrl = new URL(url);
URLConnection dc = jsonUrl.openConnection();
dc.setConnectTimeout(5000);
dc.setReadTimeout(5000);
inputStream = new BufferedReader(new InputStreamReader(
dc.getInputStream()));
String jsonResult = inputStream.readLine();
return jsonResult;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_news_feed, menu);
return true;
}
}
这是我的logcat:
03-12 11:01:17.172: E/AndroidRuntime(10297): FATAL EXCEPTION: main
03-12 11:01:17.172: E/AndroidRuntime(10297): java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
03-12 11:01:17.172: E/AndroidRuntime(10297): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
03-12 11:01:17.172: E/AndroidRuntime(10297): at android.os.AsyncTask.execute(AsyncTask.java:534)
03-12 11:01:17.172: E/AndroidRuntime(10297): at com.example.newsfeed.NewsFeed$2.onClick(NewsFeed.java:83)
03-12 11:01:17.172: E/AndroidRuntime(10297): at android.view.View.performClick(View.java:4202)
03-12 11:01:17.172: E/AndroidRuntime(10297): at android.view.View$PerformClick.run(View.java:17340)
03-12 11:01:17.172: E/AndroidRuntime(10297): at android.os.Handler.handleCallback(Handler.java:725)
03-12 11:01:17.172: E/AndroidRuntime(10297): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 11:01:17.172: E/AndroidRuntime(10297): at android.os.Looper.loop(Looper.java:137)
03-12 11:01:17.172: E/AndroidRuntime(10297): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-12 11:01:17.172: E/AndroidRuntime(10297): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 11:01:17.172: E/AndroidRuntime(10297): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 11:01:17.172: E/AndroidRuntime(10297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 11:01:17.172: E/AndroidRuntime(10297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 11:01:17.172: E/AndroidRuntime(10297): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
在onClick
方法中,您必须检查您的任务是否已在运行。如果您尝试多次启动任务,那么您的应用程序将崩溃。我建议你将你的任务声明分成私有类,然后在onClick方法中实例化该类(在你检查它是否正在运行之后)。
您可以参考AsyncTask documentation了解更多详情。那里也有例子。
如果随后单击该按钮,我修改了您的代码以重新创建任务。找到它here。你将不得不重做一点,因为一直取消它是没有意义的,但我还是决定如何做一个例子。
答案 1 :(得分:1)
根据您的代码,当您单击Button时,它将执行asyntask,如果您尝试在按钮上多次单击它也会多次执行任务并崩溃。因此,请在执行期间禁用该按钮。