我有一个名为HomeFragment.java的片段
代码如下: -
public class HomeFragment extends Fragment {
public HomeFragment(){}
String username, id;
ListView t1;
int idno;
JSONArray jsonArray;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Intent intent = getActivity().getIntent();
t1 = (ListView) getActivity().findViewById(R.id.getInfo);
if(intent.getStringExtra("username") == null)
{
Intent intent1 = new Intent(getActivity(),LoginActivity.class);
startActivity(intent1);
}
else
{
username = intent.getStringExtra("username");
id = intent.getStringExtra("id");
idno = Integer.parseInt(id);
new getDetails().execute(new ApiConnector());
}
return rootView;
}
public void setListAdaptor(JSONArray jsonArray)
{
this.jsonArray = jsonArray;
t1.setAdapter(new GetAllInfoListView(jsonArray,getActivity()));
}
private class getDetails extends AsyncTask<ApiConnector,Long,JSONArray> {
@Override
protected JSONArray doInBackground(ApiConnector... params) {
return params[0].getDetails(idno);
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
// TODO Auto-generated method stub
//setListAdaptor(jsonArray);
}
}
}
没有编译错误。
但是当我运行它时,我得到一个NullPointerException - &gt; t1.setAdapter(new GetAllInfoListView(jsonArray,getActivity()));
这是我的List Adapter类: -
public class GetAllInfoListView extends BaseAdapter {
private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflator = null;
public GetAllInfoListView(JSONArray jsonArray, Activity a)
{
this.dataArray = jsonArray;
this.activity = a;
inflator = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataArray.length();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListCell cell;
if(convertView == null)
{
convertView = inflator.inflate(R.layout.cell_layout, null);
cell = new ListCell();
cell.name = (TextView) convertView.findViewById(R.id.txt_name);
cell.age = (TextView) convertView.findViewById(R.id.txt_age);
cell.city = (TextView) convertView.findViewById(R.id.txt_city);
cell.airline = (ImageView) convertView.findViewById(R.id.airline_pick);
cell.dep_time = (TextView) convertView.findViewById(R.id.dep_time);
convertView.setTag(cell);
}
else
{
cell = (ListCell) convertView.getTag();
}
try {
JSONObject jsonObject = this.dataArray.getJSONObject(position);
cell.name.setText(jsonObject.getString("BKTBOARDCITY")+" - "+jsonObject.getString("BKTOFFCITY"));
cell.age.setText(jsonObject.getString("BKTAIRLINE"));
cell.city.setText(jsonObject.getString("BKTDEPDATE").substring(11));
cell.dep_time.setText(jsonObject.getString("BKTDEPDATE").substring(0,11));
String image = jsonObject.getString("BKTAIRLINE");
if(image.equals("Air India"))
{
cell.airline.setImageResource(R.drawable.airindia);
}
if(image.equals("Air Costa"))
{
cell.airline.setImageResource(R.drawable.aircosta);
}
if(image.equals("Go Airways"))
{
cell.airline.setImageResource(R.drawable.goairways);
}
if(image.equals("Indian Airlines"))
{
cell.airline.setImageResource(R.drawable.indianairline);
}
if(image.equals("Indigo"))
{
cell.airline.setImageResource(R.drawable.indigo);
}
if(image.equals("Jet Airways"))
{
cell.airline.setImageResource(R.drawable.jetairways);
}
if(image.equals("Jet Lite"))
{
cell.airline.setImageResource(R.drawable.jetlite);
}
if(image.equals("Kingfisher Airlines"))
{
cell.airline.setImageResource(R.drawable.kingfisher);
}
if(image.equals("Paramount Airways"))
{
cell.airline.setImageResource(R.drawable.paramount);
}
if(image.equals("Spice Jet"))
{
cell.airline.setImageResource(R.drawable.spicejet);
}
if(image.equals("United Airlines"))
{
cell.airline.setImageResource(R.drawable.unitedairlines);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
private class ListCell
{
private TextView name;
private TextView age;
private TextView city;
private ImageView airline;
private TextView dep_time;
}
}
请帮忙。谢谢:D
答案 0 :(得分:1)
我认为这是错误的
t1 = (ListView) getActivity().findViewById(R.id.getInfo);
像这样使用
t1 = (ListView) rootView.findViewById(R.id.getInfo);