come andare da un frammento all’altro da un clic del pulsante in android?

come andare da un frammento all'altro da un clic del pulsante in android?Sto lavorando su di transazione tra i frammenti in android, sto avendo un frammento contiene alcuni relativelayout, su ogni formato devo aprire un nuovo frammento, Quindi devo vedere tanti collegamenti e provato, come di seguito, Ma mi dà errore di tutti i tempi, Quindi, per Favore mi suggeriscono dove sbagliavo nel mio codice, si Prega di salvare me, grazie in anticipo.

Errore

09-04 12:21:47.764: E/AndroidRuntime(17756): java.lang.IllegalArgumentException: No view found for id 0x7f0500b3 (com.eps.fortalented:id/show_fragment) for fragment DetailDashBoardFragment{42b0b1e0 #1 id=0x7f0500b3}

codice

onClick(){
                                    //second fragment
            DetailDashBoardFragment fragment = new DetailDashBoardFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.show_fragment, fragment).addToBackStack(null);
            ft.commit();

}

DetailDashBoardFragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/show_fragment"
    android:layout_width="match_parent"
    android:layout_height="0dp" >




</FrameLayout>

deatailDescFragment.java

public class DetailDashBoardFragment extends Fragment implements
        OnClickListener {
    View v;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.detail_dashboard_fragmnet, container,
                false);
        return v;
    }

    @Override
    public void onClick(View v) {
        //TODO Auto-generated method stub

    }

}

Attività

public class MyMainActivity extends FragmentActivity implements
        OnTabChangeListener {
    Menu menu;
    /** Called when the activity is first created. */
    public FragmentTabHost tabHost;

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.tab_bar);
        menu = (Menu) findViewById(R.id.menu_tab);
        menu.arrow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //TODO Auto-generated method stub
                 finish();
                    overridePendingTransition(R.anim.bottom_in, R.anim.top_out);

            }
        });
        tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        tabHost.setOnTabChangedListener(this);
        tabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);

        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(""),
                DashBoardFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(""),
                DashBoardFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator(""),
                DashBoardFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator(""),
                DashBoardFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab5").setIndicator(""),
                DashBoardFragment.class, null);

        tabHost.getTabWidget()
                .getChildAt(1)
                .setBackground(
                        getResources().getDrawable(R.drawable.friend_request));
        tabHost.getTabWidget().getChildAt(2)
                .setBackground(getResources().getDrawable(R.drawable.masseges));
        tabHost.getTabWidget()
                .getChildAt(3)
                .setBackground(
                        getResources().getDrawable(R.drawable.notification_btn));

        tabHost.getTabWidget()
                .getChildAt(4)
                .setBackground(
                        getResources().getDrawable(R.drawable.share_icon));

        //Set Tab1 as Default tab and change image
        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget()
                .getChildAt(0)
                .setBackground(
                        getResources().getDrawable(
                                R.drawable.dashboard_hover_btn));
    }

    @Override
    public void onTabChanged(String tabId) {

        /************ Called when tab changed *************/

        //********* Check current selected tab and change according images
        //*******/

        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            if (i == 0)
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.dashboard_icon);
            else if (i == 1)
                tabHost.getTabWidget()
                        .getChildAt(i)
                        .setBackground(
                                getResources().getDrawable(
                                        R.drawable.friend_request));
            else if (i == 2)
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.masseges);
            else if (i == 3)
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.notification_btn);
            else if (i == 4)
                tabHost.getTabWidget().getChildAt(i)
                        .setBackgroundResource(R.drawable.share_icon);
        }

        Log.i("tabs", "CurrentTab: " + tabHost.getCurrentTab());

        if (tabHost.getCurrentTab() == 0)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                    .setBackgroundResource(R.drawable.dashboard_hover_btn);
        else if (tabHost.getCurrentTab() == 1)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                    .setBackgroundResource(R.drawable.friend_request_hover);
        else if (tabHost.getCurrentTab() == 2)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                    .setBackgroundResource(R.drawable.message_hover);
        else if (tabHost.getCurrentTab() == 3)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                    .setBackgroundResource(R.drawable.notification_btn_hover);
        else if (tabHost.getCurrentTab() == 4)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                    .setBackgroundResource(R.drawable.share_icon_hover);

    }

    @SuppressLint("InflateParams")
    private View prepareTabView(Drawable resId) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator,
                null);
        ImageView iv = (ImageView) view.findViewById(R.id.iv_photo);
        iv.setBackground(resId);
        return view;
    }

}

logcat

09-04 14:33:53.656: E/AndroidRuntime(20715): FATAL EXCEPTION: main
09-04 14:33:53.656: E/AndroidRuntime(20715): Process: com.eps.fortalented, PID: 20715
09-04 14:33:53.656: E/AndroidRuntime(20715): java.lang.IllegalArgumentException: No view found for id 0x7f0500b3 (com.eps.fortalented:id/show_fragment) for fragment DetailDashBoardFragment{42b03730 #1 id=0x7f0500b3}
09-04 14:33:53.656: E/AndroidRuntime(20715):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at android.os.Handler.handleCallback(Handler.java:733)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at android.os.Looper.loop(Looper.java:146)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at android.app.ActivityThread.main(ActivityThread.java:5653)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at java.lang.reflect.Method.invoke(Method.java:515)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
09-04 14:33:53.656: E/AndroidRuntime(20715):    at dalvik.system.NativeStart.main(Native Method)

tabbar.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/home_page_bg" >

    <com.eps.fortalented.uc.Header
        android:id="@+id/hdr_tab"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.app.FragmentTabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/hdr_tab" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:background="#1F64A4" />

            <TabWidget
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />

            <FrameLayout
                android:id="@+id/tabFrameLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

    <com.eps.fortalented.uc.Menu
        android:id="@+id/menu_tab"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
    </com.eps.fortalented.uc.Menu>

</RelativeLayout>
  • posta la tua attività clas
  • ok…attendere Prego,sto inviando il codice
  • vedere la mia modifica
  • come stai a partire frammento tua attività?
  • ho usato fragmentTabHost,quindi nel mio primo frammento sto avendo 5 layout(pulsante).Sul suo fare clic voglio andare in un altro frammento.
InformationsquelleAutor user3820044 | 2014-09-04



2 Replies
  1. 1

    Provare questo:

    Esempio:

    DetailDashBoardFragment fragment = new DetailDashBoardFragment();
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        ft.replace(R.id.show_fragment, fragment).addToBackStack(null);
        ft.commit();

    Edit:

    android.app.Fragment fr;
                fr = new DetailDashBoardFragment();
                FragmentManager fm = getFragmentManager();
                FragmentTransaction fragmentTransaction = fm.beginTransaction();
                fragmentTransaction.replace(R.id.show_fragment, fr);
                fragmentTransaction.commit();

    Speranza che aiuta.

    • dà lo stesso errore…si Prega di vedere il mio logcat traccia
    • Dove l’hai fatto questo layout? DetailDashBoardFragment.xml e anche possibile inviare il tuo tab_bar.xml
    • Si prega di vedere la modifica
    • attendere che ho messo alcune schermate in modo che si può suggerire di me e di ottenere il mio problema..si prega di attendere
    • Penso che questo è perché non avete R. id.show_fragment sul tuo tab_bar.xml. Provare questo fragmentTransaction.sostituire(R. id.tabFrameLayout, fr);
    • Si prega di vedere la schermata
    • Hai provato quello che ho suggerito?..R. id.show_fragment deve essere un figlio del layout specificato nella chiamata a setcontentview() Nel tuo caso tab_bar.xml dovrebbe avere un oggetto con id show_fragment.
    • Cerchiamo di continuare questa discussione in chat.

  2. 0

    Si prega di provare questo codice:

      Fragment fragment = new MoveSearchStudentFragment();
      FragmentManager fm = getActivity().getSupportFragmentManager();
      FragmentTransaction ft = fm.beginTransaction();
      ft.replace(R.id.content_frame, fragment);
      ft.commit();

Lascia un commento