I want to have AbstractMainActivity, which creates a navigation box. There I should also handle clicks on menu items, and then trigger new actions. In these steps, I want to use the same navigation box again.
I would expand into subclasses using AbstractMainActivity and call getLayoutResourceID differently from each subclass (as suggested here: android, how to create your own activity and extend it? ).
The problem is that now in AbstractMainActivity, where I want to create a navigation box, I have no access to the navigation bar layout element (xml), since of course I want to have a different base layout for the subclasses.
Do I need to “include a layout” in all subclass layout files? But it doesn’t work, what am I doing wrong if I want to use “Actions” instead of “Snippets” with a navigation box?
public abstract class MainActivity extends Activity { private String[] menuItems; private DrawerLayout mDrawerLayout; private ListView mDrawerList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
This is the layout of the subclass of the product list (activity_product_list.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".ProductList" > <include layout="@layout/activity_main"/> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > </ListView>
This is the layout of the navigation box (activity_main.xml):
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android1="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="300dp" android:layout_height="500dp" > <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#c3c3c3"/>
But this does not work, but if I don’t have it, I get null pointer exceptions when my subclass calls onCreate of the abstract class, where I want to build a navigation box, it does not find mock elements for setting lists and mock (R.id. left_drawer or R.id.drawer_layout)!
fischer_zh
source share