Android:“扩展BaseActivity”不执行BaseActivity方法

时间:2014-08-12 08:43:15

标签: android extends drawer

我尝试在所有活动中出现两个滑动抽屉。因此,我构建了一个BaseActivity,它扩展了我的子类MainActivity。我使用this代码来组合布局。这很有效,两个布局都出现在我的孩子活动上。我为我的抽屉和一些onItemClick Listenere等实现了一个列表视图。但是这些都没有在我的子类中执行。我有一个空抽屉。扼杀每一个toast我放入任何BaseActivity的方法出现在屏幕上。

activity_base.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/act_content"
        android:layout_width="match_parent"
        android:layout_height="200dp" >
    </FrameLayout>

    <android.support.v4.widget.DrawerLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="de.trialar.mtb_werkstatt.BaseActivity" >

        <ListView
            android:id="@+id/drawerList"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#FF8800"
            android:divider="@null" >
        </ListView>
    </android.support.v4.widget.DrawerLayout>

</FrameLayout>

BaseActivity.java

public class BaseActivity extends ActionBarActivity implements
        OnItemClickListener {

    private DrawerLayout drawerLayout;
    private ListView listView;

    private ActionBarDrawerToggle drawerListener;
    private MyAdapter myAdapter;

    protected FrameLayout fullLayout;
    protected FrameLayout actContent;

    @Override
    public void setContentView(final int layoutResID) {
        fullLayout = (FrameLayout) getLayoutInflater().inflate(
                R.layout.activity_base, null); // Your base layout here
        actContent = (FrameLayout) fullLayout.findViewById(R.id.act_content);
        getLayoutInflater().inflate(layoutResID, actContent, true); 
        super.setContentView(fullLayout);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        listView = (ListView) findViewById(R.id.drawerList);
        myAdapter = new MyAdapter(this);
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(this);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerListener = new ActionBarDrawerToggle(this, drawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {
            @Override
            public void onDrawerClosed(View drawerView) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub

            }
        };
        drawerLayout.setDrawerListener(drawerListener);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
        drawerListener.syncState();
    }

    @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 boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (drawerListener.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        drawerListener.onConfigurationChanged(newConfig);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        selectItem(arg2);
        Toast.makeText(BaseActivity.this,
                myAdapter.getItem(arg2) + " angeklickt: " + arg2,
                Toast.LENGTH_LONG).show();
        switch (arg2) {
        case 0:
            startActivity(new Intent(this, MainActivity.class));
            break;
        case 1:
            startActivity(new Intent(this, Teileliste.class));
            break;
        case 2:
            startActivity(new Intent(this, Reinigung.class));
            break;
        default:
        }

    }

    public void selectItem(int position) {
        // TODO Auto-generated method stub
        listView.setItemChecked(position, true);

    }

    public void setTitle(String title) {
        // TODO Auto-generated method stub
        getSupportActionBar().setTitle(title);
    }

    class MyAdapter extends BaseAdapter {
        private Context context;
        String[] mainCatSites;
        int[] images = { R.drawable.ic_werkstatt, R.drawable.ic_teileliste,
                R.drawable.ic_reinigung };

        public MyAdapter(Context context) {
            this.context = context;
            mainCatSites = context.getResources().getStringArray(
                    R.array.mainCat);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mainCatSites.length;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return mainCatSites[arg0];
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            View row = null;
            if (arg1 == null) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.custom_row, arg2, false);
            } else {
                row = arg1;
            }
            TextView titleTextView = (TextView) row
                    .findViewById(R.id.tvRowList);
            ImageView titleImageView = (ImageView) row
                    .findViewById(R.id.imageView1);
            titleTextView.setText(mainCatSites[arg0]);
            titleImageView.setImageResource(images[arg0]);
            return row;
        }

    }

}

MainActivity.java

    public class MainActivity extends BaseActivity {

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

    }

}

1 个答案:

答案 0 :(得分:1)

您仍在调用super.setContentView()和THEN setContentView()。这意味着您要对R.layout.activity_base进行膨胀并检索其布局,然后使用activity_main替​​换整个布局。现在setContentView()再次膨胀R.layout.activity_base,这次没有初始化来自activity_base的视图。基本上,变量&#34; listView&#34;等等,参考你所替换的布局的视图。

要解决此问题,您需要将BaseActivity中的onCreate()替换为类似

的内容
protected final void initViews() { ... }

并删除里面的super.onCreate()和super.setContentView()。最后,只需添加initViews();低于setContentView(R.layout.activity_main);

这将确保您的变量中保存正确的视图。