我在android studio中关注Slidnerd的材质设计教程,我被困在导航抽屉里。
这是我的NavigationDrawerFragment.java
package com.example.baumann.perfectposture;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* A simple {@link Fragment} subclass.
*/
public class NavigationDrawerFragment extends Fragment {
public static final String PREF_FILE_NAME="testpref";
public static final String KEY_USER_LEARNED_DRAWER="user_learned_drawer";
private ActionBarDrawerToggle mDrawerToggle;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
public NavigationDrawerFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity().KEY_USER_LEARNED_DRAWER,"false"));
if(savedInstanceState==null){
mFromSavedInstanceState=true;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
}
public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_closed) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
public static void saveToPreferences(Context context, String preferenceName, String preferenceValue){
SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString(preferenceName, preferenceValue);
editor.apply();
}
public static String readFromPreferences(Context context, String preferenceName, String defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(preferenceName,defaultValue);
}
}
以下是强调的内容:
"KEY_USER_LEARNED_DRAWER"
in:
mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity().KEY_USER_LEARNED_DRAWER,"false"));
和
"mDrawerLayout"
两次:
mDrawerLayout = drawerLayout;
和
mDrawerLayout.setDrawerListener(mDrawerToggle);
以下是我的三个错误:
Error:(52, 77) error: cannot find symbol variable KEY_USER_LEARNED_DRAWER
Error:(67, 9) error: cannot find symbol variable mDrawerLayout
Error:(79, 9) error: cannot find symbol variable mDrawerLayout
答案 0 :(得分:1)
更改“。”在getActivity()
进入“,”之后
mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity(),KEY_USER_LEARNED_DRAWER,"false"));
声明一个字段private DrawerLayout mDrawerLayout;
,您可以将抽屉分配给。{/ p>