英文:
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 '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));
答案1
得分: 2
问题在于代码调用了SimpleView
的findViewById
版本,它使用了test.xml
,但ListView
位于SimpleActivity
的activity_main.xml
中。
listView = findViewById(R.id.custom_view1);
这会返回null,因为在SimpleView
内部没有ID为R.id.custom_view1
的视图。
也许没有必要使用SimpleView
类。以下代码行可以移到SimpleActivity
的onCreate
方法中:
listView = findViewById(R.id.custom_view1);
simpleAdapter = new SimpleAdapter(context, valueList);
listView.setAdapter(simpleAdapter);
如果您确实想创建一个CustomView,那么您需要确保所有与之交互的视图都位于R.layout.test
中。理论上,您可以将ListView
移到那里,但我认为这可能是不必要的。我建议在Activity
的onCreate
中进行视图的膨胀、查找和设置适配器。
如果您确实希望有一个包含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'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论