当前位置: 代码迷 >> Android >> 适配器中getView方法的NullPointerException
  详细解决方案

适配器中getView方法的NullPointerException

热度:142   发布时间:2023-08-04 11:37:08.0

我正在尝试从SQLite数据库填充ListView。 当我滚动到ListView时,应用程序崩溃了,并且在适配器类的getView()中得到了NullPointerException 如何解决这个问题?

在此行获取NullPointerException

holder.txtInIt.setText(followUp_List.getShortRGN());

这是我的适配器代码。

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            final Holder holder;
            if (row == null)
            {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                row = vi.inflate(R.layout.follow_uplist_item, parent, false);
                holder = new Holder();

                holder.txtInIt = (TextView) row.findViewById(R.id.txtInitialLetter);
                holder.txtUserName = (TextView) row.findViewById(R.id.txt_UserName);
                holder.txtDate = (TextView) row.findViewById(R.id.txt_followDate);
                holder.txtFolloUp = (TextView) row.findViewById(R.id.textFolloup);
            }
            else
            {
                holder = (Holder) row.getTag();
            }

            final FollowUp_List followUp_List = data.get(position);

            holder.txtInIt.setText(followUp_List.getShortRGN());
            if (holder.txtInIt.getText().toString().equals("R"))
            {
                holder.txtInIt.setBackgroundResource(R.drawable.red_circle_shape);
            }
            if (holder.txtInIt.getText().toString().equals("G")) {

                holder.txtInIt.setBackgroundResource(R.drawable.green_circle_shape);
            }
            if (holder.txtInIt.getText().toString().equals("N")) {

                holder.txtInIt.setBackgroundResource(R.drawable.blue_circle_shape);
            }


            holder.txtUserName.setText(followUp_List.getCreatedBy());
            holder.txtDate.setText(followUp_List.getFollowup_date());
            holder.txtFolloUp.setText(followUp_List.getFollowUp());

            return row;

        }

        final class Holder {
            TextView txtInIt;
            TextView txtUserName;
            TextView txtDate;
            TextView txtFolloUp;
        }
    }

这是logcat信息:

10-24 09:54:06.057    5826-5826/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.tazeen.classnkk.FollowUpList$FollowUpListAdapter.getView(FollowUpList.java:299)
            at android.widget.AbsListView.obtainView(AbsListView.java:2012)
            at android.widget.ListView.makeAndAddView(ListView.java:1772)
            at android.widget.ListView.fillDown(ListView.java:672)
            at android.widget.ListView.fillGap(ListView.java:636)
            at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4546)
            at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:2852)
            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3106)
            at android.view.View.dispatchTouchEvent(View.java:5486)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1714)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1892)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1840)
            at android.view.View.dispatchPointerEvent(View.java:5662)
            at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2863)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

holder对象为null。 因此,例外。 因此,如果holder为null,则需要初始化holder

代替if (row == null) ,请使用if (row == null || holder == null)

此外,你内心if条件下,你需要添加row.setTag(holder) 这很重要,只有这样您才能执行getTag()

因此它将是这样的:

            if (row == null || holder == null)
            {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                row = vi.inflate(R.layout.follow_uplist_item, parent, false);
                holder = new Holder();

                holder.txtInIt = (TextView) row.findViewById(R.id.txtInitialLetter);
                holder.txtUserName = (TextView) row.findViewById(R.id.txt_UserName);
                holder.txtDate = (TextView) row.findViewById(R.id.txt_followDate);
                holder.txtFolloUp = (TextView) row.findViewById(R.id.textFolloup);
                row.setTag(holder);
            }
            else
            {
                holder = (Holder) row.getTag();
            }

请检查您的布局文件,该文件与您对适配器类实现的文件相同,然后检查您的textview ID是否与您添加到适配器类的相同。 我以为您的textview ID与适配器类不同。

只需将以下代码替换为您的适配器类

if (row == null)
            {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                row = vi.inflate(R.layout.follow_uplist_item, parent, null);
                holder = new Holder();

                holder.txtInIt = (TextView) row.findViewById(R.id.txtInitialLetter);
                holder.txtUserName = (TextView) row.findViewById(R.id.txt_UserName);
                holder.txtDate = (TextView) row.findViewById(R.id.txt_followDate);
                holder.txtFolloUp = (TextView) row.findViewById(R.id.textFolloup);
            }
            else
            {
                holder = (Holder) row.getTag();
            }
  相关解决方案