【问题标题】:Android: How do I add a checkbox by clicking a button?Android:如何通过单击按钮添加复选框?
【发布时间】:2014-02-25 10:14:27
【问题描述】:

我正在尝试通过按下我的“添加”按钮 (b1) 在我的线性布局中添加一个复选框。 我的应用程序在l1.addView(ckbx); 线上不断崩溃(日志显示“这里”而不是“这里 2”)。 有什么想法吗?

谢谢:)

这是我的代码:

package com.example.todolist;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

private LinearLayout l1;
private EditText e1;
private Button b1;
private Button b2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    l1 = (LinearLayout)findViewById(R.layout.activity_main);
    e1 = (EditText)findViewById(R.id.editText1);
    b1 = (Button)findViewById(R.id.button1);    
    b2 = (Button)findViewById(R.id.button2);

    // Buttons should register with listener
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick (View v) {

    switch(v.getId())
    {   
        // Starts Service.class 
        case R.id.button1: 
            Toast.makeText(this, "Add pressed", Toast.LENGTH_SHORT).show();
            // Create new check box in layout           
            CheckBox ckbx = createNewCheckBox(e1.getText().toString());
            Log.d("here","here now");
            l1.addView(ckbx);
            Log.d("here 2","here now 2");
                            setContentView(l1);

            break;

        case R.id.button2: 
            Toast.makeText(this, "Delete pressed", Toast.LENGTH_SHORT).show();
            break;
    }
}

// User defined method to create a new check box in the linear layout page
private CheckBox createNewCheckBox(String text) {
    final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final CheckBox checkBox = new CheckBox(this);
    checkBox.setLayoutParams(lparams);
    checkBox.setText(text);
    return checkBox;
}
}

【问题讨论】:

    标签: java android checkbox android-linearlayout add


    【解决方案1】:

    l1 = (LinearLayout)findViewById(R.layout.activity_main);

    改成

    l1 = (LinearLayout)findViewById(R.id.yourlinearlayoutidfromxml);
    

    您还应该将堆栈信息与问题一起发布,因为有关崩溃的相关信息已被记录。嫌疑人NullpointerException

    另外你有两次setContentView 用于同一个活动,这不是一个好的设计。

    setContentView(R.layout.activity_main);
    

    你已经有了

    l1 = (LinearLayout)findViewById(R.id.yourlinearlayoutidfromxml);
    

    你应该删除

    setContentView(l1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-13
      • 2013-12-24
      • 2020-06-08
      • 2015-07-02
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2021-08-17
      相关资源
      最近更新 更多