在空对象引用上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'

时间:2016-01-14 03:44:47

标签: android listview fragment

嗨,我在解决此错误方面遇到了困难

this is the error enter image description here

我在片段中填写列表视图时出现问题。我的片段是一个标签,我假设这可能是我的问题所在。

我希望你能帮助我 谢谢你们

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


/**
  * Created by Mark Angelo on 1/13/2016.
 */  
public class ListFragment extends Fragment {


TextView code,name,quantity,total;
ListView lv;
List<List_Items> myList = new ArrayList<List_Items>();


public ListFragment(){

}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);


}



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,       Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.list_fragment_layout,container,false);

    populateList();
    populateListView();


    //lv = (ListView)getActivity().findViewById(R.id.client_listView);

    return view;
}


private void populateList() {

    myList.add(new List_Items("19912","Mark Angelo Bajaro", "1", "1993"));
    myList.add(new List_Items("12812","Jonas Zaldua","3","18841"));
    myList.add(new List_Items("2323", "Thor Sevilla", "1", "12121"));

}

private void populateListView() {

    ArrayAdapter<List_Items> adapter = new MyListAdapter();
    ListView list =  (ListView)getActivity().findViewById(R.id.client_listView);
    list.setAdapter(adapter);
}



private class MyListAdapter extends ArrayAdapter<List_Items> {
    public MyListAdapter() {super(getActivity(), R.layout.list_content_layout, myList); }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemview = convertView;
        if(itemview == null){
            itemview = getActivity().getLayoutInflater().inflate(R.layout.list_content_layout,parent,false);
        }

        List_Items currentlist = myList.get(position);

        code = (TextView)itemview.findViewById(R.id.code_contentent);
        name = (TextView)itemview.findViewById(R.id.name_content);
        quantity = (TextView)itemview.findViewById(R.id.quantity_content);
        total = (TextView)itemview.findViewById(R.id.total_content);

        code.setText(currentlist.getCode());
        name.setText(currentlist.getClient_name());
        quantity.setText(currentlist.getQuantity());
        total.setText(currentlist.getTotal());


        return itemview;
    }


}

}

2 个答案:

答案 0 :(得分:3)

您的ListView是否在片段布局中?或者它是在活动布局中。

如果它在片段布局中,请尝试更改

ListView list =  (ListView)getActivity().findViewById(R.id.client_listView);

ListView list =  (ListView) view.findViewById(R.id.client_listView);

希望能解决它。

答案 1 :(得分:0)

当你试图调用一些尚未初始化的函数时发生NullPointerException,你刚刚声明,如果你通过xml布局的引用初始化或者初始化

,请尝试查看id是否匹配