我在Android App ListView中工作,在片段片段下工作,但我无法使其工作。我收到一条错误消息:
对于新的Runnable(){} ,未定义setListAdapter方法(ListAdapter)
我不知道我需要改变什么才能让它发挥作用。
public class FragmentA extends Fragment {
private String url = "http://192.168.2.150/54_tower/Json_Tower.php";
private static final String J_NAME = "t_name";
private static final String J_NUM = "t_num";
private static final String J_FLOOR = "t_floor";
private static final String J_CLASS = "t_room_class";
private static final String J_TEACHER = "t_room_teacher";
private static final String J_TOILET = "t_toilet";
private static final String J_MAP = "t_map";
private static final String D_NAME = "NAME";
private static final String D_NUM = "NUM";
private static final String D_FLOOR = "FLOOR";
private static final String D_CLASS = "CLASS";
private static final String D_TEACHER = "TEACHER";
private static final String D_TOILET = "TOILET";
private static final String D_MAP = "MAP";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
Activity act;
ListView ListView1;
JSONArray products = null;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> MyArrList;
public static FragmentA newInstance(int page, String title) {
FragmentA fragmentFirst = new FragmentA();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_tower, container, false);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
MyArrList = new ArrayList<HashMap<String, String>>();
new LoadAllData().execute();
final ListView lisView1 = (ListView) rootView
.findViewById(R.id.listView1);
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(getActivity(), Map.class);
startActivity(i);
}
});
return rootView;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
// Intent intent = act.getIntent();
// act.finish();
// startActivity(intent);
}
}
class LoadAllData extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url, "GET", params);
Log.d("All JSon: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
HashMap<String, String> map = new HashMap<String, String>();
// Log.d("TAG JSON",""+data.toString());
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// adding each child node to HashMap key => value
map = new HashMap<String, String>();
map.put(D_NAME, c.getString(J_NAME));
map.put(D_NUM, c.getString(J_NUM));
map.put(D_FLOOR, c.getString(J_FLOOR));
map.put(D_CLASS, c.getString(J_CLASS));
map.put(D_TEACHER, c.getString(J_TEACHER));
map.put(D_TOILET, c.getString(J_TOILET));
map.put(D_MAP, c.getString(J_MAP));
MyArrList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
// Intent i = new Intent(getApplicationContext(),
// NewProductActivity.class);
// Closing all previous activities
// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
final ListView myList = (ListView) getView().findViewById(
R.id.listView1);
act.runOnUiThread(new Runnable() {
public void run() {
ListAdapter sAdap = new SimpleAdapter(getActivity(),
MyArrList, R.layout.tower_column, new String[] {
D_NAME, D_NUM, D_FLOOR, D_CLASS, D_TEACHER,
D_TOILET, D_MAP }, new int[] { R.id.Name,
R.id.Num, R.id.Floor, R.id.Room_Class,
R.id.Room_Teacher, R.id.Toilet });
myList.setAdapter(sAdap);
}
});
}
}
}
这是LogCat:Image LogCat
答案 0 :(得分:0)
自从您在UI线程上启动任务后,您无需在UI线程onPostExecute上发布。删除runOnUIThread
,它应该可以。
答案 1 :(得分:0)
而不是runOnUIThread只需创建一个Handler并将此runnable发布到该处理程序。
还将listview创建为此类的字段,并在onCreateView或onActivityCreated中获取它的引用。我认为它导致nullPointerException。重要的是你应该将Listview作为片段类的字段,而不是将它放在方法的本地范围内。