在Android中添加/删除RadioButton / EditText

时间:2014-02-16 23:55:14

标签: android

我试图让用户在我的应用中创建一个调查,但我正在努力处理我的一段代码。用户可以单击“添加答案”,它将添加RadioButton / EditText。然后单击“删除答案”,它应删除它们。它适用于删除添加的第一个Radiobutton / RditText,但是如果我已经添加了2个或更多的RadioButtons,则会抛出错误。

知道我的代码有什么问题吗?

mAddAnswer = (Button) v.findViewById(R.id.addAnswer);
mAddAnswer.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        RadioButton question = new RadioButton(getActivity().getApplicationContext());
        question.setId(newQuestionRadioId);
        newQuestionRadioId += 1;
        mAnswersGroup.addView(question);

        EditText answer = new EditText(getActivity().getApplicationContext());
        answer.setInputType(InputType.TYPE_CLASS_TEXT);
        answer.setId(newQuestionId);
        newQuestionId += 1;
        answer.setHint("Enter answer");
        mAnswerHolder.addView(answer);
        questionNumber += 1;
        }
    });

mRemoveAnswer = (Button) v.findViewById(R.id.removeAnswer);
mRemoveAnswer.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (mAnswersGroup.getChildCount() > 1) {
            mAnswersGroup.removeViewAt(newQuestionRadioId - 1);
            mAnswerHolder.removeViews(questionNumber - 1, questionNumber - 1);
            newQuestionId -= 1;
            newQuestionRadioId -= 1;
            questionNumber -= 1;
        }           
    }
});

logcat的:

 02-16 16:39:49.295: E/AndroidRuntime(2532): FATAL EXCEPTION: main
 02-16 16:39:49.295: E/AndroidRuntime(2532): java.lang.NullPointerException
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at android.view.ViewGroup.removeViewsInternal(ViewGroup.java:2231)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at android.view.ViewGroup.removeViews(ViewGroup.java:2179)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at com.example.surveymetest.AddQuestionFragment$2.onClick(AddQuestionFragment.java:106)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at android.view.View.performClick(View.java:2485)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at android.view.View$PerformClick.run(View.java:9080)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at android.os.Handler.handleCallback(Handler.java:587)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at android.os.Handler.dispatchMessage(Handler.java:92)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at android.os.Looper.loop(Looper.java:130)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at android.app.ActivityThread.main(ActivityThread.java:3683)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at java.lang.reflect.Method.invokeNative(Native Method)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at java.lang.reflect.Method.invoke(Method.java:507)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 02-16 16:39:49.295: E/AndroidRuntime(2532):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我认为由于removeViews

,可能会发生null ponter

方法removeViews在开发人员网站中定义,如

removeViews(int start, int count) 

参数在哪里

start  the first position in the group of the range of views to remove 
count  the number of views to remove  

计数应该是视图的否,您要从视图组中删除。