单击列表视图按钮引发空指针异常

Listview button click throws null pointer exception

提问人:Sajib 提问时间:7/15/2019 最后编辑:Sajib 更新时间:8/30/2019 访问量:193

问:

在我的类中调用方法以生成 .每个按钮都有一个按钮,用于删除 .加载后,当我尝试通过单击列表视图按钮从中删除项目时,它会引发异常并且应用程序崩溃。UpdateSupervitionActivityCallAdapter()listviewlistviewitemitemlistviewlistviewnullpointer


带有 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)
Java Android ListView NullPointerException 适配器

评论

0赞 Ikazuchi 7/15/2019
你能展示你初始化的部分吗?listView
0赞 Sajib 7/15/2019
@Ikazuchi好的,我已经在我的课堂活动部分添加了该代码。你可以检查。
0赞 Sagar Chapagain 7/15/2019
只是一个建议:不要使用 ListView,而是使用 RecyclerView 来处理列表数据。RecyclerView 实现更加灵活。

答:

1赞 Teempy 7/15/2019 #1

出现此错误的原因是 id 找不到列表视图。似乎您的适配器首先从另一个活动初始化,这是您使用 listview 布局的真实上下文。有两种方法可以解决这个问题: 1)您应该通过以下方式在更新类中初始化您的列表视图

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

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

更新尝试:

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

评论

0赞 Sajib 7/15/2019
将 移动到适配器类不起作用。同样的错误。而且您的第一个解决方案格式不正确。它也会产生错误。CallAdapter()
0赞 Teempy 7/15/2019
第一个解决方案的错误是什么?并且不应将 CallAdapter 移动到适配器类中。您应该已将其移动到活动类中,该类是带有列表视图的布局的活动。问题是找不到 UpdateSupervitionActivity 中的 listView。您可以从传输的上下文中提取列表视图,也可以将重新初始化适配器的实际方法移动到首次初始化适配器的类中
0赞 Sajib 7/16/2019
我不清楚你的第一个解决方案。上下文中没有 findViewById。
0赞 Teempy 7/16/2019
ListView listView = (ListView) ((YourRealActivityName) context).findViewById(R.id.faclistview)