- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做的是使用嵌入式布局在 Google Glass 中使用 CardScrollView 插入卡片,因为表格布局在卡片生成器中尚不可用。假设每个表都有 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 布局2
<?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);
}
}
请帮忙,因为我被困在这里好几天了。谢谢你!
最佳答案
您为各种 View 设置文本的方式存在一些问题。
它崩溃的原因是因为 CardScrollView
当前一次检索 5 个 Views
,当前一个和两个方向。由于 CardScrollView
从位置 0 开始,因此仅加载位置 0、1 和 2 的 Views
:这使得 tv1
的设置文本,tv2
和 tv2
可能,因为它们在 View 层次结构中。
由于 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>
适配器
:
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");
}
关于java - 使用 CardScrollView 的 Google Glass 多嵌入式布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27913744/
我在我的项目中使用了 CardScrollView 和 CardFrame,但是 CardScrollView 不能滚动,这是我的代码,有什么建议吗? CardScrollViewActivity.j
我有点卡在这里, 我刚开始学习 Glass 开发的基础知识,是否可以将 CardScrollView/Card 添加到 Livecard? 看起来只能将 RemoteView 添加到 LiveCard
我正在使用上面有很多卡片的 CardScrollView。问题是,我想使用语音命令来控制它们。但是,根据卡的不同,应该可以使用不同的语音命令。我无法更改每张特定卡片的上下文语音命令菜单。我尝试使用我将
我们如何向 CardScrollView 添加字母指示器,就像 google glass 联系人列表一样。 我目前使用比较语句对我的列表进行排序,并且它们已正确放入。现在,当您用力滑动时(这样它在卡片
我有一个 CardScrollView里面有多个项目,我希望能够在一个项目上拉出一个菜单,类似于内置的时间轴。 我知道 Card 不能附加特定的菜单,所以我在 Activity 级别准备了菜单。 但是
我正在使用 CardScrollView 在沉浸式应用中呈现一堆内容,但我不想使用标准 Card,因为我想要更多布局控制。 我有一个 xml 布局文件,它定义了我想要的布局,但我无法弄清楚如何在代码中
我正在使用 CardScrollView 在沉浸式应用中展示一堆内容,我不想使用标准的 Card,因为我想要更多控制布局。 我有一个定义我想要的布局的 xml 布局文件,但我无法弄清楚如何在代码中创建
我有一个包含多张卡片的 CardScrollView。在卡片之间左右滑动。 有些卡片上有很多内容。我使用了 ScrollView,因此用户可以滚动卡片以查看内容。 出于显而易见的原因,当用户滑动手指时
我有一个使用 CardScrollView 来显示一系列卡片的 Google Glass 应用程序。 触摸手势可以在卡片之间移动,但我需要能够根据索引跳转到特定的卡片。我想使用上下文语音命令(暂时忽略
我正在做的是使用嵌入式布局在 Google Glass 中使用 CardScrollView 插入卡片,因为表格布局在卡片生成器中尚不可用。假设每个表都有 1 个 textView。发生的事情是当我插
我们如何获取 CardScrollView 的当前位置或当前项?我还没有在 CardScrollView 或 CardScrollAdapter 上看到 getCurrentPosition() 或
所以我已经有了一个应用程序的小小开端,它可以将数据从 json 加载到 CardScrollView 中。有时这需要几秒钟,所以我需要一个加载屏幕。通常我会使用进度操作栏或旋转的进度条来执行此操作。我
我是一名优秀的程序员,十分优秀!