gpt4 book ai didi

java - Listview按钮点击抛出空指针异常

转载 作者:行者123 更新时间:2023-12-02 09:41:21 26 4
gpt4 key购买 nike

在我的 UpdateSupervitionActivity 类中,调用 CallAdapter() 方法来生成 listviewlistview 的每个item 都有一个用于删除item 的按钮。加载 listview 后,当我尝试通过单击 listview 按钮从 listview 中删除项目时,它会抛出 nullpointer 异常,并且应用程序崩溃。


具有 CallAdapter() 方法的 Java 类:

package bd.edu.bubt.regup;

public class UpdateSupervitionActivity extends AppCompatActivity {

EditText fac_code, s_intake, s_section;
ListView listView;
Button search, addlist, update;
Spinner shift;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_supervition);

fac_code = (EditText) findViewById(R.id.sh_code);
s_intake = (EditText) findViewById(R.id.intake);
s_section = (EditText) findViewById(R.id.section);
listView = (ListView) findViewById(R.id.faclistview);
shift = (Spinner) findViewById(R.id.shift);
addlist = (Button) findViewById(R.id.addlist);
search = (Button) findViewById(R.id.search);
update = (Button) findViewById(R.id.update);
}

public void CallAdapter(Context context)
{
adapterlist.clear();

if(daylist.size() == 0)
{
//no intake list for day shift

if(evelist.size() == 0)
{
//no intake list for evening shift
}
else if(evelist.size() > 0)
{
for (int i=0;i<evelist.size();i++)
{
String s = evelist.get(i);
int p = s.indexOf("-");

String intake = s.substring(0,p);
String section = s.substring(p+1,s.length());

adapterlist.add(new UpdateSupervisionItem(intake, section, "Evening"));
}
}
}
else if(daylist.size() > 0)
{
for (int i=0;i<daylist.size();i++)
{
String s = daylist.get(i);
int p = s.indexOf("-");

String intake = s.substring(0,p);
String section = s.substring(p+1,s.length());

adapterlist.add(new UpdateSupervisionItem(intake, section, "Day"));
}

if(evelist.size() == 0)
{
//no intake list for evening shift
}
else if(evelist.size() > 0)
{
for (int i=0;i<evelist.size();i++)
{
String s = evelist.get(i);
int p = s.indexOf("-");

String intake = s.substring(0,p);
String section = s.substring(p+1,s.length());

adapterlist.add(new UpdateSupervisionItem(intake, section, "Evening"));
}
}
}

UpdateSupervisionAdapter updateSupervisionAdapter = new UpdateSupervisionAdapter(context, R.layout.update_supervision_list_view_layout, adapterlist);
listView.setAdapter(updateSupervisionAdapter);
}
}


适配器类:

package bd.edu.bubt.regup;

public class UpdateSupervisionAdapter extends ArrayAdapter<UpdateSupervisionItem> {

ArrayList<UpdateSupervisionItem> adapterlist = new ArrayList<>();
Context context;

public UpdateSupervisionAdapter(Context context, int textViewResourceId, ArrayList<UpdateSupervisionItem> objects) {

super(context, textViewResourceId, objects);
this.context = context;
adapterlist = objects;
}

@Override
public int getCount() {

return super.getCount();
}


@Override
public View getView(final int position, View convertView, ViewGroup parent){

View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.update_supervision_list_view_layout, null);

TextView intake = (TextView) v.findViewById(R.id.intake);
TextView section = (TextView) v.findViewById(R.id.section);
TextView shift = (TextView) v.findViewById(R.id.shift);
final Button remove = (Button) v.findViewById(R.id.remove);

remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.blink_anim);
remove.startAnimation(animation);

UpdateSupervisionItem updateSupervisionItem = getItem(position);

String in_take = updateSupervisionItem.getIntake();
String sec_tion = updateSupervisionItem.getSection();
String shi_ft = updateSupervisionItem.getShift();

UpdateSupervitionActivity updateSupervitionActivity = new UpdateSupervitionActivity();

if(TextUtils.equals(shi_ft,"Day"))
{
updateSupervitionActivity.daylist.remove(in_take+ "-" +sec_tion);
updateSupervitionActivity.CallAdapter(context);
}
else if(TextUtils.equals(shi_ft,"Evening"))
{
updateSupervitionActivity.evelist.remove(in_take+ "-" +sec_tion);
updateSupervitionActivity.CallAdapter(context);
}
}
});

intake.setText("Intake: " +adapterlist.get(position).getIntake());
section.setText("Section: " +adapterlist.get(position).getSection());
shift.setText("Shift: " +adapterlist.get(position).getShift());

return v;
}

}


错误日志:

FATAL EXCEPTION: main
Process: bd.edu.bubt.regup, PID: 31219
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at bd.edu.bubt.regup.UpdateSupervitionActivity.CallAdapter(UpdateSupervitionActivity.java:352)
at bd.edu.bubt.regup.UpdateSupervisionAdapter$1.onClick(UpdateSupervisionAdapter.java:70)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最佳答案

出现这个错误的原因是listview没有通过id找到。看起来您的适配器首先是从另一个 Activity 初始化的,这是您的 ListView 布局的真实上下文。有两种方法可以解决这个问题:1)您应该通过

在更新类中初始化您的 ListView
ListView listView = context.findViewById(R.id.faclistview)

2) 您可以将 CallAdapter 移动到适配器的实际上下文 Activity 中

更新尝试一下:

ListView listView = (ListView) ((ActivityWhereAdapterIsForstInitialized) context).findViewById(R.id.faclistview)

关于java - Listview按钮点击抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57042192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com