我想以编程方式设置我的文本视图,因为每个文本视图中的特定上下文基于每次启动应用程序时更改的计算。他们的名字是textView1, textView2, textView3...
,我想在循环中设置它们,但是我收到了这个错误:
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
我的循环:
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>
另一个错误:
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
答案 0 :(得分:1)
这不是动态获取资源的方法。你应该这样做:
int resId = getResources().getIdentifier("textView" + i, "id", this.getPackageName());
试试这样:
for (i = 1; i <= 5; i++) {
int resId = getResources().getIdentifier("textView" + i , "id", this.getPackageName());
TextView t = (TextView)findViewById(resId);
t.setText("Hello");
}
答案 1 :(得分:0)
您无法获得类似的观看次数。使用&#39; Hashmap&#39;进行操作。将您的图像放到hashmap并在循环中获取hashmap项。这是最好的方式。
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);
并在循环中执行:
map.get(i);
它重新出现了抽奖。