2017-04-23 50 views
0

我有一个总是发生的错误:由:java.lang.NullPointerException引起。 这是我的源代码java.lang.NullPointerException

FrameLayout frameLayout1 = (FrameLayout) findViewById(R.id.frameContainer1); 
     LayoutInflater layoutInflater1 = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view1 = layoutInflater1.inflate(R.layout.activity_add_hm, null, false); 
     frameLayout1.addView(view1); 


     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawerListView = (ListView) findViewById(R.id.drawer_listview); 

     ViewGroup header = (ViewGroup) layoutInflater1.inflate(R.layout.nav_header_main, drawerListView, false); 
     drawerListView.addHeaderView(header, null, false); 

     menuButton1 = (ImageButton) findViewById(R.id.menuButton1); 
     menuButton1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       drawerLayout.openDrawer(GravityCompat.START); 

      } 
     }); 

有人可以帮我吗? 在此先感谢。

+1

NullPointerException异常是当您使用空引用,请阅读并公布完整的logcat建立一些东西,在那里你可以看到什么可能产生NullPointerException异常 –

+0

@peterparker你可以看到异常消息是什么代码语句引起的。仔细阅读例外的堆栈跟踪。 'NullPointerException'意味着你正试图调用一个方法或从不引用对象的引用中获取状态。它接受一个空值。它在调用方法或获取状态之前未被启动,或者已被设置为空。 –

回答

0

空指针异常的你的情况可能的原因是从

任何findViewById()方法。

原因是您的视图ID可能不会出现在活动或片段布局或您膨胀的布局中。

可能调用此方法

drawerLayout = (DrawerLayout) view1.findViewById(R.id.drawer_layout); 
drawerListView = (ListView) voew1.findViewById(R.id.drawer_listview); 

ViewGroup header = (ViewGroup) layoutInflater1.inflate(R.layout.nav_header_main, drawerListView, false); 
drawerListView.addHeaderView(header, null, false); 
menuButton1 = (ImageButton) header.findViewById(R.id.menuButton1); 
+0

非常感谢! –