addView() method throwing Nullpointer Exception - android

I am trying to instantiate a row of Buttons Programatically using Table Layout
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private final int gridSize = 3;
private TableRow rowArr[] = new TableRow[gridSize];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create the layout
TableLayout MainLayout = new TableLayout(this);
MainLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
//MainLayout.setStretchAllColumns(true);
for ( int i =0 ; i < gridSize ; i++){
for(int j = 0; j < gridSize ; j++){
Button button = new Button(this);
button.setText(Integer.toString(i)+","+Integer.toString(j));
rowArr[i].addView(button);
}
MainLayout.addView(rowArr[i]);
}
//Set the view
setContentView(MainLayout);
}
However this line seems to throw a nullpointerexception
rowArr[i].addView(button);
What am i doing wrong?

you have initialized the rowArr array but not the individual TableRow element of it. so rowArr[i] will be null. in the for loop, put the following line in it:-
rowArr[i] = new TableRow(this);

Your TableRow is null as you haven't instantiate it.
try to instantiate it like this rowArr[i]=new TableRow();
for ( int i =0 ; i < gridSize ; i++){
rowArr[i]=new TableRow();
for(int j = 0; j < gridSize ; j++){
Button button = new Button(this);
button.setText(Integer.toString(i)+","+Integer.toString(j));
rowArr[i].addView(button);
}
MainLayout.addView(rowArr[i]);
}

Related

pass value from stringbuilder to textview

I am trying to get words from several EditTexts and display them using TextView without using any layout. But the program is force closed every-time I run it.
LOGCAT: java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.tablelayout/com.example.tablelayout.MainActivity}:
java.lang.NullPointerException
public class MainActivity extends Activity {
private List<EditText> editTextList = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(VERTICAL);
int count = 10;
linearLayout.addView(tableLayout(count));
linearLayout.addView(submitButton());
setContentView(linearLayout);
}
private Button submitButton() {
Button button = new Button(this);
button.setHeight(WRAP_CONTENT);
button.setText("Submit");
button.setOnClickListener(submitListener);
return button;
}
TextView txt=new TextView(this);
// Access the value of the EditText
private View.OnClickListener submitListener = new View.OnClickListener() {
public void onClick(View view) {
StringBuilder stringBuilder = new StringBuilder();
for (EditText editText : editTextList) {
stringBuilder.append(editText.getText().toString());
}
txt.setText(stringBuilder.toString().trim());
}
};
// Using a TableLayout as it provides you with a neat ordering structure
private TableLayout tableLayout(int count) {
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
int noOfRows = count / 5;
for (int i = 0; i < noOfRows; i++) {
int rowId = 5 * i;
tableLayout.addView(createOneFullRow(rowId));
}
int individualCells = count % 5;
tableLayout.addView(createLeftOverCells(individualCells, count));
return tableLayout;
}
private TableRow createLeftOverCells(int individualCells, int count) {
TableRow tableRow = new TableRow(this);
tableRow.setPadding(0, 10, 0, 0);
int rowId = count - individualCells;
for (int i = 1; i <= individualCells; i++) {
tableRow.addView(editText(String.valueOf(rowId + i)));
}
return tableRow;
}
private TableRow createOneFullRow(int rowId) {
TableRow tableRow = new TableRow(this);
tableRow.setPadding(0, 10, 0, 0);
for (int i = 1; i <= 5; i++) {
tableRow.addView(editText(String.valueOf(rowId + i)));
}
return tableRow;
}
private EditText editText(String hint) {
EditText editText = new EditText(this);
editText.setId(Integer.valueOf(hint));
editText.setHint(hint);
editTextList.add(editText);
return editText;
}
}
TextView txt=new TextView(this);
you can't declare and initialize a class member View at the same time, because the Context is not yet valid
change it to:
public class MainActivity extends Activity {
private List<EditText> editTextList = new ArrayList<EditText>();
private TextView txt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txt = new TextView(this);
// other code
also I noticed that you are not adding this View to any of the containers that your provided to setContentView. The suggestion will fix the crash but you will not see the TextView's content
Exception shows problem is in your manifest file.
Your package name is manifest tag and your activity are in different locations.

How to give a dynamic generated button an id?

Hello i used a code to generate some buttons in a tablelayout. The buttons are generated by this layout, so none of them has an id yet. So here is my question. How can i give a specific button an id? (For example the button in the first row, second colum should have the id "button2")
public class MainActivity extends ActionBarActivity {
private static final int NUM_ROWS = 3;
private static final int NUM_COLS = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateButtons();
}
private void populateButtons() {
TableLayout table = (TableLayout) findViewById(R.id.TableLayout);
for (int row =0; row < NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f));
table.addView(tableRow);
for (int col = 0; col < NUM_COLS; col++) {
Button button = new Button(this);
tableRow.addView(button);
}
}
}
You can use View.setId(), however I guess you don't have to find the Views by id because you already know them.

How to create a TableLayout in android dynamically?

Im trying to create a TableLayout dynamically. I want to create rows and columns of 3X3.
public class PortAFareActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout seatTable=new TableLayout(this);
ImageButton[][] seatButton=new ImageButton[3][3];
seatTable.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
TableRow[] seatRow=new TableRow[3];
for (int i = 0; i < 3; i++) {
seatRow[i]=new TableRow(this);
seatRow[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++) {
seatButton[i][j]=new ImageButton(this);
seatButton[i][j].setImageResource(R.drawable.seat);
seatButton[i][j].setLayoutParams(new LayoutParams(50,30));
seatRow[i].addView(seatButton[i][j]);
}
seatTable.addView(seatRow[i]);
}
LinearLayout lin=(LinearLayout)findViewById(R.id.linLayout);
lin.addView(seatTable,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
lin.invalidate();
Toast.makeText(this, " "+seatTable.getChildCount() , Toast.LENGTH_SHORT).show();
}
}
My Layout XML file contains a LinearLayout with id linLayout
Any help would be appreciated. =)
try this it will work
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
TableLayout seatTable=new TableLayout(this);
ImageButton[][] seatButton=new ImageButton[3][3];
seatTable.setBackgroundColor(Color.RED);
seatTable.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
TableRow[] seatRow=new TableRow[3];
for (int i = 0; i < 3; i++) {
seatRow[i]=new TableRow(this);
seatRow[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++) {
seatButton[i][j]=new ImageButton(this);
seatButton[i][j].setImageResource(R.drawable.seat);
//seatButton[i][j].setLayoutParams(new LayoutParams(50,30));//<--------problem
seatRow[i].addView(seatButton[i][j]);
}
seatTable.addView(seatRow[i]);
}
LinearLayout lin=(LinearLayout)findViewById(R.id.linLayout);
lin.addView(seatTable);//<-------- not the problem but not required
lin.invalidate();
Toast.makeText(this, " "+seatTable.getChildCount() , Toast.LENGTH_SHORT).show();
}

tablelayout delete tablerow in android

final TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
TableRow row = new TableRow(this);
TextView t2 = new TextView(this);
t2.setText("test");
row.addView(t2);
Button bu = new Button(this);
bu.setBackgroundResource(R.drawable.del);
bu.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//I need to delete the tablerow
//how to do?
}
});
row.addView(bu);
table.addView(row, new TableLayout.LayoutParams(WC, WC));
**
i want to delete tablerow in bu.setOnClickListener
how to do removeViewAt() ,i cant find indexId
**
use removeView for removing tablerow as:
table.removeView(row);
NOTE: If they don't have unique id then use:
table.removeView(rowIndex);
and by using removeViewAt
for(int i = 0, j < table.getChildCount(); i < j; i++){
// then, you can remove the the row you want...
// for instance...
TableRow row = getChildAt(i);
if( something you want to check ) {
removeViewAt(i);
// or...
removeView(row);
}
}

Tableview in android [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
show data in table view in android
how to data set in tableview.
how to show data in tableview.
A quick example:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TableRow.LayoutParams params = new TableRow.LayoutParams(100, 100);
TableLayout table = new TableLayout(this);
for (int i = 0; i < 3; i++) {
TableRow row = new TableRow(this);
for (int j = 0; j < 3; j++) {
TextView text = new TextView(this);
text.setLayoutParams(params);
text.setText(String.format("(%d, %d)", i, j));
row.addView(text);
}
table.addView(row);
}
setContentView(table);
}
}
If you can get away with it, GridView and ListView can be much easier to work with using adapters such as ArrayAdapter and CursorAdapter.

Categories

Resources