Search code examples
androidtablelayoutfindviewbyid

Android TableLayout findViewByID returns null


First of all I want to mention, that already a lot of threads regarding such problems are around... but unfortunatelly no one could solve my problem.

I have two activities in my app: - the standard MainActivity with an XML which has a EditText and a Button. On top of that it has one menu button which is called ShowDB and directs to the second activity - the second activity called ShowDB, should dynamically create a table with the DB content (I only have one plain table with 3 columns ;) )

MainActivity opens ShowDB activity with:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
        //Opens a new activity to show the content of the WorkTimeTracker Database
        case R.id.showDB:
            Intent intent = new Intent(MainActivity.this, ShowDB.class);
            startActivity(intent);
            break;
        case R.id.action_settings:
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

The ShowDB.class:

    public class ShowDB extends ActionBarActivity {

    private LinearLayout linearLayout;
    private TableLayout tableLayout;
    private WorkTimeEntrySQLiteDAO wteDAO;
    private static Logger logger = Logger.getAnonymousLogger();

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        //init of the table Layout
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
        tableLayout = (TableLayout) findViewById(R.id.tableLayout1);
        //init of DB access
        wteDAO = new WorkTimeEntrySQLiteDAO(this);
        //creation of the table
        buildTable();

    }

    private void buildTable(){
        logger.log(Level.INFO, "TRY TO DOWNLOAD WORK TIME ENTRIES.....");
        ArrayList<WorkTimeEntry> wtes = (ArrayList<WorkTimeEntry>) wteDAO.findAll();
        logger.log(Level.INFO, "DOWNLOADED ALL WORK TIME ENTRIES!!!!!!!!!");
        int rows = wtes.size();
        int cols = 3;

        for(int i = 0; i < rows; i++){
            WorkTimeEntry wte = wtes.get(i);
            TableRow row = new TableRow(this);
            row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            for(int j = 0; j < cols; j++){
                TextView tv = new TextView(this);
                tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                tv.setBackgroundResource(R.drawable.cell_shape);
                tv.setGravity(Gravity.CENTER);
                tv.setTextSize(14);
                tv.setPadding(0, 5, 0, 5);
                switch(j){
                case 0:
                    String _id = "" + wte.getId();
                    tv.setText(_id);
                    logger.log(Level.INFO, "ID from" + j + " = " + _id);
                    break;
                case 1:
                    tv.setText(wte.getInAsString());
                    logger.log(Level.INFO, "CHECKIN: " + wte.getInAsString());
                    break;
                case 2:
                    tv.setText(wte.getOutAsString());
                    logger.log(Level.INFO, "CHECKOUT: " + wte.getOutAsString());
                    break;
                }
            }
            tableLayout.addView(row);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.show_db, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_show_db,
                    container, false);
            return rootView;
        }
    }

}

And here's my XML file (fragment_show_db.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:shrinkColumns="*"
        android:stretchColumns="*" >
    </TableLayout>

</LinearLayout>

Can somebody see the problem?

Thanks a lot for your help in advance,

Patrick


Solution

  • setContentView(R.layout.activity_show_db); In this line, you write "activity_show_db" and your xml name is "fragment_show_db". Do you have the layout "activity_show_db"?