错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(android.view.View)' on a null object reference
at com.cy.mynj.com.cy.mynj.activities.MainActivity.onClick(MainActivity.java:57)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20300)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5682)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//左边栏
private List mList = new ArrayList();
private ListView mListView;
DrawerLayout mDrawerLayout;
//
private ImageView iv_left;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initViews();
}
private void initData() {
mList.add(new ContentModel(R.drawable.weather_icons, "天气", 1));
mList.add(new ContentModel(R.drawable.scenic_spot_icon, "本地美景", 2));
mList.add(new ContentModel(R.drawable.bus_icon, "交通", 3));
mList.add(new ContentModel(R.drawable.security_icon, "社保", 4));
mList.add(new ContentModel(R.drawable.insurance_icon, "医疗", 5));
mList.add(new ContentModel(R.drawable.ciziten_card_icon, "市民卡", 6));
mList.add(new ContentModel(R.drawable.employ_icon, "招聘", 7));
mList.add(new ContentModel(R.drawable.news_icon, "新闻", 8));
}
private void initViews(){
mListView = (ListView) findViewById(R.id.left_listview);
ContentAdapter adapter = new ContentAdapter(this,mList);
mListView.setAdapter(adapter);
iv_left = (ImageView)findViewById(R.id.leftmenu);
iv_left.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.leftmenu:
**mDrawerLayout.openDrawer(Gravity.LEFT);**
break;
default:
break;
}
}
}