提问人:Julia 提问时间:7/29/2020 最后编辑:ElletlarJulia 更新时间:11/16/2020 访问量:206
自定义视图适配器中的 NullPointerException
NullPointerException in the Adapter for a Custom View
问:
我有以下异常的问题:
exception:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
我有一个班级:Adapter
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;
}
}
我的 CustomView 类:
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);
}
}
我的活动类:
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);
}
}
测试。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>
如果我不在类中添加,那么它就可以工作。但是,如果我在类中添加,则会出现异常:Adapter
SimpleView
Adapter
SimpleView
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));
答:
问题是代码正在调用其版本正在使用 test.xml,但 位于 activity_main.xml 中的 .SimpleView's
findViewById
ListView
SimpleActivity
listView = findViewById(R.id.custom_view1);
这将返回 null,因为 SimpleView 中没有 ID 为 R.id.custom_view1 的视图。
可能不需要这个类。以下代码行可以移入:SimpleView
onCreate
SimpleActivity
listView = findViewById(R.id.custom_view1);
simpleAdapter = new SimpleAdapter(context, valueList);
listView.setAdapter(simpleAdapter);
另一方面,如果您确实想创建 CustomView,则您正在使用的所有视图都需要位于 R.layout.test 中。从理论上讲,你可以把它移到那里,但我认为这可能是不必要的。我只是充气,找到它并设置适配器。ListView
onCreate
Activity
如果你确实想要一个抽象来包含 ,我会研究把它放到 .ListView
Fragment
编辑: 如果必须使用自定义视图完成,请确保以下内容在 test.xml 中:
<ListView
android:id="@+id/custom_view1"
android:layout_width="71dp"
android:layout_height="77dp"
android:orientation="vertical">
使用此 ID 非常重要,以便它与 Java 代码中的 ID 匹配。android:id="@+id/custom_view1
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 返回的值进行查找。
=== 代码转储 ===:
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"/>
测试.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>
行.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>
活动Main.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;
}
}
}
对不起,我可能更改了一两个名字。我希望它不会引起任何混乱。
此外,您可以清理布局。我从粘贴到您的代码中获得了一些“额外”的 LinearLayouts,但它们并不是真正需要的。
但是此代码在我的设备上显示 4 个列表项。
评论