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个回答

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

github_38319507
ChellyZ 666 果然是这样 十分感谢
3 年多之前 回复

Button对象是空,,,


view.findViewById

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

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问