AbsListView中的NullPointerException

时间:2013-05-20 10:15:55

标签: android listview

我在我的应用程序之外的ListView中有一个惊人的异常。我已经读过它可能是由我的适配器引起的,它返回null。但我的适配器检查它:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    SmsHolder v;
    if(view == null) {
        if(MainActivity.DEBUG) {
            view = lInflater.inflate(R.layout.sms_list_item_debug, parent, false);
        } else {
            view = lInflater.inflate(R.layout.sms_list_item, parent, false);
        }
    }

    v = (SmsHolder) view.getTag(R.id.screenHolder);

    SMS sms = getSMS(position);

    v.date.setText(ZenDate.format(ZenDate.FORMAT_RUS_DM, sms.time_stamp * 1000));
    v.sender.setText(sms.sender + " ");
    v.text.setText(sms.text + " ");
    v.selector.setOnCheckedChangeListener(null);
    v.selector.setChecked(checked.contains(sms._id));
    v.status.setTag(sms);
    v.selector.setTag(position);

    boolean smsParsed = sms.parsed != null
            && sms.checkFlag(SMS.FLAG_SENDER_OK)
            && sms.checkFlag(SMS.FLAG_FORMAT_OK)
            && sms.checkFlag(SMS.FLAG_ACCOUNT_OK);
    if(smsParsed) {
        v.status_bg.setVisibility(View.GONE);
    } else {
        v.status_bg.setVisibility(View.VISIBLE);
        if (sms.checkFlag(SMS.FLAG_FORMAT_OK)) {
            v.status.setEnabled(false);
        } else {
            v.status.setEnabled(true);
        }
        v.status.setText(getSMSStatus(sms));
        v.status.setEventListener(new ContextMenu.EventListener() {
            @Override
            public void onItemSelected(ContextMenu v, int index) {
                SMS sms = (SMS) v.getTag();
                if (index == 0) {
                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.setType("message/rfc822");
                    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"support@zenmoney.ru", ""});
                    String subj = getSMSStatus(sms) + "["+ ZenMoney.getVersionName()+"]";
                    i.putExtra(Intent.EXTRA_SUBJECT, subj);
                    String text = context.getString(R.string.sms_mail);
                    i.putExtra(Intent.EXTRA_TEXT, String.format(text, sms.sender, sms.text));
                    try {
                        i = Intent.createChooser(i, context.getString(R.string.sms_app_choose));
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(i);
                    } catch (android.content.ActivityNotFoundException ex) {
                        Toast.makeText(context, context.getString(R.string.no_apps), Toast.LENGTH_SHORT).show();
                    }
                } else if (MainActivity.DEBUG && index == 1) {
                    Intent i = new Intent(context, SmsTestActivity.class);
                    i.putExtra("sms_sender", sms.sender);
                    i.putExtra("sms_body", sms.text);
                    context.startActivity(i);
                }
            }

            @Override
            public void onClick(View view) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

        });

    }
    v.selector.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton v, boolean isChecked) {

            // TODO Auto-generated method stub
            Integer position = (Integer) v.getTag();
            SMS sms = smsList.get(position);
            Long id = sms._id;
            if (!isChecked) {
                checked.remove(id);
            } else if (!checked.contains(id)) {
                checked.add(id);
            }
            if (checked.size() > 0) {
                SmsScreen.showBottomCAB();
            } else {
                SmsScreen.hideBottomCAB();
            }
        }
    });
    if (view == null) {
        ZenMoney.uncaughtException(new NullPointerException());
    }
    return view;
}

这是错误日志:

ERROR/AndroidRuntime(4393): FATAL EXCEPTION: main
    java.lang.NullPointerException
    at android.widget.AbsListView.obtainView(AbsListView.java:1482)
    at android.widget.ListView.makeAndAddView(ListView.java:1786)
    at android.widget.ListView.fillDown(ListView.java:705)
    at android.widget.ListView.fillFromTop(ListView.java:762)
    at android.widget.ListView.layoutChildren(ListView.java:1639)
    at android.widget.AbsListView.onLayout(AbsListView.java:1310)
    at android.view.View.layout(View.java:7224)
    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1528)
    at android.view.View.layout(View.java:7224)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1290)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1166)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1083)
    at android.view.View.layout(View.java:7224)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1290)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1166)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1083)
    at android.view.View.layout(View.java:7224)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:943)
    at android.view.View.layout(View.java:7224)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
    at android.view.View.layout(View.java:7224)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1290)
    at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1279)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1085)
    at android.view.View.layout(View.java:7224)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
    at android.view.View.layout(View.java:7224)
    at com.slidingmenu.lib.CustomViewAbove.onLayout(CustomViewAbove.java:426)
    at android.view.View.layout(View.java:7224)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:943)
    at android.view.View.layout(View.java:7224)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
    at android.view.View.layout(View.java:7224)
    at android.view.ViewRoot.performTraversals(ViewRoot.java:1181)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1913)
    at android.os.Handler.dispatchMessage(Handler.java:130)
    at android.os.Looper.loop(SourceFile:351)
    at android.app.ActivityThread.main(ActivityThread.java:3820)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:538)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
    at dalvik.system.NativeStart.main(Native Method)

问题是:为什么会出现这种错误,我该如何避免呢?

更新:问题已解决。我刚刚在适配器中感到困惑并使用了另一个。对不起:)

2 个答案:

答案 0 :(得分:2)

如果这是getView方法,它应该以这种方式实现......

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if(view == null) { 
         //inflate
    }
    ....

    return view;
}

检查http://www.vogella.com/articles/AndroidListView/article.html

答案 1 :(得分:0)

这是因为您检查指针是否为空以及是否仍然抛出NullPointerException。对于指针为null而不是抛出NullPointerException的情况,您需要做一些合理的事情。否则你也可以不检查。