自定义列表视图调用时Android App停止

时间:2016-04-24 13:38:17

标签: java android

我尝试使用基本适配器创建一个简单的自定义列表视图。当我运行应用程序时它停止了。当我用getView方法检查错误时。我多次尝试修复但没有工作。请帮我找到解决这个问题的方法。

我的自定义适配器类。

public class activity_one extends Activity {



  ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity1);

    list=(ListView)findViewById(R.id.listView2);
    list.setAdapter(new VivzAdapter(this));


}


class SingleRow{
    ListView list;
    String polices;
    String policenum;
    SingleRow(String polices, String policenum) {
        this.polices = polices;
        this.policenum=policenum;
    }



}
class VivzAdapter extends BaseAdapter {

    ArrayList<SingleRow> list;
    Context context;
    VivzAdapter(Context c){
        list=new ArrayList<SingleRow>();

        Resources res=c.getResources();
        String[] polices=res.getStringArray(R.array.polices);
        String[] policenum=res.getStringArray(R.array.policenum);
        for(int i=0;i<10;i++)
        {
            list.add(new SingleRow(polices[i], policenum[i]));
        }
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {


        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row= inflater.inflate(R.layout.listview_singlerow,viewGroup,false);
        TextView polices=(TextView) row.findViewById(R.id.org_name);
        TextView policenum=(TextView)row.findViewById(R.id.cn_num);

        SingleRow temp=list.get(i);

        polices.setText(temp.polices);
        policenum.setText(temp.policenum);

        return row;
    }
}}

自定义列表视图xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffffff">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:src="@drawable/hello_avator"
    android:layout_alignParentTop="true"
    android:layout_margin="10dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Company Name"
    android:id="@+id/org_name"
    android:layout_alignTop="@+id/imageView2"
    android:layout_toRightOf="@+id/imageView2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="000-0000000"
    android:id="@+id/cn_num"
    android:layout_alignLeft="@+id/org_name"
    android:layout_alignStart="@+id/org_name"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/org_name"
    android:layout_alignBottom="@+id/imageView2" />

logcat的

java.lang.NullPointerException
        at com.example.abc.helloapp.activity_one$VivzAdapter.getView(activity_one.java:116)
        at android.widget.AbsListView.obtainView(AbsListView.java:2267)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
        at android.widget.ListView.onMeasure(ListView.java:1156)
        at android.view.View.measure(View.java:15172)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
        at android.view.View.measure(View.java:15172)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15172)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
        at android.view.View.measure(View.java:15172)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
        at android.view.View.measure(View.java:15172)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您错过了在适配器中初始化context变量 因此这条线因NPE而失败

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

因为context当然要解决此更新你的适配器构造函数使用这行context来初始化变量context = c; 所以代码应该是这样的:

VivzAdapter(Context c){
    list=new ArrayList<SingleRow>();
    context = c; // add this
    Resources res=c.getResources();
    String[] polices=res.getStringArray(R.array.polices);
    String[] policenum=res.getStringArray(R.array.policenum);
    for(int i=0;i<10;i++)
    {
        list.add(new SingleRow(polices[i], policenum[i]));
    }
}