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>
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top