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

lqsunshine 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);
}
...全文
5302 6 2 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
  • 打赏
  • 举报
回复
_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。
  • 打赏
  • 举报
回复
相关推荐
发帖
Android
加入

7.9w+

社区成员

移动平台 Android
申请成为版主
帖子事件
创建了帖子
2017-11-18 09:46
社区公告
暂无公告