Nullpointer Exception by FindViewById in Android

非 Y 不嫁゛ 提交于 2019-12-07 20:23:37

问题


Hey guys

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.

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 - Stackoverflow

Post2 - Stackoverflow

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:

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>

My Adapter:

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

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

Any help would be great... Thanks! :D


回答1:


1) check in

holder.headText.setText(rowItem.getHeadText());

exactly holder.headText return null

2) check in

if(convertView == null) {}
else {
    holder = (ViewHolder) convertView.getTag();
}

executed first branch

3) usually I do like this:

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



回答2:


Try to inflate layout without parent

Use this:

convertView = inflater.inflate(layoutResourceId, null);

Instead this:

convertView = inflater.inflate(layoutResourceId, parent, false);

Edit 1

If this not help, try to inflate layout by id in getView()

convertView = inflater.inflate(R.layout.listitem_row, null);


来源:https://stackoverflow.com/questions/12590342/nullpointer-exception-by-findviewbyid-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!