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)