Php Listfragment和Asynctask错误
我对AsyncTask类有问题,并且无法在listview中打印php结果。谁能给我一个解决办法?为什么OnPostExecute错误 片段选项卡1Php Listfragment和Asynctask错误,php,android,android-asynctask,Php,Android,Android Asynctask,我对AsyncTask类有问题,并且无法在listview中打印php结果。谁能给我一个解决办法?为什么OnPostExecute错误 片段选项卡1 public class FragmentTab1 extends ListFragment { ArrayList<HashMap<String, String>> arraylist; JSONObject jsonobject; JSONArray jsonarray; ListViewAdapter adapter;
public class FragmentTab1 extends ListFragment {
ArrayList<HashMap<String, String>> arraylist;
JSONObject jsonobject;
JSONArray jsonarray;
ListViewAdapter adapter;
private static final String TAG="nombre";
private Context context;
private ArrayAdapter<String> arrayAdapter;
static String imagen = "imagen";
static String nombre = "nombre";
static String total = "total";
static String empresa = "empresa";
static String precionuevo = "precionuevo";
static String precioantiguo = "precioantiguo";
static String direccion = "direccion";
static String telefono = "telefono";
static String unidades = "unidades";
static String codeqr = "codeqr";
static String longitud = "longitud";
static String latitud = "latitud";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// normally you should inflate a view here and save references
// using ListFragment default layout for this example
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
new FetchFactTask().execute();
}
private class FetchFactTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>>{
@Override
protected void onPreExecute() {
// Any code to execute before fetching the data
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
// Create an array
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("productos");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("imagen", jsonobject.getString("imagen"));
map.put("nombre", jsonobject.getString("nombre"));
map.put("total", jsonobject.getString("total"));
map.put("empresa", jsonobject.getString("empresa"));
map.put("unidades", jsonobject.getString("unidades"));
map.put("precionuevo", jsonobject.getString("precionuevo")+" €");
map.put("precioantiguo", jsonobject.getString("precioantiguo")+" €");
map.put("descripcion", jsonobject.getString("descripcion"));
map.put("direccion", jsonobject.getString("direccion"));
map.put("telefono", jsonobject.getString("telefono"));
map.put("latitud", jsonobject.getString("latitud"));
map.put("longitud", jsonobject.getString("longitud"));
map.put("codeqr", jsonobject.getString("codeqr"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
//e.printStackTrace();
}
return arraylist;
}
protected void onPostExecute(ArrayList<HashMap<String, String>> args) {
//Use that args to populate the listview
for(int i=0;i<args.size();i++) {
Log.i("onPostExecute ->", args.get(i).get("nombre"));
Toast.makeText(getActivity(), args.get(i).get("nombre"), Toast.LENGTH_SHORT).show();
}
// Pass the results into ListViewAdapter.java
//adapter = new ListViewAdapter(getActivity(), arraylist);
/*ListAdapter adapter=new SimpleAdapter(context,
arraylist,
R.layout.fragmenttab1, new String[]{ TAG },
new int[]{R.id.list});
setListAdapter(adapter); */
ListView productList = (ListView) getActivity().findViewById(R.id.list);
adapter = new ListViewAdapter(getActivity(), args);
// Set the adapter to the ListView
productList.setAdapter(adapter);
}
}
}
公共类FragmentTab1扩展了ListFragment{
ArrayList ArrayList;
JSONObject JSONObject;
JSONArray JSONArray;
ListViewAdapter适配器;
私有静态最终字符串标记=“nombre”;
私人语境;
专用阵列适配器阵列适配器;
静态字符串imagen=“imagen”;
静态字符串nombre=“nombre”;
静态字符串total=“total”;
静态字符串empresa=“empresa”;
静态字符串precionuevo=“precionuevo”;
静态字符串precioantiguo=“precioantiguo”;
静态字符串direccion=“direccion”;
静态字符串telefono=“telefono”;
静态字符串unidades=“unidades”;
静态字符串codeqr=“codeqr”;
静态字符串longitud=“longitud”;
静态字符串latitud=“latitud”;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
//通常,您应该在此处膨胀视图并保存引用
//在本例中使用ListFragment默认布局
返回super.onCreateView(充气机、容器、savedInstanceState);
}
@凌驾
public void onStart(){
super.onStart();
新建FetchFactTask().execute();
}
私有类FetchFactTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
//获取数据之前要执行的任何代码
}
@凌驾
受保护的ArrayList doInBackground(字符串…参数){
//创建一个数组
ArrayList ArrayList=新的ArrayList();
//从给定的URL地址检索JSON对象
jsonobject=JSONfunctions
.getJSONfromURL(“http://”);
试一试{
//在JSON中找到数组名称
jsonarray=jsonobject.getJSONArray(“productos”);
for(int i=0;i 对于(int i=0;i,因为您使用的是列表片段
,以下行不起作用:
ListView productList = (ListView) getActivity().findViewById(R.id.list);
adapter = new ListViewAdapter(getActivity(), args);
// Set the adapter to the ListView
productList.setAdapter(adapter);
第一行导致NPE,因为活动不包含id为R.id.list
的ListView;该列表位于您的ListFragment
中。您应该将代码更改为:
adapter = new ListViewAdapter(getActivity(), args)
getListView().setAdapter(adapter);
FragmentTab1的第145行是什么?@NathanWalters第145行->productList.setAdapter(适配器);谢谢!已更改,不再显示任何错误,但不打印列表:(这是应用程序中的实际代码行吗?如果是,这将不会起任何作用,因为它不是一个有效的网址jsonobject=JSONfunctions.getJSONfromURL(“http://”);
在这一行中,我正确地输入了我的网址,并toast“toast.makeText(getActivity(),args.get(I).get(“nombre”),toast.LENGTH\u SHORT).show()”正确显示结果但不打印列表请发布承载此片段的活动的代码。这里是我的代码:
adapter = new ListViewAdapter(getActivity(), args)
getListView().setAdapter(adapter);