Android TableLayout findViewByID返回null

时间:2014-08-07 18:13:16

标签: android tablelayout findviewbyid

首先我想提一下,关于这些问题已经有很多线索......但不幸的是没有人可以解决我的问题。

我的应用中有两项活动:   - 标准的MainActivity,带有一个带有EditText和Button的XML。最重要的是,它有一个名为ShowDB的菜单按钮,并指向第二个活动   - 第二个名为ShowDB的活动,应该动态创建一个包含DB内容的表(我只有一个包含3列的普通表;)

MainActivity打开ShowDB活动:

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

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

}

这是我的XML文件(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>

有人能看到问题吗?

非常感谢您的帮助,

帕特里克

1 个答案:

答案 0 :(得分:1)

的setContentView(R.layout.activity_show_db);在这一行中,您编写“activity_show_db”,您的xml名称为“fragment_show_db”。你有布局“activity_show_db”吗?