Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android应用程序崩溃:将JSON解析为ListView_Android_Json_Android Listview_Crash_Android Asynctask - Fatal编程技术网

Android应用程序崩溃:将JSON解析为ListView

Android应用程序崩溃:将JSON解析为ListView,android,json,android-listview,crash,android-asynctask,Android,Json,Android Listview,Crash,Android Asynctask,我之前发过帖子,但还是有同样的问题。我的android应用程序将加载此活动,然后在能够显示JSON对象中的任何信息之前崩溃。我不知道为什么。任何帮助都将不胜感激 以下是我对不断崩溃的活动的代码: public class MainScreen extends ListActivity{ JSONObject jsonObj= null; String[] eventArr= new String[10]; int i=0; String[] urlArr =

我之前发过帖子,但还是有同样的问题。我的android应用程序将加载此活动,然后在能够显示JSON对象中的任何信息之前崩溃。我不知道为什么。任何帮助都将不胜感激

以下是我对不断崩溃的活动的代码:

public class MainScreen extends ListActivity{
    JSONObject jsonObj= null;

    String[] eventArr= new String[10];
    int i=0;

    String[] urlArr = new String[10];//"http://accomplist.herokuapp.com/api/v1/sharedevent/1/?format=json";

    private static final String TAG_EVENT="event"; //A JSON object within the JSON object that will be returned by JSONParse()
    private static final String TAG_DESCRIPTION="description"; //A JSON tag within the JSON object EVENT
    private static String eventString="Yo";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);
        ListView eventsList= getListView();
        for(int b=0; b<urlArr.length; b++) {
            urlArr[b]= "http://accomplist.herokuapp.com/api/v1/sharedevent/"+(b+1)+"/?format=json";
        }
        new JSONParse().execute(urlArr[0]);
    }
private class JSONParse extends AsyncTask<String, Void, String> {
    HttpClient client=new DefaultHttpClient();


    @Override
    protected String doInBackground(String... jsonurl) {
//        StringBuilder url= new StringBuilder(String.valueOf(jsonurl));
        HttpUriRequest request= new HttpGet("http://accomplist.herokuapp.com/api/v1/sharedevent/2/?format=json");
        HttpResponse r= null;
        try {
            r = client.execute(request);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        int status= r.getStatusLine().getStatusCode();
        if (status==200){
            HttpEntity e=r.getEntity();
            String data= null;
            try {
                data = EntityUtils.toString(e);
            } catch (IOException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            try {
                jsonObj = new JSONObject(data);
            } catch (JSONException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            try {
                JSONObject eventJson= jsonObj.getJSONObject(TAG_EVENT);
                eventString= eventJson.getString(TAG_DESCRIPTION);
            }
            catch (JSONException e1) {
                eventString="Couldn't Parse Data";
            }
            return eventString;
        }
        else{
            return eventString;
        }
    }
    protected void onProgressUpdate() {
        Toast loadingToast= Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_LONG);
        loadingToast.show();
    }
    protected void onPostExecute(String result) {
        eventString=result;
        eventArr[i]=eventString;
        i++;
        MainScreen.this.setListAdapter(new ArrayAdapter<String>(MainScreen.this,
                android.R.layout.simple_list_item_1,eventArr));

    }
}
}
public类主屏幕扩展ListActivity{
JSONObject jsonObj=null;
String[]eventArr=新字符串[10];
int i=0;
字符串[]urlArr=新字符串[10];/“http://accomplist.herokuapp.com/api/v1/sharedevent/1/?format=json";
private static final String TAG_EVENT=“EVENT”;//JSON对象中的JSON对象,将由JSONParse()返回
私有静态最终字符串标记\u DESCRIPTION=“DESCRIPTION”//JSON对象事件中的JSON标记
私有静态字符串eventString=“Yo”;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局主屏幕);
ListView事件列表=getListView();

对于(int b=0;b您是
NullPointerException
异常,因为您没有在数组中添加10项当前您只在
eventArr
数组中添加一项,但数组大小为10

建议如果ArrayList中的项目是动态大小的,请使用ArrayList而不是Array。将代码更改为:

声明ArrayList代替数组:

ArrayList<String> eventArr=new ArrayList<String>();

谢谢你,事实证明这是解决问题的方法!非常感谢你的帮助:)@sourdesi:非常欢迎的朋友,如果这有助于你解决当前的问题,你能给出答案吗?谢谢
ArrayList<String> eventArr=new ArrayList<String>();
  @Override
 protected void onPostExecute(String result) {

        eventString=result;

        eventArr.add(result);  //<< add item to ArrayList

        MainScreen.this.setListAdapter(new ArrayAdapter<String>(MainScreen.this,
                android.R.layout.simple_list_item_1,eventArr));

    }