简体   繁体   中英

NullPointerException in the Adapter for a Custom View

I have a problem with the following exception:

exception:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

I have an Adapter class:

public class SimpleAdapter extends BaseAdapter {
    private LayoutInflater lInflater;
    private String [] simpleValueList;

    public SimpleAdapter(Context context, String[] simpleValueList) {
        lInflater = LayoutInflater.from(context);
        this.simpleValueList = simpleValueList;
    }

    @Override
    public int getCount() {
        return simpleValueList.length;
    }

    @Override
    public Object getItem(int position) {
        return simpleValueList[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.test, parent, false);

            TextView textSimple = view.findViewById(R.id.simple_list);
            textHours.setText(simpleValueList[position]);
        }
        return view;
    }
}

my CustomView class:

public class SimpleView extends View {

    private String[] valueList = {"aa", "2", "bb", "3"};
    private ListView listView;
    private SimpleAdapter simpleAdapter;

    public SimpleView(Context context, ViewGroup mviewGroup) {
        super(context);
        inflate(context, R.layout.test, mviewGroup);

        listView = findViewById(R.id.simple_list);
        simpleAdapter = new SimpleAdapter(context, valueList);
        listView.setAdapter(simpleAdapter);
  }
}

my Activity class:

public class SimpleActivity extends AppCompatActivity {
    private SimpleView simpleView;

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

        LinearLayout linearLayout = findViewById(R.id.custom_view);
        simpleView = new SimpleView(this, linearLayout);
        linearLayout.addView(simpleView);
    }
}

test. xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="100dp"
    android:layout_height="220dp"
    android:layout_gravity="center">


    <ListView
        android:id="@+id/simple_list"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/orange" />
</LinearLayout>

my activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/custom_view"
        android:layout_width="400dp"
        android:layout_height="match_parent"
        android:orientation="vertical">
 </LinearLayout>

If I do not add the Adapter in the SimpleView class then it works. But if I add the Adapter in the SimpleView class I get the exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference. (at ...here: listView.setAdapter(simpleAdapter); (simpleView = new SimpleView(this, linearLayout));

The problem is that the code is calling the SimpleView's version of findViewById which is using test.xml, but the ListView is located in activity_main.xml in the SimpleActivity .

listView = findViewById(R.id.custom_view1);

This will return null because there is no view with the ID R.id.custom_view1 inside SimpleView.

There probably is not any need for the SimpleView class. The following lines of code could be moved into onCreate in SimpleActivity :

 listView = findViewById(R.id.custom_view1);
 simpleAdapter = new SimpleAdapter(context, valueList);
 listView.setAdapter(simpleAdapter);

If you do want to create a CustomView on the other hand, all the views you are working with need to be in R.layout.test. You could in theory, move the ListView in there, but I think it is probably unnecessary. I would just inflate, find it and set the adapter in onCreate of the Activity .

If you do want an abstraction to contain the ListView , I would investigate putting it into a Fragment .

EDIT: If it has to be done with a custom view then make sure the following is in test.xml:

<ListView
    android:id="@+id/custom_view1"
    android:layout_width="71dp"
    android:layout_height="77dp"
    android:orientation="vertical">

It is important to use this ID android:id="@+id/custom_view1 so that it matches up with listView = findViewById(R.id.custom_view1); in the Java code.

To void the crash you are getting now:

public SimpleView(Context context, ViewGroup mviewGroup) {
    super(context);
    View layout = inflate(context, R.layout.test, mviewGroup);
    listView = layout.findViewById(R.id.simple_list);
    simpleAdapter = new SimpleAdapter(context, valueList);
    listView.setAdapter(simpleAdapter);
}

Make sure you do the find on the value being returned from inflate.

=== Code Dump ===:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/custom_view_container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"/>

test.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">

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

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="100dp"
    android:layout_height="220dp"
    android:layout_gravity="center">

    <TextView
        android:id="@+id/txt"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
</LinearLayout>

ActivityMain.java:

package com.example.myapplication2000;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

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

        LinearLayout linearLayout = findViewById(R.id.custom_view_container);
        SimpleView simpleView = new SimpleView(this, linearLayout);
        linearLayout.addView(simpleView);
    }
}

SimpleView.java:

package com.example.myapplication2000;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SimpleView extends View {

    private String[] valueList = {"aa", "2", "bb", "3"};
    private ListView listView;
    private SimpleAdapter simpleAdapter;

    public SimpleView(Context context, ViewGroup mviewGroup) {
        super(context);
        View layout = inflate(context, R.layout.test, mviewGroup);
        listView = layout.findViewById(R.id.simple_list);
        simpleAdapter = new SimpleAdapter(context, valueList);
        listView.setAdapter(simpleAdapter);
    }

    public class SimpleAdapter extends BaseAdapter {
        private LayoutInflater lInflater;
        private String [] simpleValueList;

        public SimpleAdapter(Context context, String[] simpleValueList) {
            lInflater = LayoutInflater.from(context);
            this.simpleValueList = simpleValueList;
        }

        @Override
        public int getCount() {
            return simpleValueList.length;
        }

        @Override
        public Object getItem(int position) {
            return simpleValueList[position];
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                view = lInflater.inflate(R.layout.row, parent, false);

                TextView textSimple = view.findViewById(R.id.txt);
                textSimple.setText(simpleValueList[position]);
            }
            return view;
        }
    }
}

Sorry, I might have changed one or two of the names. I hope it does not cause any confusion.

Also, you can clean-up the layout. There are a few 'extra' LinearLayouts that I picked up from pasting in your code, but they are not really needed.

But this code is displaying 4 list items on my device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM