我试图从Android应用程序调用Web服务方法,但我得到错误java.lang.RuntimeException错误发生执行doInBackground()和应用程序停止我已经看到很多关于此问题,但他们通过添加互联网权限解决了清单文件,但它没有工作
这是我的代码
package com.yeftaandrea.plesirsolo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.yeftaandrea.plesirsolo.adapter.JsonAttractionAdapter;
public class KaranganyarAttractionFragment extends Fragment {
// URL to get contacts JSON
// JSON Node names
public static final String TAG_ID = "ID";
public static final String TAG_NAME = "Name";
public static final String TAG_ADDRESS = "Address";
public static final String TAG_PHONE = "Phone";
public static final String TAG_PICPATH = "PicPath";
// Hashmap for ListView
ArrayList<HashMap<String, String>> attractionList;
ListView list;
JsonAttractionAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_attraction_karanganyar, container, false);
list = (ListView) rootView.findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
});
// Calling async task to get json
new LoadTask().execute();
return rootView;
}
private void loadAttraction(){
attractionList = new
ArrayList<HashMap<String, String>>();
JSONParser parser = new JSONParser();
List<NameValuePair> params = new ArrayList<NameValuePair>();
try {
JSONArray attraction = parser.makeHttpRequestArray("http://plesirsolo.meximas.com/plesir/attraction/wisata/getWisata/id/1", "GET", params);
// JSONArray attraction = parser.makeHttpRequestArray("http://192.168.56.1:8080/examples/json.html", "GET", params);
// looping through All messages
for (int i = 0; i < attraction.length(); i++) {
JSONObject c = attraction.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String nama = c.getString(TAG_NAME);
String address = c.getString(TAG_ADDRESS);
String phone = c.getString(TAG_PHONE);
String picPath = c.getString(TAG_PICPATH);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, nama);
map.put(TAG_ADDRESS, address);
map.put(TAG_PHONE, phone);
map.put(TAG_PICPATH, picPath);
// adding HashList to ArrayList
attractionList.add(map);
}
adapter = new JsonAttractionAdapter(
getActivity(), attractionList);
} catch (JSONException e) {
e.printStackTrace();
}
}
private class LoadTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
loadAttraction();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
list.setAdapter(adapter);
Log.i("JSON", "TEST");
Log.i("JSON", attractionList.toString());
}
}
}
和logcat
06-02 09:35:33.245: E/AndroidRuntime(1322): FATAL EXCEPTION: AsyncTask #2
06-02 09:35:33.245: E/AndroidRuntime(1322): java.lang.RuntimeException: An error occured while executing doInBackground()
06-02 09:35:33.245: E/AndroidRuntime(1322): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-02 09:35:33.245: E/AndroidRuntime(1322): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-02 09:35:33.245: E/AndroidRuntime(1322): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-02 09:35:33.245: E/AndroidRuntime(1322): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-02 09:35:33.245: E/AndroidRuntime(1322): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-02 09:35:33.245: E/AndroidRuntime(1322): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-02 09:35:33.245: E/AndroidRuntime(1322): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-02 09:35:33.245: E/AndroidRuntime(1322): at java.lang.Thread.run(Thread.java:841)
06-02 09:35:33.245: E/AndroidRuntime(1322): Caused by: java.lang.NullPointerException
06-02 09:35:33.245: E/AndroidRuntime(1322): at com.yeftaandrea.plesirsolo.KaranganyarAttractionFragment.loadAttraction(KaranganyarAttractionFragment.java:74)
06-02 09:35:33.245: E/AndroidRuntime(1322): at com.yeftaandrea.plesirsolo.KaranganyarAttractionFragment.access$0(KaranganyarAttractionFragment.java:63)
06-02 09:35:33.245: E/AndroidRuntime(1322): at com.yeftaandrea.plesirsolo.KaranganyarAttractionFragment$LoadTask.doInBackground(KaranganyarAttractionFragment.java:110)
06-02 09:35:33.245: E/AndroidRuntime(1322): at com.yeftaandrea.plesirsolo.KaranganyarAttractionFragment$LoadTask.doInBackground(KaranganyarAttractionFragment.java:1)
06-02 09:35:33.245: E/AndroidRuntime(1322): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-02 09:35:33.245: E/AndroidRuntime(1322): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-02 09:35:33.245: E/AndroidRuntime(1322): ... 4 more
我需要你的帮助,谢谢你
答案 0 :(得分:2)
在执行for循环之前检查吸引力!= null
JSONArray attraction = parser.makeHttpRequestArray("http://plesirsolo.meximas.com/plesir/attraction/wisata/getWisata/id/1", "GET", params);
if(attraction!=null){
// do something
}
答案 1 :(得分:1)
您的变量atraction
似乎是null
,因为此次调用
JSONArray attraction = parser.makeHttpRequestArray("http://plesirsolo.meximas.com/plesir/attraction/wisata/getWisata/id/1", "GET", params);
正在返回null
,稍后您将尝试在
for (int i = 0; i < attraction.length(); i++) {
JSONObject c = attraction.getJSONObject(i);
在访问对象attraction
之前检查它是否已初始化,或者null
是否输入loop
。您还应该阅读:What is a NullPointerException, and how do I fix it?