空指针异常在自定义视图的适配器中发生

huangapple go评论80阅读模式
英文:

NullPointerException in the Adapter for a Custom View

问题

以下是翻译好的部分:

我在以下异常中遇到了问题:

异常:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'

我有一个Adapter类:

public class SimpleAdapter extends BaseAdapter {
    // ... 省略其他代码
}

我的CustomView类:

public class SimpleView extends View {
    // ... 省略其他代码
}

我的Activity类:

public class SimpleActivity extends AppCompatActivity {
    // ... 省略其他代码
}

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>

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>

如果我不在SimpleView类中添加Adapter,那么它可以正常工作。但如果我在SimpleView类中添加Adapter,我会收到以下异常:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'(位于...此处:listView.setAdapter(simpleAdapter);(simpleView = new SimpleView(this, linearLayout));)

英文:

I have a problem with the following exception:

exception:java.lang.NullPointerException: Attempt to invoke virtual method &#39;void android.widget.ListView.setAdapter(android.widget.ListAdapter)&#39; 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 = {&quot;aa&quot;, &quot;2&quot;, &quot;bb&quot;, &quot;3&quot;};
    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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:custom=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;100dp&quot;
    android:layout_height=&quot;220dp&quot;
    android:layout_gravity=&quot;center&quot;&gt;


    &lt;ListView
        android:id=&quot;@+id/simple_list&quot;
        android:layout_width=&quot;100dp&quot;
        android:layout_height=&quot;100dp&quot;
        android:background=&quot;@color/orange&quot; /&gt;
&lt;/LinearLayout&gt;

my activity_main.xml

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

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 &#39;void android.widget.ListView.setAdapter(android.widget.ListAdapter)&#39; on a null object reference. (at ...here: listView.setAdapter(simpleAdapter); (simpleView = new SimpleView(this, linearLayout));

答案1

得分: 2

问题在于代码调用了SimpleViewfindViewById版本,它使用了test.xml,但ListView位于SimpleActivityactivity_main.xml中。

listView = findViewById(R.id.custom_view1);

这会返回null,因为在SimpleView内部没有ID为R.id.custom_view1的视图。

也许没有必要使用SimpleView类。以下代码行可以移到SimpleActivityonCreate方法中:

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

如果您确实想创建一个CustomView,那么您需要确保所有与之交互的视图都位于R.layout.test中。理论上,您可以将ListView移到那里,但我认为这可能是不必要的。我建议在ActivityonCreate中进行视图的膨胀、查找和设置适配器。

如果您确实希望有一个包含ListView的抽象层,我建议将其放入一个Fragment中。

如果必须使用自定义视图,请确保在test.xml中包含以下内容:

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

很重要的是使用这个ID android:id="@+id/custom_view1",以使其与Java代码中的 listView = findViewById(R.id.custom_view1); 相匹配。

为了避免您目前遇到的崩溃:

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);
}

确保对从inflate返回的值进行查找操作。

此外,您可以清理布局。我从您的代码粘贴中注意到了一些“额外”的LinearLayout,但它们实际上并不是必要的。

但这段代码在我的设备上显示了4个列表项。

英文:

The problem is that the code is calling the SimpleView&#39;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:

&lt;ListView
    android:id=&quot;@+id/custom_view1&quot;
    android:layout_width=&quot;71dp&quot;
    android:layout_height=&quot;77dp&quot;
    android:orientation=&quot;vertical&quot;&gt;

It is important to use this ID android:id=&quot;@+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

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

test.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:custom=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:layout_gravity=&quot;center&quot;&gt;

    &lt;ListView
        android:id=&quot;@+id/simple_list&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;/&gt;
&lt;/LinearLayout&gt;

row.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:custom=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;100dp&quot;
    android:layout_height=&quot;220dp&quot;
    android:layout_gravity=&quot;center&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/txt&quot;
        android:layout_width=&quot;100dp&quot;
        android:layout_height=&quot;100dp&quot;/&gt;
&lt;/LinearLayout&gt;

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 = {&quot;aa&quot;, &quot;2&quot;, &quot;bb&quot;, &quot;3&quot;};
    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.

huangapple
  • 本文由 发表于 2020年7月29日 03:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63141068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定