我在将 JSON 数据对象绑定到 listView 片段时遇到问题。我在绑定到我的简单适配器的第 100 行得到一个 NPE。我尝试了各种方法来做到这一点,但都没有成功。根据文档:
您必须使用ListFragment.setListAdapter()将列表与适配器相关联。不要直接调用 ListView.setAdapter() 否则会跳过重要的初始化。
我确定这是我的问题,但我不知道如何纠正我的代码。下面是 NPE 和我的活动。请任何帮助将不胜感激,尤其是一些代码示例,因为我正在学习......
threadid=1:线程以未捕获的异常退出(组=0x40a781f8) 致命异常:com.andaero.app.NavigationListFragment$1.callback(NavigationListFragment 的 android.widget.SimpleAdapter.(SimpleAdapter.java:85) .java:100) 在 com.andaero.app.NavigationListFragment$1.callback(NavigationListFragment.java:1) 在 com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:440) 在 com.androidquery.callback.AbstractAjaxCallback.afterWork (AbstractAjaxCallback.java:1010) 在 com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:804) 在 android.os.Handler.handleCallback(Handler.java:605) 在 android.os.Handler.dispatchMessage(Handler. java:92) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:4424) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit. java:787) 在 dalvik.system.NativeStart.main(Native Method) 的 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
public class NavigationListFragment extends ListFragment {
Context context;
private Activity c;
final AQuery aq = new AQuery(c);
private static String url = "http://192.168.1.17/Andaero/php/regulatory_list.php";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.listview, container, false);
Log.i("NavigationListFragment", "ListView Inflated!!");
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// AsyncTasks.getJSONArrays(context);// asynchronous task for getting
// JSONarray
aq.ajax(url, JSONArray.class, new AjaxCallback<JSONArray>() {
// JSON Node names
private static final String TAG_ID = "_id";
private static final String TAG_LABEL = "label";
private static final String TAG_TITLE = "title";
private static final String TAG_DISCR = "description";
private static final String TAG_GO2URL = "gotoURL";
public void callback(String url, JSONArray json, AjaxStatus status) {
if (json != null) {
// NavigationListFragment.jsonListCallback(json);
Log.i("NavigationListFragment", "Caught JSON: " + json.toString());
// Hashmap for ListView
ArrayList<HashMap<String, String>> mList = new ArrayList<HashMap<String, String>>();
try {
// Parse the string to a JSON object
for (int i = 0; i < json.length(); i++) {
JSONObject json_data = json.getJSONObject(i);
// Storing each json item in variable
String id = json_data.getString(TAG_ID);
String label = json_data.getString(TAG_LABEL);
String title = json_data.getString(TAG_TITLE);
String description = json_data.getString(TAG_DISCR);
String gotoURL = json_data.getString(TAG_GO2URL);
// 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_LABEL, label);
map.put(TAG_TITLE, title);
map.put(TAG_DISCR, description);
map.put(TAG_GO2URL, gotoURL);
// adding HashList to ArrayList
mList.add(map);
Log.i("NavigationListFragment", "Hash: " + map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
// create the list item mapping
String[] from = new String[] {TAG_LABEL, TAG_TITLE, TAG_DISCR, TAG_GO2URL};
int[] to = new int[] { R.id.listLabel, R.id.listTitle, R.id.listDiscription, R.id.dummy };
// Updating parsed JSON data into ListView
SimpleAdapter adapter = new SimpleAdapter(c, mList, R.layout.list_item, from, to);//<--NPE IS HERE
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//TODO
}
});
}
// Log any network/JSON Errors
switch (status.getCode()) {
case AjaxStatus.TRANSFORM_ERROR :
Log.i("GetJSONArray", "TRANSFORM_ERROR");
break;
case AjaxStatus.NETWORK_ERROR :
Log.i("GetJSONArray", "NETWORK_ERROR");
// TODO Create Alert Dialog
case AjaxStatus.AUTH_ERROR :
Log.i("GetJSONArray", "AUTH_ERROR");
break;
}
}
});
}
}