我有一个应用程序,我在一个放射组中有Present
和Absent
单选按钮的布局。
我将上述布局动态添加到另一个布局一段特定次数。在我添加的同时,我还分别为Present
和Absent
维护了一个radioButtons列表,因为我想在用户按下按钮后勾选所有Present
单选按钮。
这是我的动态布局部分,带有单选按钮:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:weightSum="10">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/RollNum"
android:textSize="20dp"
android:textColor="@color/black"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="6"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Amandeep Singh and some very very long name"
android:gravity="center"
android:id="@+id/Name"
android:textSize="20dp"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center">
<RadioGroup
android:id="@+id/radioAttendance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation='horizontal'
android:gravity="center">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P"
android:id="@+id/PresentCheck"
android:textSize="10dp"
android:gravity="center"
android:textColor="@color/black"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:id="@+id/AbsentCheck"
android:textSize="15dp"
android:gravity="center"
android:textColor="@color/black"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1">
<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:background="#ffffff" />
</LinearLayout>
</LinearLayout>
以下是我将代码添加到列表中的代码:
List<RadioButton> presentRadioList;
List<RadioButton> absentRadioList;
LoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout scrollViewLinearlayout = (LinearLayout)findViewById(R.id.parent); // The layout inside scroll view
//int count=50;
Studentlist = Arrays.copyOfRange(StudentlistInterim, 1, StudentlistInterim.length);
for(int i = 0; i < Studentlist.length; i++){
String data = Studentlist[i];
String RollNum = data.split("--")[0];
String Name = data.split("--")[1];
Log.d("Name is:", Name);
LinearLayout layout2 = new LinearLayout(context);
layout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
View item = inflater.inflate(R.layout.item_layout, null, false);
layout2.addView(item);
layout2.setId(i);
TextView trollnum = (TextView) item.findViewById(R.id.RollNum);
trollnum.setText(RollNum);
TextView tname = (TextView) item.findViewById(R.id.Name);
tname.setText(Name);
RadioButton rPresent = (RadioButton)item.findViewById(R.id.PresentCheck);
RadioButton rAbsent = (RadioButton)item.findViewById(R.id.AbsentCheck);
//rPresent.setId(i);
//rAbsent.setId(Studentlist.length+i); // to make the id unique for each present and absent entry
presentRadioList.add(rPresent);
absentRadioList.add(rAbsent);
scrollViewLinearlayout.addView(layout2);
Log.d("Id is: ", trollnum.getText().toString());
}
我面临的问题是我的活动失败并出现以下异常:
Caused by: java.lang.NullPointerException
at com.example.amandeepsingh.loginregisterandother.TakeAttendance.onCreate(TakeAttendance.java:128)
presentRadioList.add(rPresent);
absentRadioList.add(rAbsent);
请帮忙,让我知道我错过了什么。
答案 0 :(得分:2)
您似乎尚未初始化presentRadioList
。尝试编辑类似List<RadioButton> presentRadioList = new ArrayList<RadioButton>();