活动在 setAdapter(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;
}
}
建议?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是您没有初始化 ArrayList
因此您收到错误 NullPointerException。
更新:
ContractTestActivity.java
Contract.java
ContractAdapter.java
The problem is that you are not initializing your ArrayList
Its therefore you are getting the error NullPointerException.
UPADATED:
ContractTestActivity.java
Contract.java
ContractAdapter.java
问题是您没有向 ArrayList 合约添加任何值。
在此行之前,您应该向 contracts 添加一些值,
因此向 List 添加值,如下所示。
现在,合约大小将返回 ArrayList 的长度。
Problem is that your are not adding any values to your ArrayList contracts
Before this line you should add some values to contracts
So add values to List like this
Now Size of contracts will returns the Length of ArrayList.
此时合同还没有确定吗?
is contracts not set at this point ?
私有 ArrayList 合约 = ???
应该为它分配一些值...或者简单地分配一个 null,如下所示:
contracts = new ArrayList
;如果异常仍然存在...然后检查 xml 中列表视图的 Id。
这或许只是因为这个原因。
private ArrayList contracts = ???
It should be assigned some value... or simply a null as below:
contracts = new ArrayList<Contract>
;If the exception persists... Then check the Id of the list view in xml.
This may be due to that only.
在main.xml中创建一个列表视图
以及 listview.xml 中的文本视图
在按钮单击事件中
,以便单击按钮时 edittext 的内容将添加到列表视图
Create a listview in main.xml
and a text view in listview.xml
In buttons click event
so that when the button is clicked content of edittext will be added to list view