-1

I'm trying to do the very basic operation of setting the content of a TextView. I've done this in the past (I'm basing this code on something that worked elsewhere), but in this specific situation, it keeps causing an error.

I'm also using Android Studio. Intellisence is picking up all the ids and stuff.

My layout file, activity_status.xml:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="my.package.path.StatusActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cash Machine"
        android:id="@+id/button_cash_machine"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="loadCashOutActivity" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Default"
        android:id="@+id/txtStatus"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp" />
</RelativeLayout>

The onCreate from my StatusActivity class:

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

    TextView v = (TextView) findViewById(R.id.txtStatus);
    v.setText("Changed...");

    setContentView(R.layout.activity_status);
}

LogCat error:

01-15 13:13:27.772    1918-1918/my.package.path E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: my.package.path., PID: 1918
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package.path/my.package.path.StatusActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
...
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at my.package.path.StatusActivity.onCreate(StatusActivity.java:25)
TechFanDan
  • 3,329
  • 6
  • 46
  • 89
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Jan 15 '15 at 18:51
  • @njzk2 I wasn't able to find my solution in that link. Plus, my issue was specific to Android. – TechFanDan Jan 15 '15 at 19:07

2 Answers2

3

you have this

TextView v = (TextView) findViewById(R.id.txtStatus);
v.setText("Changed...");

setContentView(R.layout.activity_status);

which is wrong, any views need to be set after you call setContentView

needs to look like this

setContentView(R.layout.activity_status);
TextView v = (TextView) findViewById(R.id.txtStatus);
v.setText("Changed...");
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • indeed... wasn't able to see this subtle difference in what worked and this that didn't. Thanks for the keen eye. – TechFanDan Jan 15 '15 at 18:56
1

You try to set find view before setContentView(..). You have to call setContentView(...) as first and then find your view

Change your code to:

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

    setContentView(R.layout.activity_status);

    TextView v = (TextView) findViewById(R.id.txtStatus);
    v.setText("Changed...");


}
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45