活动在setAdapter(ArrayAdapter)上崩溃;

时间:2011-09-20 17:48:52

标签: java android android-arrayadapter

尝试编写我的第一个Android ArrayAdapter,目前失败了。它在setAdapter(adapter);行崩溃并抛出NullPointerException。

ContractTestActivity:

public class ContractTestActivity extends Activity {

    private ArrayList<Contract> contracts;
    public final String TAG = "ContractTest";
    //public Contract newContract = new Contract();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        ListView list;
        list = (ListView)findViewById(R.id.list);
        ArrayAdapter<Contract> adapter = new ContractAdapter(this, android.R.layout.simple_list_item_1, contracts);
        list.setAdapter(adapter);
    }
}

ContractAdapter:

public class ContractAdapter extends ArrayAdapter<Contract> {


        private ArrayList<Contract> contracts;

        public ContractAdapter(Context context, int view, ArrayList<Contract> passedContracts) {
                super(context, view, passedContracts);
                contracts = passedContracts;
        }

        @Override
        public int getCount() {
            return contracts.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View currentView = convertView;
                LayoutInflater currentViewInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                currentView = currentViewInflater.inflate(android.R.layout.simple_list_item_1, null);
                Contract currentContract = contracts.get(position);
                TextView text = (TextView) currentView.findViewById(android.R.id.text1);
                text.setText(currentContract.getName());
                return currentView;
        }

}

合同:

public class Contract extends ContractTestActivity {

    private String name;
    private float payRate;
    private int hoursWorked;
    private int holidays;

    public Contract() {

    }

    public String getName() {
        return name;
    }

}

建议?

5 个答案:

答案 0 :(得分:3)

问题在于您没有初始化ArrayList

private ArrayList<Contract> contracts;

因此,您收到错误NullPointerException。

<强> UPADATED:

ContractTestActivity.java

public class ContractTestActivity extends Activity {
    private List<Contract> contracts = new ArrayList<Contract>();
    public final String TAG = "ContractTest";
    //public Contract newContract = new Contract();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        ListView list;
        list = (ListView)findViewById(R.id.mylist);
        ArrayAdapter<Contract> adapter = new ContractAdapter(this, android.R.layout.simple_list_item_1, myContracts());
        list.setAdapter(adapter);
    }

    private List<Contract> myContracts(){

        List<Contract> list = new ArrayList<Contract>();

        list.add(new Contract("Friend1"));
        list.add(new Contract("Friend2"));
        list.add(new Contract("Friend3"));
        list.add(new Contract("Friend4"));
        return list;
    }
}

<强> Contract.java

public class Contract {

    private String name;

    public Contract(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

<强> ContractAdapter.java

public class ContractAdapter extends ArrayAdapter<Contract> {


    private List<Contract> contracts;

    public ContractAdapter(Context context, int view, List<Contract> passedContracts) {
            super(context, view, passedContracts);
            contracts = passedContracts;
    }

    @Override
    public int getCount() {
        return contracts.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View currentView = convertView;
            LayoutInflater currentViewInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            currentView = currentViewInflater.inflate(android.R.layout.simple_list_item_1, null);
            Contract currentContract = contracts.get(position);
            TextView text = (TextView) currentView.findViewById(android.R.id.text1);
            text.setText(currentContract.getName());
            return currentView;
    }
}

答案 1 :(得分:2)

问题是您没有向ArrayList合约添加任何值

 ArrayAdapter<Contract> adapter = new ContractAdapter(this, android.R.layout.simple_list_item_1, contracts);

在此行之前,您应该为合同

添加一些值

因此,像这样添加值

 contracts = new ArrayList<Contract>();
 contracts.add(new Contract("name"));
 ArrayAdapter<Contract> adapter = new ContractAdapter(this,android.R.layout.simple_list_item_1, contracts);

现在契约的大小将返回ArrayList的长度。

答案 2 :(得分:0)

ArrayAdapter<Contract> adapter = new ContractAdapter(this, android.R.layout.simple_list_item_1, contracts); 

此时未设定合约?

答案 3 :(得分:0)

private ArrayList contracts = ???

应该为它分配一些值...或者只是一个null,如下所示:

contracts = new ArrayList<Contract>;

Also try to String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
                "Ubuntu", "Solaris", "Android", "iPhone"};
        // Create an ArrayAdapter, that will actually make the Strings above
        // appear in the ListView
        list.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, names));

如果异常仍然存在......那么请检查xml中列表视图的ID。

这可能仅限于此。

答案 4 :(得分:0)

在main.xml中创建一个列表视图 和listview.xml中的文本视图

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    t=(EditText)findViewById(R.id.editText1);
    lv=(ListView)findViewById(R.id.listView1);
    listItems = new ArrayList<String>();
    dataAdapter = new ArrayAdapter<String>(this, R.layout.listview,R.id.row);
    //row is id of textview
     lv.setAdapter(dataAdapter);

}

在按钮中单击事件

dataAdapter.add(t.getText().toString());

dataAdapter.notifyDataSetChanged();

这样当点击按钮时,edittext的内容将被添加到列表视图