java Activity 在 setAdapter(ArrayAdapter) 上崩溃;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7489454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:11:53  来源:igfitidea点击:

Activity crashes on setAdapter(ArrayAdapter);

javaandroidandroid-arrayadapter

提问by Fraser

Attempting to write my first ArrayAdapter for Android, failing miserably at the moment. It crashes on the setAdapter(adapter);line and throws a NullPointerException.

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

}

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?

建议?

采纳答案by Lalit Poptani

The problem is that you are not initializing your ArrayList

问题是你没有初始化你的 ArrayList

private ArrayList<Contract> contracts;

Its therefore you are getting the error NullPointerException.

因此,您收到错误 NullPointerException。

UPADATED:

更新:

ContractTestActivity.java

合约测试活动.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

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

回答by Venky

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

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

 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

所以像这样向 List 添加值

 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.

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

回答by greeshma

Create a listview in main.xml and a text view in listview.xml

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

}

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

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

回答by Arpit Garg

private ArrayList contracts = ???

私有 ArrayList 合同 = ???

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

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

contracts = new ArrayList<Contract>;

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.

如果异常仍然存在...然后检查 xml 中列表视图的 Id。

This may be due to that only.

这可能只是由于这一点。

回答by roman

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

is contracts not set at this point ?

合同不是在这一点上设定的吗?