在 ActionBarDrawerToggle 对象上调用 syncstate() 时出现空指针异常
nullpointerexception on calling syncstate() on ActionBarDrawerToggle object
我在调用 ActionBarDrawerToggle 对象上的 syncstate() 函数时遇到空指针异常。
Navigation_logined.java
public class Navigation_logined extends Fragment {
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mDrawerToggle;
public Navigation_logined(){
}
public void setUp(DrawerLayout drawerLayout,Toolbar toolbar)
{
mDrawerLayout=drawerLayout;
mDrawerToggle=new ActionBarDrawerToggle(getActivity(),mDrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
//nullpointerexception at this point
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
//rest of the class
}
logined.java
public class logined extends ActionBarActivity {
protected Toolbar mToolBar;
String LOGIN_URL="";
static public Context mContext;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceIdentifier());
mToolBar=(Toolbar)findViewById(R.id.screen_default_toolbar);
setSupportActionBar(mToolBar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.screen_default_container, HomeFragment.newInstance(),"fragment_home").commit();
}
Navigation_logined navigation_logined=new Navigation_logined();
DrawerLayout d=(DrawerLayout)findViewById(R.id.drawer_layout);
navigation_logined.setUp(d,mToolBar);
}
}
logcat 显示了 ActionBarDrawerToggle 的 DrawerArrowDrawableToggle 子类的 isLayoutRtl() 函数的异常。
Logcat
04-16 00:16:20.346 18198-18198/com.xyz.abc.pqr E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v7.app.ActionBarDrawerToggle$DrawerArrowDrawableToggle.isLayoutRtl(ActionBarDrawerToggle.java:484)
at android.support.v7.app.DrawerArrowDrawable.draw(DrawerArrowDrawable.java:99)
at android.widget.ImageView.onDraw(ImageView.java:1002)
at android.view.View.draw(View.java:13614)
at android.view.View.getDisplayList(View.java:12565)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.draw(View.java:13617)
at android.view.View.getDisplayList(View.java:12565)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.draw(View.java:13617)
at android.view.View.getDisplayList(View.java:12565)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.draw(View.java:13617)
at android.widget.FrameLayout.draw(FrameLayout.java:467)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2458)
at android.view.View.getDisplayList(View.java:12565)
at android.view.View.getDisplayList(View.java:12609)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1187)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2313)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2185)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1993)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4674)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.Zygot
您在设置函数中缺少一些代码
layers = getResources().getStringArray(R.array.plans_array);
drawerList = (ListView) findViewById(R.id.left_drawer);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, android.R.id.text1,layers));
其中 text1 是 drawer_list_item 布局中的文本视图
plans_array 是列表项的字符串数组
我在调用 ActionBarDrawerToggle 对象上的 syncstate() 函数时遇到空指针异常。
Navigation_logined.java
public class Navigation_logined extends Fragment {
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mDrawerToggle;
public Navigation_logined(){
}
public void setUp(DrawerLayout drawerLayout,Toolbar toolbar)
{
mDrawerLayout=drawerLayout;
mDrawerToggle=new ActionBarDrawerToggle(getActivity(),mDrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
//nullpointerexception at this point
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
//rest of the class
}
logined.java
public class logined extends ActionBarActivity {
protected Toolbar mToolBar;
String LOGIN_URL="";
static public Context mContext;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceIdentifier());
mToolBar=(Toolbar)findViewById(R.id.screen_default_toolbar);
setSupportActionBar(mToolBar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.screen_default_container, HomeFragment.newInstance(),"fragment_home").commit();
}
Navigation_logined navigation_logined=new Navigation_logined();
DrawerLayout d=(DrawerLayout)findViewById(R.id.drawer_layout);
navigation_logined.setUp(d,mToolBar);
}
}
logcat 显示了 ActionBarDrawerToggle 的 DrawerArrowDrawableToggle 子类的 isLayoutRtl() 函数的异常。
Logcat
04-16 00:16:20.346 18198-18198/com.xyz.abc.pqr E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v7.app.ActionBarDrawerToggle$DrawerArrowDrawableToggle.isLayoutRtl(ActionBarDrawerToggle.java:484)
at android.support.v7.app.DrawerArrowDrawable.draw(DrawerArrowDrawable.java:99)
at android.widget.ImageView.onDraw(ImageView.java:1002)
at android.view.View.draw(View.java:13614)
at android.view.View.getDisplayList(View.java:12565)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.draw(View.java:13617)
at android.view.View.getDisplayList(View.java:12565)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.draw(View.java:13617)
at android.view.View.getDisplayList(View.java:12565)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.getDisplayList(View.java:12563)
at android.view.View.getDisplayList(View.java:12609)
at android.view.View.draw(View.java:13338)
at android.view.ViewGroup.drawChild(ViewGroup.java:2990)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2860)
at android.view.View.draw(View.java:13617)
at android.widget.FrameLayout.draw(FrameLayout.java:467)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2458)
at android.view.View.getDisplayList(View.java:12565)
at android.view.View.getDisplayList(View.java:12609)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1187)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2313)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2185)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1993)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4674)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.Zygot
您在设置函数中缺少一些代码
layers = getResources().getStringArray(R.array.plans_array);
drawerList = (ListView) findViewById(R.id.left_drawer);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, android.R.id.text1,layers));
其中 text1 是 drawer_list_item 布局中的文本视图 plans_array 是列表项的字符串数组