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

时间:2018-01-01 12:15:11

标签: java android nullpointerexception

我有动态视图,我添加到线性布局后,我得到了一个孩子,我添加到线性布局并设置标签,以便在点击它时点击动态布局以识别每个动态布局看到点击视图的详细信息我收到此错误

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)

1 个答案:

答案 0 :(得分:-1)

如果要在视图中添加标记,应该规定应该在创建视图的位置设置