我在for循环(i ++)中以编程方式在片段中创建这样的单选按钮
for (int i = 1; i < priceList.length(); i++) {
JSONObject priceObj = (JSONObject) priceList.get(i);
String catName = priceObj.getString("categoryName");
String uid = i*(i+100)+20;
RadioButton rdbtn = new RadioButton(getActivity().getApplicationContext());
rdbtn.setId((row * 2) + i);
rdbtn.setText(catName);
rdbtn.setTextColor(R.color.colorPrimary);
ll.addView(rdbtn);
}
linearLayout.removeAllViews();
linearLayout.addView(ll);
我想将uid添加为标记,但如果我添加.setTag(uid),则会出现如下错误
致命的例外:主要 过程:com.vistaraitsolutions.hoteladmin,PID:13629 java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.RadioButton.setTag(java.lang.Object)&#39;在null对象引用上 在com.vistaraitsolutions.hoteladmin.FragmentStepThreeBooking.addRadioButtons(FragmentStepThreeBooking.java:226) 在com.vistaraitsolutions.hoteladmin.FragmentStepThreeBooking $ 1.onResponse(FragmentStepThreeBooking.java:161) 在com.vistaraitsolutions.hoteladmin.FragmentStepThreeBooking $ 1.onResponse(FragmentStepThreeBooking.java:157) 在com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:72) 在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:145) 在android.app.ActivityThread.main(ActivityThread.java:6856) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
并且tadio按钮还没有添加到线性布局,aftre loop ony im添加无线电组到布局!现在我需要设置标签。
这是使用setTag()
的代码for (int i = 1; i < priceList.length(); i++) {
JSONObject priceObj = (JSONObject) priceList.get(i);
String catName = priceObj.getString("categoryName");
String uid = i*(i+100)+20;
RadioButton rdbtn = new RadioButton(getActivity().getApplicationContext());
rdbtn.setId((row * 2) + i);
rdbtn.setText(catName);
//rdbtn.setTag(url); here im adding setTag
rdbtn.setTextColor(R.color.colorPrimary);
ll.addView(rdbtn);
}
linearLayout.removeAllViews();
linearLayout.addView(ll);
请帮帮我