在Android的空指针异常由FindViewById [英] Nullpointer Exception by FindViewById in Android
问题描述
今天,我开始开发自己的小闹钟应用程序。但现在我有一个问题,我搜索了三个小时,不可能解决这个问题。
today I started to develop my own little alarmclock app. But now I've got a problem, and I'm searching for three hours and couldn't solve it.
当我试图实现与定制设计的每一行的ListView,我得到一个NullPointerException异常,当我尝试设置在布局一个TextView的文本。这样做的原因是例外,那findViewById呼叫返回null,而不是我想有TextView的。
When I try to implement a listview with a custom design for each row, i get a NullPointerException, when I try to set the text from a textview in the layout. The reason for this exception is, that the findViewById-Call returns null and not the TextView I want to have.
在我的搜索,我发现这两个职位,但遗憾的是他们没有帮助非常好...
On my search I found these two posts, but unfortunately they didn't help very well...
POST1 - #1
<一个href=\"http://stackoverflow.com/questions/10699422/android-listview-custom-items-nullpointerexception-findviewbyid-returns-null\">Post2 - #1
我使用的是相同的基本结构在上述两个职位,但我无法发现其中的错误...
下面是我的应用程序的一些code:
I'm using the same basic structure as in the two post above, but I'm unable to find the mistake... Here is some code from my app:
该listitem_row.xml:
The 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,从那里我实例化的适配器
And at least the MainActivity, from where I instantiate the Adapter
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
Any help would be great... Thanks! :D
推荐答案
1)检查
holder.headText.setText(rowItem.getHeadText());
正好holder.headText返回null
exactly holder.headText return null
2)检查
if(convertView == null) {}
else {
holder = (ViewHolder) convertView.getTag();
}
执行的第一家分行。
executed first branch
3)通常我这样做:
// in constructor
inflater = LayoutInflater.from(context); // context from Activity
public View getView(int position, 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);
这篇关于在Android的空指针异常由FindViewById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!