java.lang.RuntimeException异常,请大神赐教!!!

nyle_bkpp 2017-11-18 09:46:09
11-18 09:39:51.281 19604-19604/com.example.admin.emotion E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.emotion, PID: 19604
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.emotion/com.example.admin.emotion.MainFragment}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6524)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.admin.emotion.MainFragment.initView(MainFragment.java:80)
at com.example.admin.emotion.MainFragment.onCreate(MainFragment.java:59)
at android.app.Activity.performCreate(Activity.java:6910)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:156) 
at android.app.ActivityThread.main(ActivityThread.java:6524) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831) 
出问题的代码:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
fragmentManager = getSupportFragmentManager();
initView();
setChioceItem(0);
}

private void initView() {
firstImage = (ImageView) findViewById(R.id.first_image);
secondImage = (ImageView) findViewById(R.id.second_image);
thirdImage = (ImageView) findViewById(R.id.third_image);
fourthImage = (ImageView) findViewById(R.id.fourth_image);
firstText = (TextView) findViewById(R.id.first_text);
secondText = (TextView) findViewById(R.id.second_text);
thirdText = (TextView) findViewById(R.id.third_text);
fourthText = (TextView) findViewById(R.id.fourth_text);
firstLayout = (RelativeLayout) findViewById(R.id.first_layout);
secondLayout = (RelativeLayout) findViewById(R.id.second_layout);
thirdLayout = (RelativeLayout) findViewById(R.id.third_layout);
fourthLayout = (RelativeLayout) findViewById(R.id.fourth_layout);

firstLayout.setOnClickListener(MainFragment.this);
secondLayout.setOnClickListener(MainFragment.this);
thirdLayout.setOnClickListener(MainFragment.this);
fourthLayout.setOnClickListener(MainFragment.this);
}
...全文
7600 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
_WJerry 2018-12-13
  • 打赏
  • 举报
回复
你的initView的方法写的不对,你看一下Fragment的生命周期,你要使用View应该在onViewCreated之后,onCreate的时候界面还没渲染到window中。
雨焰 2017-11-20
  • 打赏
  • 举报
回复
NullPointerException 你是不是设置点击的那个控件没有赋予findviewbyid啊?
wanql0203 2017-11-20
  • 打赏
  • 举报
回复
1.看代码你都已经findViewById 2.既然能安装说明编译正确 由此可见你要找的id不在content_main这个布局文件中,而在其他的布局文件中 可以到MainFragment.java第80行看是哪个对象,然后到布局文件中搜索一下看是否存在
ghostgoodbye 2017-11-20
  • 打赏
  • 举报
回复
com.example.admin.emotion.MainFragment.initView(MainFragment.java:80) 80行调用setOnClickListener的对象为空,
MadFrogs 2017-11-20
  • 打赏
  • 举报
回复
setOnClickListener的引用view为空,使用时增加判断控件是否为空,或者排查你的流程找到真正的rootcause。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧