Android 在AsyncTask类中设置适配器
我有一个扩展片段并调用异步任务类的代码。但我不能设定收养者。我面对错误。我在下面添加了片段和错误代码: 设置适配器时,此错误在此行中:Android 在AsyncTask类中设置适配器,android,android-asynctask,Android,Android Asynctask,我有一个扩展片段并调用异步任务类的代码。但我不能设定收养者。我面对错误。我在下面添加了片段和错误代码: 设置适配器时,此错误在此行中: lv.setAdapter(adapter); 下面是扩展片段的完整代码: public class AllProductsActivity extends Fragment { private RadioGroup radioGroup; private RadioButton radioSex
lv.setAdapter(adapter);
下面是扩展片段的完整代码:
public class AllProductsActivity extends Fragment {
private RadioGroup radioGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
ListView lv;
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "http://192.168.1.108/android_connect/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
// products JSONArray
JSONArray products = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.all_products, container, false);
productsList = new ArrayList<HashMap<String, String>>();
LoadAllProducts downloadimage = new LoadAllProducts();
lv = (ListView) rootView.findViewById(R.id.list);
downloadimage.execute();
return rootView;
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
MainCustomList adapter;
// lv = (ListView) getView().findViewById(R.id.list);
String[] test = new String[] { "1", "2", "3" };
adapter = new MainCustomList(getActivity(), test);
lv.setAdapter(adapter);
// }
// });
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ListView) view.findViewById(R.id.list);}
}
在这里:
因为lv
为null
而导致NPE
的行
ListView
withlist
位于片段内部,而不是片段容器Activity
使用片段的getView方法而不是getActivity
从片段布局访问ListView:
lv = (ListView) getView().findViewById(R.id.list);
或者最好使用片段的onViewCreated
或onCreateView
方法初始化视图:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ListView) view.findViewById(R.id.list);
}
我也使用getview,但不使用help。@user5508330:然后使用创建的视图的
onviewCreate
method这行代码是:lv.setAdapter(adapter);我在ViewCreated上尝试过这个,但没有帮助。谢谢。是的,我的问题是我必须在两个不同的布局中列出具有相同名称的视图。请发布错误日志。第330行代码是什么。此行:lv.setAdapter(adapter);为什么要投反对票。我确实有问题。您是否在onCreateView中返回了根视图。
lv = (ListView) getView().findViewById(R.id.list);
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ListView) view.findViewById(R.id.list);
}