English 中文(简体)
Android: listview: 自定义项目: 无效点例外, 查找视图返回无效
原标题:Android: listview: custom items: nullpointerexception, findviewbyid returns null

我一直在寻找和寻找 解决这个错误 一段时间,我似乎无法 找出原因和 如何解决这个问题。

我正在使用一个自定义的应用程序来填充我的列表视图 。 我将描述我的列表的 xml 放大 。 我用 FindViewById 来制作浏览器对象并装入我的文本视图 。 在此之后, 我想设置文本视图的文本, 但是它会发现 Viewbyid 返回一个无效值 。 所以自动解析成一个无效点 例外 。

适应器 :

/** CLASS : Custom Adapter for the listview */
private class CustomAdapter extends ArrayAdapter<Track>{
    private List<Track> items;
    private Context mContext;
    private int layoutResourceId;       
    private ViewHolder mHolder;

    public CustomAdapter(Context context, int layoutId, List<Track> objects) {
        super(context, layoutId, objects);
        layoutResourceId=layoutId;
        items = objects;
        mContext=context;       
    }       

    public View getView(int position, View convertView, ViewGroup parent) {
         if(convertView == null){
             LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView = inflater.inflate(layoutResourceId, parent, false);
             mHolder = new ViewHolder();
             ****** RETURNS NULL DURING DEBUGGING ******
             mHolder.textViewCenter = (TextView)findViewById(R.id.textview_list_item_central);
             convertView.setTag(mHolder);
         }else{
             mHolder = (ViewHolder) convertView.getTag();
         }
         Track track = items.get(position);
         ****** ERROR HAPPENS HERE ******
         mHolder.textViewCenter.setText(track.getTrack());
         return convertView;
    }

    class ViewHolder{
        ImageView imageView;
        TextView textViewCenter;
        TextView textViewRight;
    }
}

XML: (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑) (笑)

?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageview_list_albumart_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:src="@drawable/ic_music" />

<TextView
    android:id="@+id/textview_list_item_central"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0.5"
    android:text="Center"
    android:textSize="20sp" >
</TextView>

<TextView
    android:id="@+id/textview_list_item_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:paddingRight="10dip"
    android:text="Right" />

任何帮助都感激不尽!

最佳回答

搜索您在 convertView 中膨胀的 convertView 中的观点(而不是像您目前所做的那样在当前 Aactivity 布局中搜索 ) :

 mHolder.textViewCenter = (TextView) 
                convertView.findViewById(R.id.textview_list_item_central);
问题回答

您正在膨胀布局, 但取取它视图时不使用它 。

做到这一点 :

 mHolder.textViewCenter = (TextView)convertView.findViewById(R.id.textview_list_item_central)

TextView 初始化错误

if(convertView == null){
             LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView = inflater.inflate(layoutResourceId, parent, false);
             mHolder = new ViewHolder();
             ****** RETURNS NULL DURING DEBUGGING ******
             mHolder.textViewCenter = (TextView) convertView .findViewById(R.id.textview_list_item_central); //Change done
             convertView.setTag(mHolder);
         }else{
             mHolder = (ViewHolder) convertView.getTag();
         }

更改线条

mHolder.textViewCenter = (TextView)findViewById(R.id.textview_list_item_central);

which is below * RETURNS NULL DURING DEBUGGING * by

mHolder.textViewCenter = (TextView) convertView.findViewById(R.id.textview_list_item_central);

尝试替换此 :

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

where listitemview is your xml in which you defined your ImageView and TextViews..
hope this helps you





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...