Android: How do I add a checkbox by clicking a button?

I am trying to add a checkbox to my linearlayout by pressing on my 'Add' button (b1). My app keeps crashing on line l1.addView(ckbx); (log dispays 'here' but not 'here 2'). Any ideas?

Thanks :)

This is the code I have:

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;
}
}

Answers


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

Change to

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

You should also post the stacktace along with the question as relevant info regarding crash is logged. Suspect NullpointerException.

Also you have setContentView twice for the same activity which is not a good design.

setContentView(R.layout.activity_main);

You already have

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

You should remove

setContentView(l1);

Need Your Help

print binary tree level by level in python

python python-3.4

I want to print my binary tree in the following manner:

route finding between two designation on maps in android?

android android-mapview android-maps android-location

i want to just find a shortest path between the location on map. we have to pass the location's geopoint then click the button to get direction. it will show the shortest path like a blue line. how...