我有一个UpdateSupervitionActivity
类,其中包含我的listview
。在类内部,我有一个方法CallAdapter()
来生成listview
。我的listview
对于每个button
都有一个item
,以删除item
。最初,在创建UpdateSupervitionActivity
时,listview
会正确加载。加载listview
后,我尝试通过单击listview
从button
中删除一个项目,它引发nullpointer
异常并崩溃。如果有人可以帮助我并解释为什么会发生,我将很感激。
这是我班上的method
:
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);
}
}
这是我的adapter
课:
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)
答案 0 :(得分:0)
出现此错误的原因是id找不到listview。看来您的适配器是首先从另一个活动初始化的,这是使用listview进行布局的真实上下文。有两种解决方法: 1)您应该通过
在更新类中初始化列表视图ListView listView = context.findViewById(R.id.faclistview)
2)您可以将CallAdapter移到适配器的实际上下文活动中
更新 尝试:
ListView listView = (ListView) ((ActivityWhereAdapterIsForstInitialized) context).findViewById(R.id.faclistview)