2022-09-03 17:02:35.870 14558-14558/com.example.myapplication_study E/AndroidRuntime:致命异常:main流程:com.example.mapplication_study,PID:14558java.lang.NullPointerException:尝试在null对象引用上调用虚拟方法"void android.widget.ListView.setAdapter(android.witget.ListAdapter("网址:com.example.myapplication_study.ManagerUserman.onCreateView(ManagerUserman.java:40(位于androidx.fragment.app.fragment.performCreateView(fragment.java:2995(位于androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523(位于androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261(位于androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840(位于androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1758(位于androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701(在androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:488(在android.os.Handler.handleCallback(Handler.java:938(在android.os.Handler.dispatchMessage(Handler.java:99(在android.os.Looper.loop(Looper.java:223(在android.app.ActivityThread.main(ActivityThreads.java:7656(位于java.lang.reflect.Method.ioke(本机方法(网址:com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592(网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:947(
如果你遇到这个错误,应用程序将不会运行,你不知道问题出在哪里吗?我的代码在这里,请帮我tt
管理员Userman.class
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
import java.util.List;
public class ManagerUserman extends Fragment {
private View view;
private ListView listView;
private ManagerUsermanListAdapter managerUsermanListAdapter;
private List<ManagerUsermanList> usermanList;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_manageruserman_list, container, false);
Intent intent = getActivity().getIntent();
listView = getActivity().findViewById(R.id.listView);
usermanList = new ArrayList<ManagerUsermanList>();
usermanList.add(new ManagerUsermanList("name", "ID", "Password"));
managerUsermanListAdapter = new ManagerUsermanListAdapter(getActivity().getApplicationContext(), usermanList);
listView.setAdapter(managerUsermanListAdapter);
return view;
}
}
管理员UsermanListAdapter.class
public class ManagerUsermanListAdapter extends BaseAdapter {
private Context context;
private List<ManagerUsermanList> usermanList;
public ManagerUsermanListAdapter(Context context, List<ManagerUsermanList> usermanList) {
this.context = context;
this.usermanList = usermanList;
}
@Override
public int getCount() {
return usermanList.size();
}
@Override
public Object getItem(int i) {
return usermanList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {
View v = View.inflate(context, R.layout.activity_manageruserman_list, null);
TextView userName = (TextView) v.findViewById(R.id.userName);
TextView userID = (TextView) v.findViewById(R.id.userID);
TextView userPassword = (TextView) v.findViewById(R.id.userPassword);
userName.setText(usermanList.get(i).getName());
userID.setText(usermanList.get(i).getUser_id());
userPassword.setText(usermanList.get(i).getUser_pass());
v.setTag(usermanList.get(i).getUser_id());
return v;
}
}
管理员UsermanList.class
public class ManagerUsermanList {
String name;
String user_id;
String user_pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_pass() {
return user_pass;
}
public void setUser_pass(String user_pass) {
this.user_pass = user_pass;
}
public ManagerUsermanList(String name, String user_id, String user_pass) {
this.name = name;
this.user_id = user_id;
this.user_pass = user_pass;
}
}
mangeruserman.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ManagerUserman">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:dividerHeight="10dp"/>
</LinearLayout>
mangeruserman_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ScrollView>
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textColor="#000"
android:textSize="20dp" />
<TextView
android:id="@+id/userID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="id"
android:textColor="#000"
android:textSize="15dp" />
<TextView
android:id="@+id/userPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pass"
android:textColor="#000"
android:textSize="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="38dp"
android:orientation="horizontal">
<Button
android:id="@+id/manager_user_delete_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="del" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
首先:您的片段中的布局文件是错误的。应该是像";mangerserman";但你的";activity_manageruserman_list";。
第二:你不需要使用"getActivity((.findViewById((",应该使用";view.findViewById((;。
对不起,我的英语不太好,希望能帮助你。