[英]I am getting an runtime error (Null Pointer Exception) while using ListView in Android Studio
I am making a Task Manager app using ListView to Show all the tasks.But I am getting this error我正在使用 ListView 制作一个任务管理器应用程序来显示所有任务。但是我收到了这个错误
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.access$900(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.access$900(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)
what I think the error is that, I am sending wrong context in my custom adapter in MainActivity.java file我认为错误是,我在 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);
}
}
My custom Adapter is我的自定义适配器是
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;
}
}
My Xml file for activity Main is我的活动 Main 的 Xml 文件是
<?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>
My custom layout file is我的自定义布局文件是
<?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>
My Data class is我的数据类是
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;
}
}
I am also using a Splash Screen but, I don't think that is a problem because my app is crashing after the splash screen is over, and when the intent goes to MainActivity.我也在使用启动画面,但我认为这不是问题,因为我的应用程序在启动画面结束后崩溃,并且当意图转到 MainActivity 时。 Can someone please tell me the error Also, I don't think there are any Dependencies missing from my File.
有人可以告诉我错误吗另外,我认为我的文件中没有任何依赖项。
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'
}
You are getting this error because setContentView(R.layout.layoutname)
is missing in MainActivity.java
您收到此错误是因为
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);
}
I think you are missing the setContentView(R.layout.activity_main)
in the onCreate
of MainActivity.我认为您在 MainActivity 的
onCreate
中缺少setContentView(R.layout.activity_main)
。 Assuming activity_main
is the xml file name.假设
activity_main
是xml 文件名。 If not, replace it with actual name.如果不是,请用实际名称替换它。
Due to this, your layout is never inflated and hence you should be getting null from findViewById(R.id.listView)
.因此,您的布局永远不会膨胀,因此您应该从
findViewById(R.id.listView)
获取 null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.