I want to set my textviews programatically, because of a specific context in each one which is based on calculations that changes every time app is launched. Their names are textView1, textView2, textView3...
and I want to set them up in a loop, but I got this error:
java.lang.RuntimeException:
Unable to resume activity {com.test.base/com.test.base.MyActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
My loop:
int i = 1;
for (i = firstDayNumber; i <= daysInMonth; i++) {
TextView tv1 = (TextView)findViewById(R.id.textView + i);
tv1.setText("Hello");
}
XML:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_column="0"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="14dp"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:layout_marginLeft="4dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:background="@drawable/field"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_column="0"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="14dp"
android:layout_weight="1"
android:layout_marginTop="12dp"
android:layout_marginLeft="4dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:background="@drawable/field"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_column="0"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="14dp"
android:layout_weight="1"
android:layout_marginTop="22dp"
android:layout_marginLeft="4dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:background="@drawable/field"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_column="0"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="14dp"
android:layout_weight="1"
android:layout_marginTop="32dp"
android:layout_marginLeft="4dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:background="@drawable/field"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_column="0"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="14dp"
android:layout_weight="1"
android:layout_marginTop="42dp"
android:layout_marginLeft="4dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:background="@drawable/field"/>
</RelativeLayout>
Another error:
11-16 00:02:45.484 15603-15603/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
11-16 00:02:45.484 15603-15603/? E/android.os.Debug﹕ failed to load memtrack module: -2
11-16 00:02:45.739 1229-1252/system_process E/WindowState﹕ getStack: Window{169349a0 u0 Starting com.moneyfount.base} couldn't find taskId=869 Callers=com.android.server.wm.WindowState.getDisplayContent:782 com.android.server.wm.WindowState.getWindowList:1354 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2774 com.android.server.wm.WindowManagerService.removeWindowLocked:2675
11-16 00:02:45.739 1229-1252/system_process E/WindowState﹕ getStack: Window{169349a0 u0 Starting com.moneyfount.base} couldn't find taskId=869 Callers=com.android.server.wm.WindowState.getDisplayContent:782 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2779 com.android.server.wm.WindowManagerService.removeWindowLocked:2675 com.android.server.wm.WindowManagerService.removeWindow:2597
11-16 00:02:46.339 15620-15620/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
11-16 00:02:46.340 15620-15620/? E/android.os.Debug﹕ failed to load memtrack module: -2
11-16 00:02:46.660 1229-1498/system_process E/WindowManager﹕ Performed 6 layouts in a row. Skipping
11-16 00:02:46.789 1229-1492/system_process E/WindowManager﹕ Performed 6 layouts in a row. Skipping
11-16 00:02:46.799 1229-1495/system_process E/WindowManager﹕ Performed 6 layouts in a row. Skipping
11-16 00:02:46.847 1229-1252/system_process E/WindowManager﹕ Performed 6 layouts in a row. Skipping
11-16 00:02:46.870 1229-1381/system_process E/WindowManager﹕ Performed 6 layouts in a row. Skipping