将方法参数复制到最终的临时变量是不是

时间:2018-04-02 21:03:16

标签: java android variables final

所以我有一个初始化抽屉菜单的方法,它采用Drawer参数来初始化抽屉。现在我尝试在点击抽屉项目之后调用此抽屉参数来关闭它,但它给了我:

Variable 'drawer' is accessed from within inner class, needs to be declare final

所以我尝试手动在参数旁边指定final,它在我初始化新抽屉的行上给出了这个错误:

Cannot assign a value to final variable 'drawer'

所以相反,我让IDE自动修复第一个问题,并建议复制'抽屉'最终临时变量的参数。并且一旦我做了,没有错误显示,但是当我运行应用程序时,它崩溃了,并在我调用drawer.close的行上给了我NPE!

现在如何解决这个问题..!

守则:

public void initDrawerMenu(Drawer drawer) {

    final Drawer finalDrawer = drawer; // the auto fix
    drawer = new DrawerBuilder()
        .withActivity(this)
        .withMultiSelect(false)
        .withOnDrawerItemClickListener(new OnDrawerItemClickListener() {
          @Override
          public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {

            switch (position) {

              case 0:
                finalDrawer.closeDrawer(); // the finalDrawer = null hence causes NPE 
                startActivity(new Intent(getApplicationContext(), HomeActivity.class));
                break;
            }
            return true;
          }
        })
        .build();
  }

2 个答案:

答案 0 :(得分:1)

这可能是因为initDrawerMenu中的抽屉为空或者在方法之外被取消了。

如果要初始化Drawer,则需要更改方法以返回Drawer,而不是将Drawer变量传递给方法。因此,initDrawerMenu不取决于抽屉变量。

从您的代码中,我认为您要做的事情是创建一个抽屉而不是从现有抽屉中初始化它。因此,您最好更改方法名称并使用返回来创建抽屉。像这样:

public Drawer initDrawerMenu() {
  final Drawer drawer = new DrawerBuilder()
        .withActivity(this)
        .withMultiSelect(false)
        .withOnDrawerItemClickListener(new OnDrawerItemClickListener() {
          @Override
          public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
            switch (position) {
              case 0:
                drawer.closeDrawer(); 
                startActivity(new Intent(getApplicationContext(), HomeActivity.class));
                break;
            }
            return true;
          }
        })
        .build();

  return drawer;
}

您应该将方法名称更改为更具可读性,例如:

public Drawer createDrawerMenu() {
  ...
}

当您想要使用它时,您只需使用它来调用它:

Drawer drawer = createDrawerMenu();

答案 1 :(得分:0)

我使用过DrawerLayout和NavigationView。希望这有帮助

    public class MyActivity extends Activity implements OnNavigationItemSelectedListener{

     private DrawerLayout drawer;

     @Override
        public void onCreate(Bundle savedInstanceState) { 
            drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
     }

    @Override
        public boolean onNavigationItemSelected(MenuItem item) {

             //close the drawer
             drawer.closeDrawers();
             switch (item.getItemId()) {
                case R.id.item:
                //do something
                break;
             }
    }
}

P.S - >这里的R.id.item是一个MenuItem

您可以将菜单设置为NavigationView,如下所示

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <android.support.design.widget.NavigationView
            android:id="@+id/navigation"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/my_navigation_items" />

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