因为我是android的新手 我的片段类如下 -
/**
* A simple {@link Fragment} subclass.
*/
public class DeviceListFragment extends android.support.v4.app.Fragment {
//Initialize of device list and device adapter
private ListView listview;
private ArrayList<DeviceList> deviceLists;
String deviceUrl;
public DeviceListFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_geo_fence, container, false);
listview = (ListView) view.findViewById(R.id.device_list);
MyLiveTracking activity = (MyLiveTracking) getActivity();
String username = activity.getUserNameData();
String token = activity.getTokenData();
String privateKeyData = activity.getPrivateKeyData();
deviceLists = new ArrayList<DeviceList>();
deviceUrl = "https://agdtrack.herokuapp.com/api/getdevices/"+token+"/"+privateKeyData;
new JSONAsyncTask().execute(deviceUrl);
DeviceListAdapter deviceListAdapter = new DeviceListAdapter(getActivity().getApplicationContext(), R.layout.device_row, deviceLists);
listview.setAdapter(deviceListAdapter);
// listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//@Override
// public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {
// TODO Auto-generated method stub
// Toast.makeText(getActivity(), deviceLists.get(position).getCarImei(), Toast.LENGTH_LONG).show();
// Toast.makeText(getActivity(), deviceLists.get(position).getCarModel(), Toast.LENGTH_LONG).show();
// }
// });
return view;
}
private void populateList() {
}
//Get user's Devices list form live server
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
//Call Json Parser to get Json data
JSONParser jsonParser = new JSONParser();
JSONArray json = jsonParser.getJSONFromUrl(urls[0]);
//System.out.println(json);
try {
for (int i=0; i<json.length(); i++) {
JSONObject object = json.getJSONObject(i);
DeviceList deviceList = new DeviceList();
deviceList.setCarName(object.getString("_id"));
deviceList.setCarModel(object.getString("hardware_model"));
deviceList.setCarImei(object.getString("imei"));
deviceList.setUserId(object.getString("users_id"));
deviceLists.add(deviceList);
}
return true;
}catch (Exception e){ e.printStackTrace();}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
//deviceListAdapter.notifyDataSetChanged();
if(result == false) {
Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDetach() {
super.onDetach();
}
}
我的适配器类位于 -
之下/**
* Created by tanvinn on 11/15/2015.
*/
public class DeviceListAdapter extends ArrayAdapter<DeviceList> {
ArrayList<DeviceList> deviceLists;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public DeviceListAdapter(Context context, int resource, ArrayList<DeviceList> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
deviceLists = objects;
}
static class ViewHolder {
public TextView tvCarName;
public TextView tvCarModel;
public TextView tvImei;
public TextView tvUserId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.tvCarName = (TextView) v.findViewById(R.id.tvCarName);
holder.tvCarModel = (TextView) v.findViewById(R.id.tvCarModel);
holder.tvImei = (TextView) v.findViewById(R.id.tvImei);
holder.tvUserId = (TextView) v.findViewById(R.id.tvUserId);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.tvCarName.setText(deviceLists.get(position).getCarName());
holder.tvCarModel.setText(deviceLists.get(position).getCarModel());
holder.tvImei.setText(deviceLists.get(position).getCarImei());
holder.tvUserId.setText(deviceLists.get(position).getUserId());
return v;
}
}
这是我的DeviceList
/**
* Created by tanvinn on 11/15/2015.
*/
public class DeviceList {
private String carname;
private String carmodel;
private String carimei;
private String userid;
public DeviceList() {
// TODO Auto-generated constructor stub
}
public DeviceList(String carname, String carmodel, String carimei, String userid) {
super();
this.carname = carname;
this.carmodel = carmodel;
this.carimei = carimei;
this.userid = userid;
}
public String getCarName() {
return carname;
}
public void setCarName(String carname) {
this.carname = carname;
}
public String getCarModel() {
return carmodel;
}
public void setCarModel(String carmodel) {
this.carmodel = carmodel;
}
public String getCarImei() {
return carimei;
}
public void setCarImei(String carimei) {
this.carimei = carimei;
}
public String getUserId() {
return userid;
}
public void setUserId(String userid) {
this.userid = userid;
}
}
我得到的错误如下 -
9910-9910/bd.com.agdits.agdittracking E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: bd.com.agdits.agdittracking, PID: 9910
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at bd.com.agdits.agdittracking.DeviceListFragment.onCreateView(DeviceListFragment.java:70)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:948)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)