ChellyZ 2017-04-15 02:16 采纳率: 100%
浏览 2624
已采纳

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写样吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写
  • ¥15 graphpad prism 三因素重复测定报错