-2

在我的应用程序之外,我在 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)

问题是:为什么会发生此错误,我该如何避免?

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

4

2 回答 2

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

于 2013-05-20T10:22:03.293 回答
0

发生这种情况是因为您检查指针是否为空,如果是,则无论如何您都会抛出 NullPointerException。您需要针对指针为空的情况做一些明智的事情,而不是抛出 NullPointerException。否则你还不如不检查。

于 2013-05-20T10:22:11.643 回答