今天,我开始开发自己的小闹钟应用程序。但是现在我遇到了一个问题,我正在搜索三个小时而无法解决。
[当我尝试通过每行的自定义设计实现列表视图时,当我尝试从布局中的textview设置文本时,会收到NullPointerException。发生此异常的原因是,findViewById-Call返回null而不是我想要的TextView。
在我的搜索中,我发现了这两个帖子,但不幸的是,它们并没有很好地帮助您...
我使用的是与上述两篇文章相同的基本结构,但我找不到错误...这是我的应用程序中的一些代码:
listitem_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:name="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
<TextView
android:name="@+id/bottomtext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
我的适配器:
public class AlarmClockArrayAdapter extends ArrayAdapter<RowItem> {
private class ViewHolder {
TextView headText;
TextView subText;
}
public AlarmClockArrayAdapter(Context context, int layoutId, List<RowItem> objects) {
super(context, layoutId, objects);
this.mContext = context;
this.layoutResourceId = layoutId;
this.mListItems = objects;
}
/** getView()
*
* ListView asks the Adapter for a view for each list item element
* Override the getView()-Method to customize the ListView
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
// findViewById(...) returns Null
holder.headText = (TextView) convertView.findViewById(R.id.toptext);
holder.subText = (TextView) convertView.findViewById(R.id.bottomtext);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
// HERES THE NULLPOINTEREXCEPTION
holder.headText.setText(rowItem.getHeadText());
holder.subText.setText(rowItem.getSubText());
return convertView;
}
至少是MainActivity,我从那里实例化适配器
public class MainActivity extends Activity {
public void initList() {
//Adapter for the listview
AlarmClockArrayAdapter mAdapter = new AlarmClockArrayAdapter(this, R.layout.listitem_row, this.mListItems);
final ListView mListView = (ListView) findViewById(R.id.listView1);
mListView.setAdapter(mAdapter);
}
任何帮助都会很棒...谢谢! :D
[1)签到
holder.headText.setText(rowItem.getHeadText());
确切地是holder.headText返回null
[2)签到
if(convertView == null) {}
else {
holder = (ViewHolder) convertView.getTag();
}
已执行的第一分支
3)通常我喜欢这样:
// in constructor
inflater = LayoutInflater.from(context); // context from Activity
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listitem_row, null);
}
headText = (TextView) convertView.findViewById(R.id.toptext);
subText = (TextView) convertView.findViewById(R.id.bottomtext);
尝试在没有父母的情况下扩大布局
使用此:
convertView = inflater.inflate(layoutResourceId, null);
代替此:
convertView = inflater.inflate(layoutResourceId, parent, false);
编辑1
如果这没有帮助,请尝试通过getView()中的id来增加布局。>
convertView = inflater.inflate(R.layout.listitem_row, null);