ChellyZ
2017-04-15 02:16
采纳率: 100%
浏览 2.5k

Fragment内按钮使用Intent跳转Activity时强退

 public class Fragment_searchall extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_searchall, container, false);

        Button b =(Button)getActivity().findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(getActivity(), Activity_settings.class);
                startActivity(intent);
            }
        });
        return view;


    }

}

Fragment布局文件就一个按钮,Activity已经在AndroidMainfest注册

这是控制台报错信息

 04-15 02:12:38.158 9946-9946/com.level1.yukinoshita.swan0 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.level1.yukinoshita.swan0, PID: 9946
                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                at com.level1.yukinoshita.fragment.Fragment_searchall.onCreateView(Fragment_searchall.java:27)
                                                                                at android.app.Fragment.performCreateView(Fragment.java:2220)
                                                                                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
                                                                                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                                at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                                at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
                                                                                at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-15 02:12:42.958 9946-9946/com.level1.yukinoshita.swan0 I/Process: Sending signal. PID: 9946 SIG: 9
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • YXTS122 2017-04-15 03:00
    已采纳

    好像不是getActivity().findViewById吧,应该是view.findViewById写样吧

    已采纳该答案
    打赏 评论
  • 月盡天明 2017-04-19 02:13

    Button对象是空,,,

    
    view.findViewById
    
    
    打赏 评论
  • 大宝风幻 2017-05-16 10:54

    这个你要用View.findview
    不能用getactivity

    打赏 评论
  • 大宝风幻 2017-05-16 10:54

    getactivity 是获取主页面的 你现在在Fragment 页面里 当然找不到啦

    打赏 评论

相关推荐 更多相似问题