Grayson Briggs Grayson Briggs - 5 months ago 19
Java Question

Android TextView troubles

Ok so i'm starting to learn Android and i cant append or set the text of the TextView.. i always get a NullPointerException.. anyone help me out?

Field variables

public TextView t = null;
public Button b = null;


My onCreate method

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
t = (TextView)findViewById(R.id.textView1);
b = (Button)findViewById(R.id.dummy_button);

//t.append("hello again");
setContentView(R.layout.activity_fullscreen);

final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content);

// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, contentView,
HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;

@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}

if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});


The listener i decided to put the append in..

contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("onClick", "before append");
t.append("ummmm" + System.currentTimeMillis());
Log.e("onClick", "after");
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});


Here is the .xml file that i use in layout

<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:background="#0099cc"
tools:context=".FullscreenActivity" >

<!--
The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc.
-->

<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />

<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows.
-->

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >

<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent" >

<Button
android:id="@+id/dummy_button"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button" />
</LinearLayout>

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="411dp"
android:text="Why Hello There" />

</FrameLayout>




R.java.. it is auto generated however.. here is inside R.java

public static final class id {
public static final int dummy_button=0x7f070002;
public static final int fullscreen_content=0x7f070000;
public static final int fullscreen_content_controls=0x7f070001;
public static final int textView1=0x7f070003;
}


Any help would be amazing! :) i am quite new to Android development

Answer

Your OnCreate should be like this,

             @Override
           protected void onCreate(Bundle savedInstanceState) {
                         super.onCreate(savedInstanceState);
                         setContentView(R.layout.xml);  

                         t = (TextView)findViewById(R.id.textView1);
                         b = (Button)findViewById(R.id.dummy_button);
                }