在android中创建自定义列表视图

时间:2017-06-07 23:13:38

标签: android

我想为android studio中的列表视图设置自定义布局

我在activity_main.xml文件中添加了一个列表视图

<ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

并创建一个名为row.xml的布局文件

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

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/icon"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium"
            android:id="@+id/text1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="small"
            android:id="@+id/text2"/>
    </LinearLayout>

</LinearLayout>

和MainActivity.java文件包括:

公共类MainActivity扩展了AppCompatActivity {

ListView list;
String [] titles;
String [] description;
int[]imgs = {R.drawable.fb,R.drawable.chat,R.drawable.gplus,R.drawable.instagram};

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

    Resources res = getResources();

    titles = res.getStringArray(R.array.titles);
    description = res.getStringArray(R.array.description);

    list = (ListView) findViewById(R.id.list);
    MyAdapter adapter = new MyAdapter(this,titles,imgs,description);
    list.setAdapter(adapter);

}
class MyAdapter extends ArrayAdapter{
    Context context;
    int[] imgs;
    String[] myTitles;
    String[] myDescription;

    MyAdapter (Context c , String[] titles , int[] imgs , String[]Description){
        super(c,R.layout.row,R.id.text1,titles);
        this.context = c;
        this.imgs = imgs;
        this.myDescription = description;
        this.myTitles = titles;
    }
    @Override
    public View getView(int position , View convertView , ViewGroup parent){
        LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = layoutInflater.inflate(R.layout.row,null,true);
        ImageView images = (ImageView) findViewById(R.id.icon);
        TextView myTitle = (TextView) findViewById(R.id.text1);
        TextView myDescription = (TextView) findViewById(R.id.text2);
        images.setImageResource(imgs[position]);
        myTitle.setText(titles[position]);
        myDescription.setText(description[position]);
        return row;
    }
}

}

当我运行应用程序时,显示:不幸的是,应用程序已停止。

如何解决这个问题?

错误日志:

AndroidRuntime: FATAL EXCEPTION: main
                                                                               java.lang.NullPointerException
                                                                                   at com.example.nik.myapplication.MainActivity$MyAdapter.getView(MainActivity.java:57)
                                                                                   at android.widget.AbsListView.obtainView(AbsListView.java:2207)
                                                                                   at android.widget.ListView.makeAndAddView(ListView.java:1845)
                                                                                   at android.widget.ListView.fillDown(ListView.java:678)
                                                                                   at android.widget.ListView.fillFromTop(ListView.java:739)
                                                                                   at android.widget.ListView.layoutChildren(ListView.java:1661)
                                                                                   at android.widget.AbsListView.onLayout(AbsListView.java:2037)
                                                                                   at android.view.View.layout(View.java:14118)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4474)
                                                                                   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:985)
                                                                                   at android.view.View.layout(View.java:14118)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4474)
                                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                                                                                   at android.view.View.layout(View.java:14118)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4474)
                                                                                   at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                                                                                   at android.view.View.layout(View.java:14118)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4474)
                                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                                                                                   at android.view.View.layout(View.java:14118)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4474)
                                                                                   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
                                                                                   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
                                                                                   at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
                                                                                   at android.view.View.layout(View.java:14118)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4474)
                                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
                                                                                   at android.view.View.layout(View.java:14118)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4474)
                                                                                   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
                                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1947)
                                                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
                                                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
                                                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
                                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:579)
                                                                                   at android.view.Choreographer.doFrame(Choreographer.java:548)
                                                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
                                                                                   at android.os.Handler.handleCallback(Handler.java:725)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                   at android.os.Looper.loop(Looper.java:153)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5341)
                                                                                   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:929)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
                                                                                   at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

这是你的答案:

@Override
public View getView(int position , View convertView , ViewGroup parent){
    LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = layoutInflater.inflate(R.layout.row,null,true);
    ImageView images = (ImageView) row.findViewById(R.id.icon);
    TextView myTitle = (TextView) row.findViewById(R.id.text1);
    TextView myDescription = (TextView) row.findViewById(R.id.text2);
    images.setImageResource(imgs[position]);
    myTitle.setText(titles[position]);
    myDescription.setText(description[position]);
    return row;
}

这就是为什么这是你的答案:

你有一个空指针异常,因为你在你的活动上调用了findViewById而不是你刚刚膨胀的视图。由于活动没有包含ID图标,text1或text2的字段,因此您无法获得任何结果。当您尝试设置它们时,它会抛出异常。

希望有所帮助!