自定义列表视图中的LayoutInflater中的空指针异常

时间:2014-07-31 07:30:42

标签: java android android-listview baseadapter

我是android的初学者。我收到了NullPointerEcxeption错误。

我希望我的活动让我获得listview,每个listview包含图像,它的标题,以及它的描述, 我读过文章this

我无法正确理解MainActivity.java LayoutInflater中的上下文会发生什么......

我使用Base Adapter而不是ArrayAdapter。

请帮助..提前致谢。

这里我放了一些代码......

一个imageview,两个textviews CustomeList.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="fill_parent" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/imageView1"
    android:text="Descript1" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/img2" />

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Title1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 </RelativeLayout>

activity_main.xml,其中包含Listview ..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
  </ListView>

</RelativeLayout>

MainActivity.java

package com.example.customadapter;

public class MainActivity extends Activity {

ListView lv;
//Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv=(ListView) findViewById(R.id.listView1);
    lv.setAdapter(new myAdapter(this));
    //lv.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

class SingleRow {
   String title;
   String desc;
   int image;
   SingleRow(String title, String desc, int img){
    this.title=title;
    this.desc=desc;
    this.image=img;
}
}

class myAdapter extends BaseAdapter{
 Context context;
 ArrayList<SingleRow> list;
 myAdapter(Context c){

    list=new ArrayList<SingleRow>();
    /*
     * 1. get the title, descrp, image from xml
     * 2. group each title, its related description and its image into a single row     

object.
     * 3. put single row object into array list.
     * 
     * */
    Resources res=c.getResources();

    String[] title=res.getStringArray(R.array.titles);
    String[] desc=res.getStringArray(R.array.desc);

    int[] img=  

 {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5};

    for(int i=0;i<5;i++) {
        list.add(new SingleRow(title[i],desc[i],img[i]));
    }
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View row, ViewGroup parent) {
    // TODO Auto-generated method stub

    /*
     1. get the root view
     2. use the root view for find other view
     3. set the view.
     */

    LayoutInflater inflater=(LayoutInflater) 

    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row= inflater.inflate(R.layout.custom_list,null);

    TextView title= (TextView) row.findViewById(R.id.tv1);
    TextView desc=(TextView) row.findViewById(R.id.textView2);
    ImageView img=(ImageView) row.findViewById(R.id.imageView1);

    SingleRow temp=list.get(position);
    title.setText(temp.title);
    desc.setText(temp.desc);
    img.setImageResource(temp.image);

    return null;
}
}

错误::

07-30 18:34:29.487: E/AndroidRuntime(627): FATAL EXCEPTION: main
07-30 18:34:29.487: E/AndroidRuntime(627): java.lang.NullPointerException
07-30 18:34:29.487: E/AndroidRuntime(627):  at com.example.customadapter.myAdapter.getView(MainActivity.java:108)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.widget.AbsListView.obtainView(AbsListView.java:1430)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.widget.ListView.onMeasure(ListView.java:1127)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.view.View.measure(View.java:8313)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.view.View.measure(View.java:8313)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.view.View.measure(View.java:8313)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.view.View.measure(View.java:8313)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.view.View.measure(View.java:8313)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.os.Looper.loop(Looper.java:123)
07-30 18:34:29.487: E/AndroidRuntime(627):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-30 18:34:29.487: E/AndroidRuntime(627):  at java.lang.reflect.Method.invokeNative(Native Method)
07-30 18:34:29.487: E/AndroidRuntime(627):  at java.lang.reflect.Method.invoke(Method.java:507)
07-30 18:34:29.487: E/AndroidRuntime(627):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-30 18:34:29.487: E/AndroidRuntime(627):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-30 18:34:29.487: E/AndroidRuntime(627):  at dalvik.system.NativeStart.main(Native Method)
07-30 18:39:29.697: I/Process(627): Sending signal. PID: 627 SIG: 9

2 个答案:

答案 0 :(得分:1)

上下文变量被引用为null

在你的构造函数中你没有使用参数Context c

添加此行代码

context=c;

并且您正在使用getView()方法中的上下文,这也会导致空指针。

在自定义适配器中,getView()返回null,触发空指针异常。你需要退回膨胀的view;

替换

return null;

return row;

答案 1 :(得分:0)

您尚未正确使用“上下文”值。这就是你得到的错误。也返回视图。 以下代码可以帮助您。您也可以使用ViewHolder模式来提高代码效率。

公共类MyBaseAdapter扩展了BaseAdapter {

ArrayList<ListData> myList = new ArrayList<ListData>();
LayoutInflater inflater;
Context context;


public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
    this.myList = myList;
    this.context = context;
    inflater = LayoutInflater.from(this.context);
}

@Override
public int getCount() {
    return myList.size();
}

@Override
public ListData getItem(int position) {
    return myList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MyViewHolder mViewHolder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
        mViewHolder = new MyViewHolder(convertView);
        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (MyViewHolder) convertView.getTag();
    }

    ListData currentListData = getItem(position);

    mViewHolder.tvTitle.setText(currentListData.getTitle());
    mViewHolder.tvDesc.setText(currentListData.getDescription());
    mViewHolder.ivIcon.setImageResource(currentListData.getImgResId());

    return convertView;
}

private class MyViewHolder {
    TextView tvTitle, tvDesc;
    ImageView ivIcon;

    public MyViewHolder(View item) {
        tvTitle = (TextView) item.findViewById(R.id.tvTitle);
        tvDesc = (TextView) item.findViewById(R.id.tvDesc);
        ivIcon = (ImageView) item.findViewById(R.id.ivIcon);
    }
}