I have read many documents and have banged my head trying to figure out why my implementation of CardScrollAdapter returns a NullPointerException in the getView() method. Turns out, I was missing setItemOnCard() - however this is now deprecated as of XE16 replaced by getPosition()
Here is what I have:
@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);
I am simply trying to load a ScrollView with a card defined in an XML layout.
Here is what the XML looks like
<?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>
The Card object does not meet some of the requirements since I am populating the view with a JSONObject. The values that I have there are for testing which still throw an error. The error is pointing towards
message.setText("message");