Java JSONArray上的NullPointerException在后台线程中初始化
我正在编写一个android应用程序,我使用一个后台线程从web服务中提取JSONArray。然后我需要在主活动中与JSONArray交互。以下是我现在正在做的:Java JSONArray上的NullPointerException在后台线程中初始化,java,android,Java,Android,我正在编写一个android应用程序,我使用一个后台线程从web服务中提取JSONArray。然后我需要在主活动中与JSONArray交互。以下是我现在正在做的: public class MainActivity extends Activity { JSONArray stories; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
public class MainActivity extends Activity {
JSONArray stories;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new getAll().execute();
// try {
System.out.println("stories.length());
// } catch (JSONException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
//}
}
和背景线程:
private class getAll extends AsyncTask <Void, Void, JSONArray> {
private static final String url = "http://10.0.2.2:8080/CalibServer/webresources/storypkg.story/";
@Override
protected JSONArray doInBackground(Void... params) {
//set up client and prepare request object to accept a json object
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
httpget.addHeader("accept", "application/json");
HttpResponse response;
String resprint = new String();
try {
response = httpclient.execute(httpget);
// Get the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
// get entity contents and convert it to string
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
resprint = result;
// construct a JSON object with result
stories =new JSONArray(result);
// Closing the input stream will trigger connection release
instream.close();
}
}
catch (ClientProtocolException e) {System.out.println("CPE"); e.printStackTrace();}
catch (IOException e) {System.out.println("IOE"); e.printStackTrace();}
catch (JSONException e) { System.out.println("JSONe"); e.printStackTrace();}
System.out.println("FUCKYEAHBG: " + resprint);
// stories = object;
return stories;
}
它的行为就像一个没有初始化故事数组,但是在调用之前,后台线程(在第行:stories=new-JSONArray(result);)不应该处理这个问题吗
我有一种感觉,这是因为线程——也许在AsyncTask运行后,我必须采取另一个步骤来更新主活动 当与UI线程并行运行的独立线程初始化和更新
故事时,您不能依赖于初始化故事
也许我还需要采取另一个步骤来更新主目录
异步任务运行后的活动
异步任务的一部分。在那里做任何你需要的UI更新。由于getAll
已经是一个私有的内部类,因此您可以完全访问该活动。您已经将stories
off返回到该(unoverriden)方法,因此这应该是一个小的更改
@Override
protected void onPostExecute (JSONArray stories)
{
//use the now initialized stories
}
当与UI线程并行运行的独立线程初始化和更新故事时,不能依赖故事初始化
也许我还需要采取另一个步骤来更新主目录
异步任务运行后的活动
异步任务的一部分。在那里做任何你需要的UI更新。由于getAll
已经是一个私有的内部类,因此您可以完全访问该活动。您已经将stories
off返回到该(unoverriden)方法,因此这应该是一个小的更改
@Override
protected void onPostExecute (JSONArray stories)
{
//use the now initialized stories
}
您正在后台线程中初始化变量。这意味着这条线
System.out.println(stories.length());
与初始化变量的代码并行执行。因此,这意味着在执行这一行时,后台线程很有可能还没有时间初始化变量
您的代码类似于以下情况:您面前有一个空杯子,请某人去煮些咖啡并给您斟满。问了之后,你马上开始喝酒。显然,杯子里不会有咖啡
重新阅读有关如何执行异步任务的android文档。您正在后台线程中初始化变量。这意味着这条线
System.out.println(stories.length());
与初始化变量的代码并行执行。因此,这意味着在执行这一行时,后台线程很有可能还没有时间初始化变量
您的代码类似于以下情况:您面前有一个空杯子,请某人去煮些咖啡并给您斟满。问了之后,你马上开始喝酒。显然,杯子里不会有咖啡
重新阅读关于如何执行异步任务的android文档