在 Android Studio 中使用 ListView 时出现运行时错误(空指针异常)
I am getting an runtime error (Null Pointer Exception) while using ListView in Android Studio
我正在使用 ListView 制作任务管理器应用程序以显示所有 tasks.But 我收到此错误
Process: com.example.taskmanager, PID: 29293
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taskmanager/com.example.taskmanager.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.taskmanager.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:6024)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
我认为错误是,我在 MainActivity.java 文件
中的自定义适配器中发送了错误的上下文
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.taskmanager.Model.DataClass;
import com.example.taskmanager.MyAdapter.MyAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<DataClass> datalist= new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
DataClass data1 = new DataClass();
data1.setId(1);
data1.setStatus(0);
data1.setTask("yo yo yo");
datalist.add(data1);
datalist.add(data1);
datalist.add(data1);
datalist.add(data1);
datalist.add(data1);
ListView taskListView = findViewById(R.id.listView);
MyAdapter myadapter = new MyAdapter(getApplicationContext(),R.layout.new_task_layout,datalist);
taskListView.setAdapter(myadapter);
}
}
我的自定义适配器是
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.taskmanager.Model.DataClass;
import com.example.taskmanager.R;
import java.util.List;
public class MyAdapter extends ArrayAdapter<DataClass> {
Context mcontext;
int lytresourceId;
List<DataClass> data;
public MyAdapter(@NonNull Context context, int resource, @NonNull List<DataClass> objects) {
super(context, resource, objects);
this.mcontext=context;
this.lytresourceId=resource;
this.data=objects;
}
@Nullable
@Override
public DataClass getItem(int position) {
return super.getItem(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View row, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mcontext);
row = inflater.inflate(lytresourceId,parent,false);
CheckBox checkbox;
checkbox=row.findViewById(R.id.checkBox);
DataClass mdata = data.get(position);
checkbox.setText(mdata.getTask());
return row;
}
}
我的 Xml 文件 activity Main 是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/TaskText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="Task"
android:textColor="#000000"
android:textSize="40sp"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/LinearLayout1"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="30dp"
android:layout_marginRight="20dp"
app:srcCompat="@android:drawable/ic_input_add" />
</RelativeLayout>
我的自定义布局文件是
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="3dp"
app:cardCornerRadius="8dp"
android:layout_marginHorizontal="4dp"
android:layout_marginVertical="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="task1"
android:padding="4dp"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
我的数据class是
package com.example.taskmanager.Model;
public class DataClass {
private int id,status;
private String task;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
}
我也在使用启动画面,但我认为这不是问题,因为启动画面结束后,当 Intent 转到 MainActivity 时,我的应用程序崩溃了。
有人可以告诉我错误吗
另外,我不认为我的文件中缺少任何依赖项。
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
您收到此错误是因为 MainActivity.java
中缺少 setContentView(R.layout.layoutname)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is missing in your class
setContentView(R.layout.activity_main);
}
我认为您在 MainActivity 的 onCreate
中遗漏了 setContentView(R.layout.activity_main)
。假设 activity_main
是 xml 文件名。如果不是,请将其替换为实际名称。
因此,您的布局永远不会膨胀,因此您应该从 findViewById(R.id.listView)
.
中获取 null
我正在使用 ListView 制作任务管理器应用程序以显示所有 tasks.But 我收到此错误
Process: com.example.taskmanager, PID: 29293
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taskmanager/com.example.taskmanager.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.taskmanager.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:6024)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
我认为错误是,我在 MainActivity.java 文件
中的自定义适配器中发送了错误的上下文import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.taskmanager.Model.DataClass;
import com.example.taskmanager.MyAdapter.MyAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<DataClass> datalist= new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
DataClass data1 = new DataClass();
data1.setId(1);
data1.setStatus(0);
data1.setTask("yo yo yo");
datalist.add(data1);
datalist.add(data1);
datalist.add(data1);
datalist.add(data1);
datalist.add(data1);
ListView taskListView = findViewById(R.id.listView);
MyAdapter myadapter = new MyAdapter(getApplicationContext(),R.layout.new_task_layout,datalist);
taskListView.setAdapter(myadapter);
}
}
我的自定义适配器是
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.taskmanager.Model.DataClass;
import com.example.taskmanager.R;
import java.util.List;
public class MyAdapter extends ArrayAdapter<DataClass> {
Context mcontext;
int lytresourceId;
List<DataClass> data;
public MyAdapter(@NonNull Context context, int resource, @NonNull List<DataClass> objects) {
super(context, resource, objects);
this.mcontext=context;
this.lytresourceId=resource;
this.data=objects;
}
@Nullable
@Override
public DataClass getItem(int position) {
return super.getItem(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View row, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mcontext);
row = inflater.inflate(lytresourceId,parent,false);
CheckBox checkbox;
checkbox=row.findViewById(R.id.checkBox);
DataClass mdata = data.get(position);
checkbox.setText(mdata.getTask());
return row;
}
}
我的 Xml 文件 activity Main 是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/TaskText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="Task"
android:textColor="#000000"
android:textSize="40sp"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/LinearLayout1"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="30dp"
android:layout_marginRight="20dp"
app:srcCompat="@android:drawable/ic_input_add" />
</RelativeLayout>
我的自定义布局文件是
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="3dp"
app:cardCornerRadius="8dp"
android:layout_marginHorizontal="4dp"
android:layout_marginVertical="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="task1"
android:padding="4dp"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
我的数据class是
package com.example.taskmanager.Model;
public class DataClass {
private int id,status;
private String task;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
}
我也在使用启动画面,但我认为这不是问题,因为启动画面结束后,当 Intent 转到 MainActivity 时,我的应用程序崩溃了。 有人可以告诉我错误吗 另外,我不认为我的文件中缺少任何依赖项。
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
您收到此错误是因为 MainActivity.java
setContentView(R.layout.layoutname)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is missing in your class
setContentView(R.layout.activity_main);
}
我认为您在 MainActivity 的 onCreate
中遗漏了 setContentView(R.layout.activity_main)
。假设 activity_main
是 xml 文件名。如果不是,请将其替换为实际名称。
因此,您的布局永远不会膨胀,因此您应该从 findViewById(R.id.listView)
.