尝试在空对象引用上调用虚拟方法 'int java.lang.Integer.intValue()'



我有动态视图,我添加到线性布局中,在此之后我得到了我添加到线性布局的孩子,并为它们设置了标签,以便在单击每个动态布局时识别每个动态布局,当我单击动态布局以替换片段以查看单击视图的详细信息时,我收到此错误

Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference

这是我正在做的事情:

for(int i = 0; i < leafletList.getChildCount(); i++){
        leafletList.getChildAt(i).setTag(i);
    }

这是我单击动态视图时正在做的事情

 view.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            fragment.replaceLeaflet((Integer)view.getTag(), data);
        }
    });

如果(Integer)view.getTag()为空,为什么为空? 无论如何,我正在为它们中的每一个设置标签

编辑:这就是我在 xml 中定义的线性布局中添加动态视图的方式

String[] disease = {"Colds","Cough"};
    for(int i = 0; i < disease.length ; i++){
        DrReportLeafletLayout leaflet = new DrReportLeafletLayout(getActivity(), disease[i], this, disease);
        leafletList.addView(leaflet);
    }

崩溃堆栈:

FATAL EXCEPTION: main
                                                                        Process: com.myhealthathand.hah.dev, PID: 25104
                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
                                                                            at com.myhealthathand.hah.customviews.DrReportLeafletLayout$1.onClick(DrReportLeafletLayout.java:55)
                                                                            at android.view.View.performClick(View.java:5207)
                                                                            at android.view.View$PerformClick.run(View.java:21168)
                                                                            at android.os.Handler.handleCallback(Handler.java:746)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5491)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

当您要向视图添加标签时,应该在创建视图的位置设置标签

最新更新