extending navigation drawer activity to other activities

前端 未结 6 1089

I\'m trying to create a navigation drawer activity so I can extend that activity and use the menu in all activities by following the answer given in this question Link but m

相关标签:
6条回答
  • 2020-12-10 06:01

    As suggested by oli, you need to call setContentView(R.layout.activity_view) in your main activity.

    BaseActivity's onCreate must be called once content view in Main Activity has been set as shown below :

    public class BaseActivity extends Activity {
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // REST Navigation drawer initialization
        }
    
    }
    
    
    
    public class MainActivity extends BaseActivity {
    
        protected void onCreate(Bundle savedInstanceState) {
    
            setContentView(R.layout.activity_view);
            super.onCreate(savedInstanceState);
    
            // REST APPLICATION CODE
        }
    }
    
    0 讨论(0)
  • 2020-12-10 06:04

    In the main activity you have to call super.onCreate(savedInstanceState) after setContentView()

    0 讨论(0)
  • 2020-12-10 06:12

    In you MainActivity just put set setContentView(R.layout.activity_main) before

    super.onCreate(savedInstanceState);
    

    like below:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_profile);
        super.onCreate(savedInstanceState);
    }
    
    0 讨论(0)
  • 2020-12-10 06:22

    On the parent classBaseActivity:

    • declare a protected abstract int getLayoutId();

    • onCreate() I added the line

      setContentView(getLayoutId());
      

    Now implement it on your subclass MainActivity something like:

     protected int getLayoutId() {
        return R.layout.activity_profile;
    }
    

    Please tell me if you know a better solution.

    0 讨论(0)
  • 2020-12-10 06:23

    Your xml file should look something like this, taken from the android website: http://developer.android.com/training/implementing-navigation/nav-drawer.html

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <!-- Add content here -->
        </FrameLayout>
    
        <!-- The navigation drawer -->
        <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="#111"/>
    </android.support.v4.widget.DrawerLayout>
    
    0 讨论(0)
  • 2020-12-10 06:25

    The problem appears that the base class is not aware of the layout. Not sure how or why it worked for others without having to do something like this.. but I am new to Android.

    I was getting the same error and solved by passing my layout to the base via onCreate.

    So in the base, I modified onCreate to be like this:

    protected void onCreate(Bundle savedInstanceState, int resLayoutID)
    {
        super.onCreate(savedInstanceState);
        setContentView(resLayoutID);
    

    Then, from my new activity, I have this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState, R.layout.my_activity);
    

    This code in my base now works instead of staying null:

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerList = (ListView) findViewById(R.id.left_drawer);
    

    btw, this result was suggested to me by a friend and it worked for me.

    0 讨论(0)
提交回复
热议问题