-2

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
Joe.S
  • 1
  • 1

2 Answers2

1

That's not the way to get resources dynamically. You should do something like this:

 int resId = getResources().getIdentifier("textView" + i, "id", this.getPackageName());

Try it like this:

for (i = 1; i <= 5; i++) {
      int resId = getResources().getIdentifier("textView" + i , "id", this.getPackageName());
      TextView t = (TextView)findViewById(resId);
      t.setText("Hello");
}
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
  • I added XML to my question. There is one more line no the top - `int firstDayNumber = some calculations;` – Joe.S Nov 15 '15 at 22:33
  • What are the values of firstDayNumber and daysInMonth? – SuperFrog Nov 15 '15 at 22:35
  • firstDayNumber = between 9 and 16, daysInMonth = between 28 and 31 – Joe.S Nov 15 '15 at 22:37
  • You have 5 TextViews, numberd from 1 to 5. Your loop starts at 9, so how you'll find textView1 this way? and also, your loop, might have much more then 5 iterations, so what will happen then? – SuperFrog Nov 15 '15 at 22:44
  • :D okay, got it. But I set firstDayNumber to 1 and daysInMonth to 5 and still got same error. – Joe.S Nov 15 '15 at 22:47
0

You can't get the textviews like that. Do it with 'Hashmap'. Put your images to hashmap and in your loop get hashmap items. It is the best way.

private Map< Integer, Integer> map = new HashMap<>();
    map.put(0, R.drawable.p1);
    map.put(1, R.drawable.p10);
    map.put(3, R.drawable.p11);
    map.put(4, R.drawable.p12);
    map.put(5, R.drawable.p13);
    map.put(6, R.drawable.p3n);
    map.put(7, R.drawable.p4);
    map.put(8, R.drawable.p5);

and in loop do:

map.get(i);

It reurns the drawables.