开发者

Activity crashes on setAdapter(ArrayAdapter);

Attempting to write my first ArrayAdapter for Android, failing miserably at the moment. It crashes on the setAdapter(adapter); line and throws a 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.onCr开发者_Go百科eate(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;
        }

}

Contract:

public class Contract extends ContractTestActivity {

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

    public Contract() {

    }

    public String getName() {
        return name;
    }

}

Suggestions?


The problem is that you are not initializing your ArrayList

private ArrayList<Contract> contracts;

Its therefore you are getting the error 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;
    }
}


Problem is that your are not adding any values to your ArrayList contracts

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

Before this line you should add some values to contracts

So add values to List like this

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

Now Size of contracts will returns the Length of ArrayList.


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

is contracts not set at this point ?


private ArrayList contracts = ???

It should be assigned some value... or simply a null as below:

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));

If the exception persists... Then check the Id of the list view in xml.

This may be due to that only.


Create a listview in main.xml and a text view in 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);

}

In buttons click event

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

dataAdapter.notifyDataSetChanged();

so that when the button is clicked content of edittext will be added to list view

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜