Android 如果internet连接不可用,带有json和sqlite的应用程序将崩溃

Android 如果internet连接不可用,带有json和sqlite的应用程序将崩溃,android,json,sqlite,listview,Android,Json,Sqlite,Listview,我正在处理JSON数据的获取和显示,但在此之前,我将其存储在Sqlite中 从Sqlite表中提取后,当互联网可用时,它可以正常工作,但当互联网连接不可用时,应用程序会自动关闭。我正在使用哈希映射自定义适配器在listview中显示数据。我已经从SqlHelper类创建了一个fetchdata方法 protected Void doInBackground(Void... params) { // Create an array arraylist = new ArrayList<H

我正在处理JSON数据的获取和显示,但在此之前,我将其存储在Sqlite中

从Sqlite表中提取后,当互联网可用时,它可以正常工作,但当互联网连接不可用时,应用程序会自动关闭。我正在使用哈希映射自定义适配器在listview中显示数据。我已经从SqlHelper类创建了一个fetchdata方法

 protected Void doInBackground(Void... params) 
 {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
    int state = NetworkUtilClass.checkInternetConenction(getActivity());
    if (state == 1) {
        // jsonobject = new JSONObject(str1);
        jsonobject = JSONFunction.getJSONfromURL("url");
        JSONObject collection = null;
        try {
            collection = jsonobject.getJSONObject("collection");
        }
            catch (JSONException e) {
            // TODO Auto-generated catch block
                e.printStackTrace();
        }
        JSONArray response = null;
        try {
            response = collection.getJSONArray("response");
        }
            catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 0; i < response.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject jsonobject1 = null;
            try {
                jsonobject1 = (JSONObject) response.get(i);
                noticeId = jsonobject1.getString("id").toString();
                noticeTitle = jsonobject1.getString("title").toString();
                noticeDescription = jsonobject1.getString("description").toString();
                noticePublishedBy = jsonobject1.getString("publishedBy").toString();
                noticeValidFrom = jsonobject1.getString("validFrom").toString();
                noticeValidTo = jsonobject1.getString("validTo").toString();
                Log.e(noticeId, "show");
                Log.e(noticeTitle, "show");
                Log.e(noticeDescription, "show");
                //demo_database.insertData(noticeTitle,noticeDescription,noticePublishedBy,noticeValidFrom,noticeValidTo);
                            demo_database.insertNoticeData(noticeId,noticeTitle,noticeDescription, noticePublishedBy,
                            noticeValidFrom, noticeValidTo);
               } 
                   catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    else{
        onPostExecute(null);
    }
    return null;
  }

  @Override
  protected void onPostExecute(Void args) 
  {
    // Locate the listview in listview_main.xml
    getData();
    demo_database.close();

// Close the progressdialog
mProgressDialog.dismiss();
  }
  }

  private void getData() {
       // TODO Auto-generated method stub
       try {
        arraylist = demo_database.fetchNoticeData();
       } catch (Exception e) {
        e.printStackTrace();
       }
       listview = (ListView) getActivity().findViewById(R.id.listview);
       listview.setOnItemClickListener(new OnItemClickListener() {

       @Override
       public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
        Toast.makeText(getActivity(), "ListView clicked", Toast.LENGTH_SHORT).show();
       }
    });

    // Pass the results into ListViewAdapter.java
    adapter = new NoticeListViewAdapter(getActivity(), arraylist);

    // Set the adapter to the ListView
    listview.setAdapter(adapter);
    }
  }
受保护的Void doInBackground(Void…params)
{
//创建一个数组
arraylist=新的arraylist();
int state=NetworkUtilClass.checkInternetConenction(getActivity());
如果(状态==1){
//jsonobject=新的jsonobject(str1);
jsonobject=JSONFunction.getJSONfromURL(“url”);
JSONObject集合=null;
试一试{
collection=jsonobject.getJSONObject(“collection”);
}
捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
JSONArray响应=null;
试一试{
response=collection.getJSONArray(“response”);
}
捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
对于(int i=0;i
事实上,当您试图在互联网不可用的情况下通过url访问json时,因此名为response的json数组中没有任何项,因此它得到了Null,而当您尝试执行给定json数组长度的For循环时,由于Null指针异常,它将崩溃


您的应用程序正在for循环中崩溃,对吗?

请发布您的logcatremove onpostresult(null),请将这三行拆分为单独的句子。我不是本地人,但我几乎不知道你在说什么。只是当互联网不可用时,其名为响应的Json数组包含Null OK,所以请编辑你的答案,以便理解。(顺便说一句:英语是一种只写大写字母名称的语言…)驼色大小写有点令人困惑和恼火…只是当互联网不可用时,它的json数组的名称响应包含null。OK似乎你不明白我的意思:请更新你的答案。我指的是在你的化身形象上没有任何意义的3行组合词。我不是指评论,我也不想让你在评论中重复你自己。再次提醒:请更新并改进答案中的文字!非常感谢。