繁体   English   中英

具有多个布局的Android光标适配器-空指针异常

[英]Android Cursor Adapter with Multiple Layouts- Null pointer exception

对于下面的格式,我预先致歉。 我没有在这个网站上发布太多。 我在绑定视图的第148行(特别是在以下行)得到了空指针异常:

AdditionalInfoView additionalInfoView = (AdditionalInfoView) view.getTag();
additionalInfoView.title.setText(cursor.getString(cursor.getColumnIndex("title")));

我相信布局具有ID标题的文本字段,并且数据库的标题列中有数据。 我不确定这种方法是否能正常工作,并且只是在问题真的更早发生时才随机给我一个错误。

我正在尝试实现一个可以处理多种数据类型的列表视图,以便可以动态创建包含所需数据的页面。 我这样做正确吗? 是否有更好的实施方案? 还是根本不建议这样做? 以下适配器的完整代码:

一个注意事项-我看到了一些实现,其中有许多列表适配器正在显示的布局类型。 我需要将它设置在某个地方吗?

编辑我将所有对游标的调用都包装在isNull()中,以确保不是游标不起作用。 我仍然遇到错误,因此我认为视图一定有问题。

/** ResourcesAdapter
*
* This takes in a cursor for the resources table and attaches the
* appropriate view to the appropriate data field. Fields are:
* AdditionalInfo
* Address
* AppLink
* Paragraph
* Phone
* Space
* TextWithBullets
* TitleBreak
* WebLink
*
* TODO- I'm totally skipping AppLink for later.
*/
public class ResourcesAdapter extends CursorAdapter {
public ResourcesAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

static class PhoneView {
    TextView title;
    TextView phone;
}

static class TitleBreakView {
    TextView title;
}

static class AdditionalInfoView {
    TextView title;
}

static class AddressView {
    TextView title;
    TextView address;
}

static class AppLinkView {
    TextView title;
}

static class ParagraphView {
    TextView paragraph;
}

static class TextWithBulletsView {
    TextView text;
    TextView bullet1;
    TextView bullet2;
    TextView bullet3;
    TextView bullet4;
    TextView bullet5;
}

static class WebLinkView {
    TextView title;
}

/** newView
*
* This method gets the views that we later bind the data to. Supposedly this will recycle views
* too, but it's not the convertView method I'm used to from array adapters. If it's happening,
* its happening in the background.
**/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = null;

    if (cursor.getString(cursor.getColumnIndex("type")).equals("Phone")) {
        view = LayoutInflater.from(context).inflate(R.layout.phone, parent, false);
        PhoneView phoneView = new PhoneView();
        phoneView.title = (TextView)(view.findViewById(R.id.title));
        phoneView.phone = (TextView) (view.findViewById(R.id.phone));
        view.setTag(phoneView);
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("TitleBreak")) {
        view = LayoutInflater.from(context).inflate(R.layout.title_break, parent, false);
        TitleBreakView titleBreakView = new TitleBreakView();
        titleBreakView.title = (TextView)(view.findViewById(R.id.title));
        view.setTag(titleBreakView);
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("AdditionalInfo")) {
        view = LayoutInflater.from(context).inflate(R.layout.additional_info, parent, false);
        AdditionalInfoView additionalInfoView = new AdditionalInfoView();
        additionalInfoView.title = (TextView)(view.findViewById(R.id.title));
        view.setTag(additionalInfoView);
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("Address")) {
        view = LayoutInflater.from(context).inflate(R.layout.address, parent, false);
        AddressView addressView = new AddressView();
        addressView.title = (TextView)(view.findViewById(R.id.title));
        addressView.address = (TextView)(view.findViewById(R.id.address));
        view.setTag(addressView);
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("AppLink")) {
        view = LayoutInflater.from(context).inflate(R.layout.app_link, parent, false);
        AppLinkView appLinkView = new AppLinkView();
        appLinkView.title = (TextView)(view.findViewById(R.id.title));
        view.setTag(appLinkView);
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("Paragraph")) {
        view = LayoutInflater.from(context).inflate(R.layout.paragraph, parent, false);
        ParagraphView paragraphView = new ParagraphView();
        paragraphView.paragraph = (TextView)(view.findViewById(R.id.paragraph));
        view.setTag(paragraphView);
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("Space")) {
        view = LayoutInflater.from(context).inflate(R.layout.space, parent, false);
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("TextWithBullets")) {
        view = LayoutInflater.from(context).inflate(R.layout.text_with_bullets, parent, false);
        TextWithBulletsView textWithBulletsView = new TextWithBulletsView();
        textWithBulletsView.text = (TextView)(view.findViewById(R.id.text));
        textWithBulletsView.bullet1 = (TextView)(view.findViewById(R.id.bullet1));
        textWithBulletsView.bullet2 = (TextView)(view.findViewById(R.id.bullet2));
        textWithBulletsView.bullet3 = (TextView)(view.findViewById(R.id.bullet3));
        textWithBulletsView.bullet4 = (TextView)(view.findViewById(R.id.bullet4));
        textWithBulletsView.bullet5 = (TextView)(view.findViewById(R.id.bullet5));
        view.setTag(textWithBulletsView);
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("WebLink")) {
        view = LayoutInflater.from(context).inflate(R.layout.web_link, parent, false);
        WebLinkView webLinkView = new WebLinkView();
        webLinkView.title = (TextView)(view.findViewById(R.id.title));
        view.setTag(webLinkView);
    }

    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    if (cursor.getString(cursor.getColumnIndex("type")).equals("Phone")) {
        PhoneView phoneView = (PhoneView) view.getTag();
        phoneView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
        phoneView.phone.setText(cursor.getString(cursor.getColumnIndex("data1")));
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("TitleBreak")) {
        TitleBreakView titleBreakView = (TitleBreakView) view.getTag();
        titleBreakView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("AdditionalInfo")) {
        AdditionalInfoView additionalInfoView = (AdditionalInfoView) view.getTag();
        additionalInfoView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("Address")) {
        AddressView addressView = (AddressView) view.getTag();
        addressView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
        addressView.address.setText(cursor.getString(cursor.getColumnIndex("data1")));
        //TODO-Properly format the address
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("AppLink")) {
        AppLinkView appLinkView = (AppLinkView) view.getTag();
        appLinkView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("Paragraph")) {
        ParagraphView paragraphView = (ParagraphView) view.getTag();
        paragraphView.paragraph.setText(cursor.getString(cursor.getColumnIndex("data1")));
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("Space")) {
        //Does something need to go here? It's just a spacer!
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("TextWithBullets")) {
        TextWithBulletsView textWithBulletsView = (TextWithBulletsView) view.getTag();
        textWithBulletsView.text.setText(cursor.getString(cursor.getColumnIndex("title")));
        textWithBulletsView.bullet1.setText(cursor.getString(cursor.getColumnIndex("data1")));
        textWithBulletsView.bullet2.setText(cursor.getString(cursor.getColumnIndex("data2")));
        textWithBulletsView.bullet3.setText(cursor.getString(cursor.getColumnIndex("data3")));
        textWithBulletsView.bullet4.setText(cursor.getString(cursor.getColumnIndex("data4")));
        textWithBulletsView.bullet5.setText(cursor.getString(cursor.getColumnIndex("data5")));
    } else if (cursor.getString(cursor.getColumnIndex("type")).equals("WebLink")) {
        WebLinkView webLinkView = (WebLinkView) view.getTag();
        webLinkView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
    }
}

}

我知道了 上面的实现是完全正确的。 问题是,因为我使用名称R.id.title来使所有这些不同的布局保持一致,这使我忘记为该特定布局添加R.id.title TextView。 我以为我先检查了一下,但是有太多不同的布局,我认为我看错了。 以下是我检查以确保问题不在光标上的方式:

if (!cursor.isNull(cursor.getColumnIndex("title"))) {
    additionalInfoView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM