gpt4 book ai didi

java - SimpleAdapter 不起作用

转载 作者:行者123 更新时间:2023-12-01 08:03:51 28 4
gpt4 key购买 nike

行中的空指针异常:

   listView.setAdapter(sAdapter);
<小时/>
public class MyEmployeFragment extends Fragment {


MyTask task;
String employer_id;
String employer_name;
String str=null;
// имена атрибутов для Map
final String ATTRIBUTE_ID = "p_id";
final String ATTRIBUTE_NAME = "p_name";
ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

//myPrefs = this.getActivity().getSharedPreferences("MYsettings", Context.MODE_PRIVATE);
task = new MyTask();
task.execute();

// Creating view correspoding to the fragment
View v = inflater.inflate(R.layout.my_employe, container, false);

// Updating the action bar title
getActivity().getActionBar();
String[] userId = {"1","2","3"};

String[] userName = {"Nadezhd","Vasya", "hardcore"};

ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
userId.length);
Map<String, Object> m;
for (int i = 0; i < userId.length; i++) {
m = new HashMap<String, Object>();
m.put(ATTRIBUTE_ID, userId[i]);
m.put(ATTRIBUTE_NAME, userName[i]);
data.add(m);
}

// массив имен атрибутов, из которых будут читаться данные
String[] from = {ATTRIBUTE_ID, ATTRIBUTE_NAME};
// массив ID View-компонентов, в которые будут вставлять данные
int[] to = {R.id.tw_employe_id, R.id.tw_employe_name};

// создаем адаптер
SimpleAdapter sAdapter = new SimpleAdapter(getActivity(), data, R.layout.list_item_employee,
from, to);


// определяем список и присваиваем ему адаптер
listView = (ListView) getActivity().findViewById(R.id.lv_employee_list);


// ListView listView = (ListView) getView();
listView.setAdapter(sAdapter);
return v;
}



class MyTask extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {

super.onPreExecute();
}

@Override
protected Void doInBackground(Void... params) {

String s = "5ACACEC6-752B-4EFF-AA50-EEBE58A52113";
// String user_guid = myPrefs.getString("guid", "");

HttpActivity _http = new HttpActivity("192.168.10.11", "80");
_http.set_addr_protocol("/WebSite/P/spr/spr.aspx/");
_http.add_param("query", "spr_employee_get");
// _http.add_param("p_guid", user_guid.toString().trim());
_http.add_param("p_guid", s);
_http.send();

employer_name = _http.getArrayParamValue("p_name");
employer_id = _http.getArrayParamValue("p_id");


// массивы данных
//String[] userId = {employer_id};

//String[] userName = {employer_name};



return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);


}
}

最佳答案

正如@Little Child提到的,您的ListViewnull,问题是以下代码行:

listView = (ListView) getActivity().findViewById(R.id.lv_employee_list);

实际上应该是:

listView = (ListView) v.findViewById(R.id.lv_employee_list);

关于java - SimpleAdapter 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23141252/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com