首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CardScrollView w/ xml布局

CardScrollView w/ xml布局
EN

Stack Overflow用户
提问于 2014-05-28 02:17:41
回答 1查看 485关注 0票数 0

我阅读了很多文档,试图弄清楚为什么我的CardScrollAdapter实现在getView()方法中返回一个NullPointerException。结果,我遗漏了setItemOnCard() --但是,由于XE16被getPosition()取代,现在已经不推荐使用它了。

这就是我所拥有的:

代码语言:javascript
复制
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Card card = new Card(context);
        if(convertView == null){
            //Inflate the layout to hold the card
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.alert_card, parent);
        }
        //populate the view
        TextView machine = (TextView) findViewById(R.id.alertMachine);
        TextView message = (TextView) findViewById(R.id.alertMessage);
        TextView id = (TextView) findViewById(R.id.alertId);
        TextView time = (TextView) findViewById(R.id.timestamp);
        message.setText("message");
        machine.setText("machine");
        id.setText("id");
        time.setText("time");
        return card.getView(convertView, parent);

我只是简单地尝试使用在XML布局中定义的卡来加载ScrollView。

下面是XML的外观

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <RelativeLayout
        android:id="@+id/body_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/glass_card_body_height"
        android:layout_marginLeft="@dimen/glass_card_margin"
        android:layout_marginTop="@dimen/glass_card_margin"
        android:layout_marginRight="@dimen/glass_card_margin"
        tools:ignore="UselessLeaf"
        >

        <!-- Put your widgets inside this RelativeLayout. -->

        <TextView
            android:id="@+id/alertMachine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="17dp"
            android:layout_marginTop="18dp"
            android:text="No new alert"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/alertMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/alertMachine"
            android:layout_centerVertical="true"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/footer_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|left"
        android:layout_marginLeft="@dimen/glass_card_margin"
        android:layout_marginBottom="@dimen/glass_card_footer_margin"
        android:layout_marginRight="@dimen/glass_card_margin"
        android:orientation="horizontal"
        >

        <!-- The footer view will grow to fit as much content as possible while the
             timestamp view keeps a fixed width. If the footer text is too long, it
             will be ellipsized with a 40px margin between it and the timestamp. -->

        <TextView
            android:id="@+id/alertId"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceSmall"
            />

        <TextView
            android:id="@+id/timestamp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/glass_card_margin"
            android:ellipsize="end"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceSmall"
            />

    </LinearLayout>

</FrameLayout>

由于我使用JSONObject填充视图,因此Card对象不满足某些要求。我所拥有的值是用于测试的,它们仍然会抛出错误。错误指向

代码语言:javascript
复制
message.setText("message");
EN

回答 1

Stack Overflow用户

发布于 2014-05-28 20:59:29

NPE是由TextView抛出的- setContentView()是用来膨胀布局以便找到子视图对象的。在本例中,我设置了

代码语言:javascript
复制
    scrollAdapter = new AlertScrollAdapter();
    cardScroller.setAdapter(scrollAdapter);
    if(cardScroller != null && !cardScroller.isActivated()){
        setContentView(cardScroller); <------
        cardScroller.activate();

然后使用子对象膨胀XML,使用新膨胀的视图查找子视图对象。

代码语言:javascript
复制
       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                Log.d(LOG_TAG, "Inflating view");
                //Inflate the layout to hold the card
                LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.alert_card, parent);
            }
            //populate the view
            TextView machine = (TextView) convertView.findViewById(R.id.alertMachine);
            TextView message = (TextView) convertView.findViewById(R.id.alertMessage);
            TextView id = (TextView) convertView.findViewById(R.id.alertId);
            TextView time = (TextView) convertView.findViewById(R.id.timestamp);


            if(cardList.size() != 0){
                try {
                    message.setText(cardList.get(position).getString(MESSAGE));
                    machine.setText(cardList.get(position).getString(MACHINE));
                    id.setText(cardList.get(position).getString(ID));
                    time.setText(cardList.get(position).getString(TIME));
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else{

            }
            return convertView;
        }

我返回的是放大视图,而不是

代码语言:javascript
复制
Card card = new Card(context);
return card.getView(convertView, parent);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23896301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档