无法调用onDrawerOpened和onDrawerClosed

时间:2015-05-01 06:32:06

标签: android eclipse

所以,我试图创建我的应用程序的导航抽屉,我不知道为什么我无法呼叫公共空虚onDrawerOpened,它只是显示错误说我手动输入时void is an invalid type for the variable onDrawerOpened

所以我的NavigationDrawerFragment.java代码

package id.WKKR.ktcafe;

import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class NavigationDrawerFragment extends Fragment {

    public DrawerLayout mDrawerLayout;
    public ActionBarDrawerToggle mDrawerToggle;

    public NavigationDrawerFragment() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_navigation_drawer, container,
                false);

    }

    public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {
        // TODO Auto-generated method stub
        mDrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout,
                toolbar, R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
        {
            public void onDrawerOpened(View drawerView){
                super.onDrawerOpened(drawerView);
            }

            public void onDrawerClosed(View drawerView){
                super.onDrawerClosed(drawerView);
            }

        };
    }
}

感谢任何帮助过的人。

(编辑)的

所以这里是我的mainactivity.java(名为MenuUtama.java)



► ► package id.WKKR.ktcafe;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;

@SuppressWarnings("deprecation")
public class MenuUtama extends ActionBarActivity{

	Toolbar toolbar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu_utama);
		
		toolbar = (Toolbar) findViewById(R.id.app_bar);
		setSupportActionBar(toolbar);
		
		NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
		drawerFragment.setUp((DrawerLayout)findViewById(R.id.drawer_layout),toolbar);


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.menu_utama, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Toast.makeText(this, "This Is " + item.getTitle() + " Button", Toast.LENGTH_SHORT).show();
			return true;
		}
		
		if (id == R.id.bill) {
			startActivity(new Intent(this, BillTagihanAnda.class));
		}
		return super.onOptionsItemSelected(item);
	}
}




4 个答案:

答案 0 :(得分:4)

您需要覆盖onDrawerOpened和onDrawerClosed方法。添加@override注释或实现,如下所示:

mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerlayout, toolBar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);

                        };

将活动中的设置方法更改为:

NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setup(R.id.fragment_navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
片段中的

设置方法包括我在其中一条评论中所说的id片段。

答案 1 :(得分:0)

谢谢Keshav,感谢您之前的帮助,现在我可以弄清楚为什么我之前无法使用onDrawerOpened,有点傻的错误,我不小心将括号放在括号后面而不知道。

public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
        // TODO Auto-generated method stub
        mDrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout,
                toolbar, R.string.drawer_open, R.string.drawer_close);
                            //here is my problem, i put the semi-colon after parentheses
                     {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

答案 2 :(得分:0)

public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {
    // TODO Auto-generated method stub
    mDrawerLayout = drawerLayout;
    mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout,
            toolbar, R.string.navigation_drawer_open,
    // you just need to remove the semicolon here
            R.string.navigation_drawer_close) {
        public void onDrawerOpened(View drawerView){
            super.onDrawerOpened(drawerView);
        }

        public void onDrawerClosed(View drawerView){
            super.onDrawerClosed(drawerView);
        }

    };
}

答案 3 :(得分:0)

我遇到了这个问题,我的解决方法是:

1-创建以下项目的实例:DrawerListener

DrawerLayout.DrawerListener acc = new DrawerLayout.DrawerListener() {
    @Override
    public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
        // Action to onDrawerSlider
    }

    @Override
    public void onDrawerOpened(@NonNull View drawerView) {
        // Action to onDrawerOpened
    }

    @Override
    public void onDrawerClosed(@NonNull View drawerView) {
        // Action to onDrawerClosed
    }

    @Override
    public void onDrawerStateChanged(int newState) {
        // Action to onDrawerStateChanged
    }
};

2-将侦听器设置为DrawerLayout对象:

mDrawerLayout.addDrawerListener(acc);