0

I am creating an Immersion type of app in Glass. I try to dynamically change a TextView in an Activity, but the program just crashes when I go to that Activity. I'd prefer not to use Cards since it is an immersive program.

Any idea how to get a TextView.setText to work in a regular activity?

Thanks!

Activity OnCreate Method

TextView tvTest = (TextView)findViewById(R.id.testText);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_checklist);
    mGestureDetector = createGestureDetector(this);
    readJson();
    tvTest.setText("testing testing");
}

Activity XML

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40px"
tools:context=".NewChecklistActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|center_horizontal"
    android:text="@string/newChecklist"
    android:textSize="50px"
    android:textStyle="bold" />

<TextView
    android:id="@+id/testText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


</FrameLayout>
The Nomad
  • 6,385
  • 12
  • 58
  • 98
  • Can you post some code? The way that you would inflate a layout and get the `TextView` widgets from it shouldn't be any different than you would do it on any other Android device, so the same techniques should work there. – Tony Allevato Dec 21 '13 at 00:05
  • @TonyAllevato added code. This way would normally work in any Android program I have used so confused as why it wouldn't work with Glass. – The Nomad Dec 21 '13 at 00:16

1 Answers1

1

Move this into onCreate after setContentView:

TextView tvTest = (TextView)findViewById(R.id.testText);

findViewByID() references the layout inflated by setContentView(). So it you use it beforehand, null is returned, hence a null pointer exception when you call a method on tvTest.

NigelK
  • 7,432
  • 2
  • 28
  • 28