活动在 setAdapter(ArrayAdapter) 上崩溃;

发布于 2024-12-05 17:18:09 字数 2248 浏览 0 评论 0原文

尝试为 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;
    }

}

建议?

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.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;
        }

}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜宝宝 2024-12-12 17:18:09

问题是您没有初始化 ArrayList

private ArrayList<Contract> contracts;

因此您收到错误 NullPointerException。

更新:

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

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;
    }
}
扭转时空 2024-12-12 17:18:09

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

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

在此行之前,您应该向 contracts 添加一些值,

因此向 List 添加值,如下所示。

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

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

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.

帅哥哥的热头脑 2024-12-12 17:18:09
ArrayAdapter<Contract> adapter = new ContractAdapter(this, android.R.layout.simple_list_item_1, contracts); 

此时合同还没有确定吗?

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

is contracts not set at this point ?

一瞬间的火花 2024-12-12 17:18:09

私有 ArrayList 合约 = ???

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

contracts = new ArrayList ;

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。

这或许只是因为这个原因。

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.

爱的十字路口 2024-12-12 17:18:09

在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 的内容将添加到列表视图

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文