简体   繁体   English

当MainActivity(主活动)通过导航抽屉扩展BaseActivity时,其上的按钮不会注册点击

[英]Buttons on MainActivity do not register click when it(Main Activity) extends a BaseActivity with Navigation Drawer

I have a Base Activity which sets up Navigation drawer. 我有一个基本活动,它设置了导航抽屉。 My Main activity extends this BaseActivtity . 我的主要活动扩展了BaseActivtity The navigation drawer works perfectly fine but when I add any buttons on Main Activity, the onClick of the button doesn't get called. 导航抽屉工作正常,但是当我在Main Activity上添加任何按钮时,按钮的onClick不会被调用。

Here is the Base Activity. 这是基本活动。

private void addDrawerItems(){
    mArrayAdapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, getResources().getStringArray(R.array.menu_list));
    mDrawerList.setAdapter(mArrayAdapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}


protected void onCreateDrawer() {
    setContentView(R.layout.activity_base);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarBase);
    setSupportActionBar(toolbar);

    mActivityTitle = getTitle().toString();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_drawer);

    addDrawerItems();
    setUpDrawer();


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

}


private class DrawerItemClickListener implements ListView.OnItemClickListener{

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id){
        selectItem(position);
        Intent intent;
        if (position==getResources().getInteger(R.integer.uploadPos)){
            intent = new Intent(getApplicationContext(), UploadActivity.class);
        }
        else if (position == getResources().getInteger(R.integer.homePos)) {
            intent = new Intent(getApplicationContext(), MainActivity.class);
        }
        else if (position == getResources().getInteger(R.integer.browsePos)){
            intent = new Intent(getApplicationContext(), BrowseActivity.class);
        }
        else if (position == getResources().getInteger(R.integer.myUploadsPos)){
            intent = new Intent(getApplicationContext(), MyUploads.class);
        }
        else if (position == getResources().getInteger(R.integer.logoutPos)){

            // destroying user session in the shared preferences table
            sharedPreferences = getSharedPreferences(getResources().getString(R.string.preferences_table_name), 0);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.remove("UserID");
            editor.commit();

            intent = new Intent(getApplicationContext(), LoginActivity.class);
        }
        else {
            intent = new Intent(getApplicationContext(), LoginActivity.class);
        }
        startActivity(intent);
    }


}

The MainActivity class MainActivity类

public class MainActivity extends BaseActivity{
private String LOG_TAG = "Main Activity";
SharedPreferences sharedPreferences;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    checkForLogin();

    Button button = (Button) findViewById(R.id.test_button);
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Log.d(LOG_TAG, "on click test button");
            Toast toast = Toast.makeText(v.getContext(), "test toast", Toast.LENGTH_SHORT);
            toast.show();

        }
    });
    button.bringToFront();
    super.onCreateDrawer();

}

I can see the button but, on clicking it, the toast doesn't show up when I click it. 我可以看到该按钮,但是单击它时,吐司不显示。 If instead of extending BaseActivity , I extend AppCompatActivity (and remove super.onCreateDrawer ), the toast works. 如果不是扩展BaseActivity ,而是扩展AppCompatActivity (并删除super.onCreateDrawer ),则吐司可以工作。

I tried bringing the button to the front with button.bringToFront() , that didn't work either. 我尝试使用button.bringToFront()将按钮显示在最前面,但也没有用。

The NavigationDrawer somehow is interfering with the clicks on MainActivity and I can't figure it out. NavigationDrawer某种程度上干扰了MainActivity的点击,但我无法弄清楚。

Any suggestions would be appreciated 任何建议,将不胜感激

activity_base.xml activity_base.xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarBase"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>


<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" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/list_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>

<include layout="@layout/content_base" />

content_base.xml content_base.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.nikhil.lecturenotessharing.BaseActivity"
tools:showIn="@layout/activity_base">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="test"
    android:id="@+id/test_button"
    />

Please ask if more information or any XML layouts are needed. 请询问是否需要更多信息或任何XML布局。

Try to remove setContentView(R.layout.activity_base); 尝试删除setContentView(R.layout.activity_base); from onCreateDrawer() , it will works. onCreateDrawer() ,它将起作用。

Because what you've initialized in onCreate() of MainActivity.class will be removed by calling setContentView(R.layout.activity_base); 因为您在MainActivity.class onCreate()中初始化的内容将通过调用setContentView(R.layout.activity_base);删除setContentView(R.layout.activity_base); in onCreateDrawer() of BaseActivity.class . BaseActivity.class onCreateDrawer()中。

Keep in touch @nikhil vavs 保持联系@nikhil vavs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM