Onclick在导航抽屉里面不起作用

时间:2017-02-26 17:31:58

标签: android onclick navigation-drawer

我有MainActivity,里面集成了谷歌地图和默认的Android导航抽屉。已经尝试了很多东西,但onclick从未被调用。我尝试过使用butterknife onclick,但它从来没有对我有效。

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,NavigationView.OnNavigationItemSelectedListener {



 @BindView(R.id.traditional) NavigationView traditionalFood;
  private GoogleMap mMap;
  private ArrayAdapter<String> mAdapter;
  private DrawerLayout drawerLayout;
  private Toolbar toolbar;
  private NavigationView naview;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
    setContentView(R.layout.activity_maps);
    setNavigationViewListner();
    SupportMapFragment mapFragment =
        (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    naview = (NavigationView) findViewById(R.id.traditional);

    setSupportActionBar(toolbar);

    initNavigationDrawer();
  }

 @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.
    switch (item.getItemId()) {

      case R.id.traditional: {
       Log.d("onclick","working traditional");
        break;
      }
      case R.id.tips:{
        Log.d("onclick","working tips");
        break;
      }
    }
    //close navigation drawer
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
  }

  private void setNavigationViewListner() {
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(this);

  }

XML文件:

<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

  <fragment 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:id="@+id/map"
      tools:context="localfoodtips.localfoodsarajevo.MapsActivity"
      android:name="com.google.android.gms.maps.SupportMapFragment"/>

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.learn2crack.myapplication.MainActivity">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/Base.AlertDialog.AppCompat.Light" />

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

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

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

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_header"
    app:menu="@menu/menu_navigation"/>

</android.support.v4.widget.DrawerLayout>

3 个答案:

答案 0 :(得分:0)

尝试在Adk清单中添加新活动

答案 1 :(得分:0)

采取以下措施:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);

并将它们放在onCreate()中而不是你正在做的事情。同时使NavigationView navigationView成为字段而不是内部变量

答案 2 :(得分:-1)

设置内容视图后调用Butterknife.bind

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
ButterKnife.bind(this);