android–Button在Fragment中单击以打开新片段

android–Button在Fragment中单击以打开新片段,第1张

概述我不知道如何在按钮点击上打开一个新片段,我只知道如何使用intent打开新的Activity但是当我尝试打开新片段时我的项目应用程序崩溃希望有人可以帮助我解决我的问题这就是我所拥有的:importandroid.app.Fragment;importandroid.os.Bundle;importandroid.app.FragmentManager;

我不知道如何在按钮点击上打开一个新片段,我只知道如何使用intent打开新的Activity但是当我尝试打开新片段时我的项目应用程序崩溃希望有人可以帮助我解决我的问题

这就是我所拥有的:

import androID.app.Fragment;import androID.os.Bundle;import androID.app.FragmentManager;import androID.app.FragmentTransaction;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.button;import androID.Widget.Spinner;public class FragmentTwo extends Fragment {public FragmentTwo() {}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,        Bundle savedInstanceState) {    VIEw vIEw = inflater.inflate(R.layout.fragment_layout_two, container,            false);    // SPINNER1    Spinner spinner1 = (Spinner) vIEw.findVIEwByID(R.ID.spinnerSpecialty);    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromresource(            getActivity(), R.array.specialty_arrays,            androID.R.layout.simple_spinner_item);    adapter.setDropDownVIEwResource(androID.R.layout.simple_spinner_dropdown_item);    spinner1.setAdapter(adapter);    // SPINNER2    Spinner spinner2 = (Spinner) vIEw.findVIEwByID(R.ID.spinnerLocation);    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromresource(            getActivity(), R.array.city_array,            androID.R.layout.simple_spinner_item);    adapter2.setDropDownVIEwResource(androID.R.layout.simple_spinner_dropdown_item);    spinner2.setAdapter(adapter);    // button    button btnSearch = (button) vIEw.findVIEwByID(R.ID.btnSearch);    btnSearch.setonClickListener(new VIEw.OnClickListener() {         @OverrIDe            public voID onClick(VIEw v) {                switch (v.getID()) {                case R.ID.btnSearch:                    //what to put here                   FragmentManager fm = getFragmentManager();                   FragmentTransaction ft = fm.beginTransaction();                   ft.replace(R.ID.content_frame, new FragmentThree());                    ft.commit();                 break;                }            }       });       return vIEw;}

}

logcat的:

04-01 03:45:25.508: E/AndroIDRuntime(1618): FATAL EXCEPTION: main04-01 03:45:25.508: E/AndroIDRuntime(1618): java.lang.NullPointerException04-01 03:45:25.508: E/AndroIDRuntime(1618):     at          com.droID.FragmentThree.onCreateVIEw(FragmentThree.java:30)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at  androID.app.Fragment.performCreateVIEw(Fragment.java:1695)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at androID.app.FragmentManagerImpl.movetoState(FragmentManager.java:885)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at   androID.app.FragmentManagerImpl.movetoState(FragmentManager.java:1057)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at androID.app.BackStackRecord.run(BackStackRecord.java:682)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at androID.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at androID.app.FragmentManagerImpl.run(FragmentManager.java:441)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at androID.os.Handler.handleCallback(Handler.java:730)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at androID.os.Handler.dispatchMessage(Handler.java:92)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at androID.os.Looper.loop(Looper.java:137)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at androID.app.ActivityThread.main(ActivityThread.java:5103)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at java.lang.reflect.Method.invokeNative(Native Method)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at java.lang.reflect.Method.invoke(Method.java:525)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:553)04-01 03:45:25.508: E/AndroIDRuntime(1618):     at dalvik.system.NativeStart.main(Native Method)

解决方法:

试试这个:

从下面代码更改ID show_fragment以匹配您的布局XML ID ..

 @OverrIDe            public voID onClick(VIEw v) {                switch (v.getID()) {                case R.ID.btnSearch:                    //what to put here                   FragmentManager fm = getFragmentManager();                   FragmentTransaction ft = fm.beginTransaction();                   ft.replace(R.ID.show_fragment, new TestFragment(), "fragment_screen");                    ft.commit();                 break;                }            }   <FrameLayout        androID:ID="@+ID/show_fragment"        androID:layout_wIDth="match_parent"        androID:layout_height="0dp">    </FrameLayout>
总结

以上是内存溢出为你收集整理的android – Button在Fragment中单击以打开新片段全部内容,希望文章能够帮你解决android – Button在Fragment中单击以打开新片段所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1113271.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存