weixin_39125267 2018-02-05 09:55 采纳率: 0%
浏览 1417
已结题

DrawerLayout使用问题 哪里有问题 点击图片后停止运行并报错

错误
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;
    }
}

}

  • 写回答

8条回答 默认 最新

  • 白萝卜。 2018-02-05 10:11
    关注

    *mDrawerLayout.openDrawer(Gravity.LEFT);

    这个东西没有初始化

    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境