I am trying to make a simple game. When I am trying to set the text of 3 TextViews
I get a NullPointerException
.
public class MainActivity extends Activity implements View.OnClickListener {
private void initRound() {
countdown = 10;
ViewGroup container = (ViewGroup) findViewById(R.id.container);
container.removeAllViews();
WimmelView wv = new WimmelView(this);
container.addView(wv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
wv.setImageCount(8*(10 + round));
fillTextView(R.id.points, Integer.toString(points));
fillTextView(R.id.round, Integer.toString(round));
fillTextView(R.id.countdown, Integer.toString(countdown*1000));
}
private void fillTextView(int id, String text) {
TextView tv = (TextView) findViewById(id);
tv.setText(text); //HERE DO I GET THE EXCEPTION
}
}
Here is the error from logcat:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.developer.nico.kissthefrognow, PID: 1940
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.developer.nico.kissthefrognow.MainActivity.fillTextView(MainActivity.java:80)
at com.developer.nico.kissthefrognow.MainActivity.initRound(MainActivity.java:34)
at com.developer.nico.kissthefrognow.MainActivity.newGame(MainActivity.java:21)
at com.developer.nico.kissthefrognow.MainActivity.startGame(MainActivity.java:67)
at com.developer.nico.kissthefrognow.MainActivity.onClick(MainActivity.java:58)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
And my 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=".MainActivity"
android:id="@+id/rl">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/container">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000000"
android:id="@+id/points"
android:layout_gravity="left|top"
android:textColor="@color/Red" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/round"
android:layout_gravity="right|top"
android:textColor="@color/Red" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00000"
android:id="@+id/countdown"
android:layout_gravity="center_horizontal|bottom"
android:textColor="@color/Red" />
</FrameLayout>
Any ideas how to solve it? Thanks!