Android通过单击按钮删除动态创建的微调器和按钮

时间:2014-01-18 06:56:17

标签: java android button dynamic layout-inflater

我使用layout-inflater动态创建了微调器和按钮。当我尝试通过单击按钮删除它们时,我得到NullPointer异常错误,并且程序在我运行时崩溃。这是我的代码:

public class MainActivity extends FragmentActivity implements OnClickListener{
SampleAlarmReceiver alarm = new SampleAlarmReceiver();
static EditText startTime;
static EditText endTime;
static EditText startDate;
static EditText EndDate;
View buttonRem;
Spinner spinnerNew;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startTime = (EditText)findViewById(R.id.EditTextST);  
    startDate = (EditText)findViewById(R.id.editTextSD); 

    //Set up Click Listener for all Buttons
    View buttonAdd = findViewById(R.id.button1);
    buttonAdd.setOnClickListener(this);
            buttonRem = findViewById(R.id.buttonRem);
    buttonRem.setOnClickListener(this);

}

public void onClick(View v)
{
    switch(v.getId()){
    case (R.id.button1):

        RelativeLayout rootLayout = (RelativeLayout)findViewById(R.id.main_layout);

        //use layout inflater to create 'spinnerNew' on the fly
        final LayoutInflater spnInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        spinnerNew = (Spinner)spnInflater.inflate(R.layout.extra_spinner, null);
        rootLayout.addView(spinnerNew);

        //move the dynamic spinner to different position
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)spinnerNew.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params.addRule(RelativeLayout.BELOW, R.id.spinner1);

        spinnerNew.setLayoutParams(params); //causes layout update

        //use layout inflater to create 'remove button' on the fly
        final LayoutInflater btnInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        buttonRem = (Button)btnInflater.inflate(R.layout.btn_remove, null);
        rootLayout.addView(buttonRem);

        //move the dynamic button to different position
        RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)buttonRem.getLayoutParams();
        params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params1.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.spinnerNew);
    break;

    case(R.id.buttonRem):
        //spinnerNew.setVisibility(View.GONE); //does nothing
        //buttonRem.setVisibility(View.GONE);//does nothing
        ViewGroup layout = (ViewGroup) spinnerNew.getParent();
        if(null!=layout) 
           layout.removeView(spinnerNew);//does nothing
    break;
    }
}

2 个答案:

答案 0 :(得分:1)

   buttonRem = findViewById(R.id.buttonRem);
    buttonRem.setOnClickListener(this);

在布局通胀之前,上面会被调用。 从onCreate中删除 将其移至:

public void onClick(View v)
{
    switch(v.getId()){
    case (R.id.button1):

        RelativeLayout rootLayout = (RelativeLayout)findViewById(R.id.main_layout);

        //use layout inflater to create 'spinnerNew' on the fly
        final LayoutInflater spnInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        spinnerNew = (Spinner)spnInflater.inflate(R.layout.extra_spinner, null);
        rootLayout.addView(spinnerNew);

        //move the dynamic spinner to different position
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)spinnerNew.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params.addRule(RelativeLayout.BELOW, R.id.spinner1);

        spinnerNew.setLayoutParams(params); //causes layout update

        //use layout inflater to create 'remove button' on the fly
        final LayoutInflater btnInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        buttonRem = (Button)btnInflater.inflate(R.layout.btn_remove, null);
        rootLayout.addView(buttonRem);

        //move the dynamic button to different position
        RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)buttonRem.getLayoutParams();
        params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params1.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.spinnerNew);
        buttonRem.setLayoutParams(params1);
        buttonRem.setOnClickListener(this);//<-------------set onclick here
    break;

    case(R.id.buttonRem):
        //spinnerNew.setVisibility(View.GONE); //does nothing
        //buttonRem.setVisibility(View.GONE);//does nothing
        ViewGroup layout = (ViewGroup) spinnerNew.getParent();
        if(null!=layout) 
           layout.removeView(spinnerNew);//does nothing
    break;
    }
}

答案 1 :(得分:0)

实际上,我发现了问题 - 在第二种情况下你的spinnerNew引用为空(你没有绑定R.layout.extra_spinner),尽管你正在使用它。在第二种情况下尝试这样的事情:

case(R.id.buttonRem):
    final LayoutInflater spnInflater = (LayoutInflater) Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Spinner spinnerNew = (Spinner)spnInflater.inflater(R.layout.extra_spinner);
    spinnerNew.setVisibility(View.GONE);
    v.setVisibility(View.GONE);
    ViewGroup layout = (ViewGroup) spinnerNew.getParent();
    if(layout != null)
        layout.removeView(spinnerNew);
    break;