Reputation: 350
What im doing is I'm inserting card using CardScrollView in google glass using an EMBEDDED LAYOUT because the table layout is not yet available in card builder. Let's assume that every table has 1 textView. What's happening is when I insert 1-3 layout, there's is no any error but when i inserted the 4th layout it gives me a java.lang.NullPointerException in this line.
tv4.setText('Value for table 4');
This is my code:
NOTE: I have atleast 5 TextView in each table. make it simple here.
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>
Adapter
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);
}
}
Please help because I'm stuck in here for days. Thank you!
Upvotes: 1
Views: 418
Reputation: 6034
There a few things that are wrong with the way you are setting text for your various views.
The reason it crashes is because the CardScrollView
currently retrieves 5 Views
at a time, the current one and 2 for either directions. Since the CardScrollView
starts at position 0, only Views
at position 0, 1, and 2 are loaded: this makes setting text for tv1
, tv2
and tv2
possible as they are in the view hierarchy.
Since tv4
has not been added, trying to retrieve the View
will return null
.
Since each card in your CardScrollView
has the same layout, you don't need to create 4 separate ones and initiliaze them in onCreate
. You should instead inflate the Views
when needed and set the text or other data appropriately.
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;
}
}
Then, in your 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");
}
Upvotes: 2