我已阅读了很多文档,并试图弄清楚为什么我的CardScrollAdapter实现在getView()方法中返回NullPointerException。事实证明,我缺少setItemOnCard() - 但是现在已经弃用了XE16,取而代之的是getPosition()
这就是我所拥有的:
@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的样子
<?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对象不符合某些要求。我在那里的值用于测试仍然会抛出错误。错误指向
message.setText("message");
答案 0 :(得分:1)
TextView
抛出NPE - setContentView()
是为了找到子视图对象而使布局膨胀的原因。在这种情况下,我设置
scrollAdapter = new AlertScrollAdapter();
cardScroller.setAdapter(scrollAdapter);
if(cardScroller != null && !cardScroller.isActivated()){
setContentView(cardScroller); <------
cardScroller.activate();
然后我用子对象扩充XML,使用新膨胀的视图来查找子视图对象。
@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;
}
我正在返回膨胀的视图而不是
Card card = new Card(context);
return card.getView(convertView, parent);