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

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

NullPointerException in the Adapter for a Custom View

问题

以下是翻译好的部分:

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

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

我有一个Adapter类:

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

我的CustomView类:

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

我的Activity类:

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

test.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:custom="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="100dp"
  6. android:layout_height="220dp"
  7. android:layout_gravity="center">
  8. <ListView
  9. android:id="@+id/simple_list"
  10. android:layout_width="100dp"
  11. android:layout_height="100dp"
  12. android:background="@color/orange" />
  13. </LinearLayout>

activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/custom_view"
  5. android:layout_width="400dp"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical">
  8. </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:

  1. 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:

  1. public class SimpleAdapter extends BaseAdapter {
  2. private LayoutInflater lInflater;
  3. private String [] simpleValueList;
  4. public SimpleAdapter(Context context, String[] simpleValueList) {
  5. lInflater = LayoutInflater.from(context);
  6. this.simpleValueList = simpleValueList;
  7. }
  8. @Override
  9. public int getCount() {
  10. return simpleValueList.length;
  11. }
  12. @Override
  13. public Object getItem(int position) {
  14. return simpleValueList[position];
  15. }
  16. @Override
  17. public long getItemId(int position) {
  18. return position;
  19. }
  20. @Override
  21. public View getView(int position, View convertView, ViewGroup parent) {
  22. View view = convertView;
  23. if (view == null) {
  24. view = lInflater.inflate(R.layout.test, parent, false);
  25. TextView textSimple = view.findViewById(R.id.simple_list);
  26. textHours.setText(simpleValueList[position]);
  27. }
  28. return view;
  29. }
  30. }

my CustomView class:

  1. public class SimpleView extends View {
  2. private String[] valueList = {&quot;aa&quot;, &quot;2&quot;, &quot;bb&quot;, &quot;3&quot;};
  3. private ListView listView;
  4. private SimpleAdapter simpleAdapter;
  5. public SimpleView(Context context, ViewGroup mviewGroup) {
  6. super(context);
  7. inflate(context, R.layout.test, mviewGroup);
  8. listView = findViewById(R.id.simple_list);
  9. simpleAdapter = new SimpleAdapter(context, valueList);
  10. listView.setAdapter(simpleAdapter);
  11. }
  12. }

my Activity class:

  1. public class SimpleActivity extends AppCompatActivity {
  2. private SimpleView simpleView;
  3. protected void onCreate(@Nullable Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. LinearLayout linearLayout = findViewById(R.id.custom_view);
  7. simpleView = new SimpleView(this, linearLayout);
  8. linearLayout.addView(simpleView);
  9. }
  10. }

test. xml:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:custom=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;100dp&quot;
  6. android:layout_height=&quot;220dp&quot;
  7. android:layout_gravity=&quot;center&quot;&gt;
  8. &lt;ListView
  9. android:id=&quot;@+id/simple_list&quot;
  10. android:layout_width=&quot;100dp&quot;
  11. android:layout_height=&quot;100dp&quot;
  12. android:background=&quot;@color/orange&quot; /&gt;
  13. &lt;/LinearLayout&gt;

my activity_main.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. android:id=&quot;@+id/custom_view&quot;
  5. android:layout_width=&quot;400dp&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:orientation=&quot;vertical&quot;&gt;
  8. &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:

  1. 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中。

  1. listView = findViewById(R.id.custom_view1);

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

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

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

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

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

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

  1. <ListView
  2. android:id="@+id/custom_view1"
  3. android:layout_width="71dp"
  4. android:layout_height="77dp"
  5. android:orientation="vertical">

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

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

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

确保对从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.

  1. 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:

  1. listView = findViewById(R.id.custom_view1);
  2. simpleAdapter = new SimpleAdapter(context, valueList);
  3. 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:

  1. &lt;ListView
  2. android:id=&quot;@+id/custom_view1&quot;
  3. android:layout_width=&quot;71dp&quot;
  4. android:layout_height=&quot;77dp&quot;
  5. 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:

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

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

=== Code Dump ===:

activity_main.xml

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

test.xml:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:custom=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:layout_gravity=&quot;center&quot;&gt;
  8. &lt;ListView
  9. android:id=&quot;@+id/simple_list&quot;
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;match_parent&quot;/&gt;
  12. &lt;/LinearLayout&gt;

row.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:custom=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;100dp&quot;
  6. android:layout_height=&quot;220dp&quot;
  7. android:layout_gravity=&quot;center&quot;&gt;
  8. &lt;TextView
  9. android:id=&quot;@+id/txt&quot;
  10. android:layout_width=&quot;100dp&quot;
  11. android:layout_height=&quot;100dp&quot;/&gt;
  12. &lt;/LinearLayout&gt;

ActivityMain.java:

  1. package com.example.myapplication2000;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.LinearLayout;
  5. public class MainActivity extends AppCompatActivity {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. LinearLayout linearLayout = findViewById(R.id.custom_view_container);
  11. SimpleView simpleView = new SimpleView(this, linearLayout);
  12. linearLayout.addView(simpleView);
  13. }
  14. }

SimpleView.java:

  1. package com.example.myapplication2000;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseAdapter;
  7. import android.widget.ListView;
  8. import android.widget.TextView;
  9. public class SimpleView extends View {
  10. private String[] valueList = {&quot;aa&quot;, &quot;2&quot;, &quot;bb&quot;, &quot;3&quot;};
  11. private ListView listView;
  12. private SimpleAdapter simpleAdapter;
  13. public SimpleView(Context context, ViewGroup mviewGroup) {
  14. super(context);
  15. View layout = inflate(context, R.layout.test, mviewGroup);
  16. listView = layout.findViewById(R.id.simple_list);
  17. simpleAdapter = new SimpleAdapter(context, valueList);
  18. listView.setAdapter(simpleAdapter);
  19. }
  20. public class SimpleAdapter extends BaseAdapter {
  21. private LayoutInflater lInflater;
  22. private String [] simpleValueList;
  23. public SimpleAdapter(Context context, String[] simpleValueList) {
  24. lInflater = LayoutInflater.from(context);
  25. this.simpleValueList = simpleValueList;
  26. }
  27. @Override
  28. public int getCount() {
  29. return simpleValueList.length;
  30. }
  31. @Override
  32. public Object getItem(int position) {
  33. return simpleValueList[position];
  34. }
  35. @Override
  36. public long getItemId(int position) {
  37. return position;
  38. }
  39. @Override
  40. public View getView(int position, View convertView, ViewGroup parent) {
  41. View view = convertView;
  42. if (view == null) {
  43. view = lInflater.inflate(R.layout.row, parent, false);
  44. TextView textSimple = view.findViewById(R.id.txt);
  45. textSimple.setText(simpleValueList[position]);
  46. }
  47. return view;
  48. }
  49. }
  50. }

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:

确定