trialar
trialar

Reputation: 129

Android: "extends BaseActivity" isnt executing Methods of BaseActivity

I try two get an Sliding Drawer appearing on all Activities. Therefore I built an BaseActivity which extends my child class MainActivity. I used this code for combining the layouts. This works well, both the layouts appear on my child activity. I implemented a list view for my drawer and some onItemClick Listenere etc. But none of these are executed in my child class. I got an empty drawer. Strangly every toast i put in whatever method of the BaseActivity appears on the screen.

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

    }

}

Upvotes: 1

Views: 2284

Answers (1)

0101100101
0101100101

Reputation: 5911

You're still calling super.setContentView() and THEN setContentView(). This means you are inflating R.layout.activity_base and retrieving its layouts, after which you replace the whole layout with activity_main. Now setContentView() inflates the R.layout.activity_base again, this time without initializing the views from activity_base. Basically, the variables "listView" and so on reference the views of the layout you replaced.

To fix this, you need to replace the onCreate() inside BaseActivity with something like

protected final void initViews() { ... }

and remove the super.onCreate() and super.setContentView() inside. Finally, just add initViews(); below setContentView(R.layout.activity_main);

This will make sure the correct views are saved in your variables.

Upvotes: 1

Related Questions