即时通讯的作用是我使用EMBEDDED LAYOUT在Google Glass中使用CardScrollView插入卡片,因为表格布局在Card Builder中尚不可用。假设每个表都有1个textView。发生的是,当我插入1-3布局时,没有任何错误,但是当我插入第4个布局时,在此行中给了我java.lang.NullPointerException。

    tv4.setText('Value for table 4');


这是我的代码:

注意:我在每个表中至少有5个TextView。在这里使其简单。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    createCards();

    mCardScrollView = new CardScrollView(this);
    mAdapter = new ExampleCardScrollAdapter();
    mCardScrollView.setAdapter(mAdapter);
    mCardScrollView.activate();
    setContentView(mCardScrollView);


    TextView tv1 = (TextView) findViewById(R.id.textView1);
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    TextView tv3 = (TextView) findViewById(R.id.textView3);
    TextView tv4 = (TextView) findViewById(R.id.textView4);



    tv1.setText('Value for table 1');
    tv2.setText('Value for table 2');
    tv3.setText('Value for table 3');
    tv4.setText('Value for table 4');
    }

private void createCards(){
    mCards = new ArrayList<CardBuilder>();

     mCards.add(new CardBuilder(this, CardBuilder.Layout.EMBED_INSIDE)
    .setEmbeddedLayout(R.layout.activity_layout1)
    .setFootnote("TABLE 1"));

     mCards.add(new CardBuilder(this, CardBuilder.Layout.EMBED_INSIDE)
    .setEmbeddedLayout(R.layout.activity_layout2)
    .setFootnote("TABLE 2"));

     mCards.add(new CardBuilder(this, CardBuilder.Layout.EMBED_INSIDE)
    .setEmbeddedLayout(R.layout.activity_layout3)
    .setFootnote("TABLE 3"));

     mCards.add(new CardBuilder(this, CardBuilder.Layout.EMBED_INSIDE)
    .setEmbeddedLayout(R.layout.activity_layout4)
    .setFootnote("TABLE 4"));
    }

private class ExampleCardScrollAdapter extends CardScrollAdapter {

    @Override
    public int getPosition(Object item) {
        return mCards.indexOf(item);
    }

    @Override
    public int getCount() {
        return mCards.size();
    }

    @Override
    public Object getItem(int position) {
        return mCards.get(position);
    }

    @Override
    public int getViewTypeCount() {
        return CardBuilder.getViewTypeCount();
    }

    @Override
    public int getItemViewType(int position){
        return mCards.get(position).getItemViewType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return mCards.get(position).getView(convertView, parent);
    }
}


activity_layout1

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TableRow>

    <TextView
        android:padding="3dip"
        android:textSize="20sp"
        android:text="@string/textViewTitle1"
        android:textColor="@color/muted" />
    <TextView
        android:id="@+id/textView1"
        android:gravity="right"
        android:padding="3dip"
        android:textSize="20sp" />

</TableRow>
</TableLayout>


activity_layout2

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TableRow>

    <TextView
        android:padding="3dip"
        android:textSize="20sp"
        android:text="@string/textViewTitle2"
        android:textColor="@color/muted" />
    <TextView
        android:id="@+id/textView2"
        android:gravity="right"
        android:padding="3dip"
        android:textSize="20sp" />

</TableRow>
</TableLayout>


activity_layout3

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TableRow>

    <TextView
        android:padding="3dip"
        android:textSize="20sp"
        android:text="@string/textViewTitle4"
        android:textColor="@color/muted" />
    <TextView
        android:id="@+id/textView3"
        android:gravity="right"
        android:padding="3dip"
        android:textSize="20sp" />

</TableRow>
</TableLayout>


activity_layout4

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TableRow>

    <TextView
        android:padding="3dip"
        android:textSize="20sp"
        android:text="@string/textViewTitle4"
        android:textColor="@color/muted" />
    <TextView
        android:id="@+id/textView4"
        android:gravity="right"
        android:padding="3dip"
        android:textSize="20sp" />

</TableRow>
</TableLayout>


适配器

    private class ExampleCardScrollAdapter extends CardScrollAdapter {

    @Override
    public int getPosition(Object item) {
        return mCards.indexOf(item);
    }

    @Override
    public int getCount() {
        return mCards.size();
    }


    @Override
    public Object getItem(int position) {
        return mCards.get(position);
    }

    @Override
    public int getViewTypeCount() {
        return CardBuilder.getViewTypeCount();
    }

    @Override
    public int getItemViewType(int position){
        return mCards.get(position).getItemViewType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return mCards.get(position).getView(convertView, parent);
    }
}


请帮忙,因为我在这里待了几天。谢谢!

最佳答案

您为各种视图设置文本的方式有些错误。

为什么崩溃

之所以崩溃,是因为CardScrollView当前一次检索5个Views,当前任一方向检索2个。由于CardScrollView从位置0开始,因此仅加载位置0、1和2处的Views:这使得可以为tv1tv2tv2设置文本,因为它们在视图层次结构中。

由于尚未添加tv4,因此尝试检索View将返回null

如何解决这个问题

由于CardScrollView中的每个卡都具有相同的布局,因此您无需创建4个单独的卡并在onCreate中对其进行初始化。相反,应在需要时为Views充气,并适当设置文本或其他数据。

card_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TableRow>

    <TextView
        android:padding="3dip"
        android:textSize="20sp"
        android:text="@string/textViewTitle"
        android:textColor="@color/muted" />
    <TextView
        android:id="@+id/textView"
        android:gravity="right"
        android:padding="3dip"
        android:textSize="20sp" />

</TableRow>
</TableLayout>


Adapter

private class ExampleCardScrollAdapter extends CardScrollAdapter {

    @Override
    public int getPosition(Object item) {
        return mTexts.indexOf(item);
    }

    @Override
    public int getCount() {
        return mTexts.size();
    }


    @Override
    public Object getItem(int position) {
        return mTexts.get(position);
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public int getItemViewType(int position){
        return CardBuilder.EMBED_INSIDE.ordinal();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = new CardBuilder(this, CardBuilder.Layout.EMBED_INSIDE)
                .setEmbeddedLayout(R.layout.card_layout)
                .setFootnote("TABLE " + position)
                .getView(convertView, parent);
        TextView textView = view.findViewById(R.id.textView);

        textView.setText(mTexts.get(position));
        return view;
    }
}


然后,在您的Activity中:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    createTexts();

    mCardScrollView = new CardScrollView(this);
    mAdapter = new ExampleCardScrollAdapter();
    mCardScrollView.setAdapter(mAdapter);
    mCardScrollView.activate();  // This should be moved in onResume.
    setContentView(mCardScrollView);
}

private void createTexts(){
    mTexts = new ArrayList<String>();

    mTexts.add("Value for table 1");
    mTexts.add("Value for table 2");
    mTexts.add("Value for table 3");
    mTexts.add("Value for table 4");
}

10-07 19:35
查看更多