user3820044
user3820044

Reputation: 177

how to go from one fragment to another from a button click in android?

my problemI am working on transaction between fragments in android, I am having a fragment contains some relativelayout, on each layout i have to open a new fragment, So I have refer so many links and tried as below, But it gives me error all time, So Please suggest me where the wrong in my code, Please save me thank you in advance.

Error

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}

code

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

    }

}

Activity

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>

Upvotes: 2

Views: 12488

Answers (2)

Vishal Vaishnav
Vishal Vaishnav

Reputation: 3422

Please try this code:

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

Upvotes: 0

yhel
yhel

Reputation: 92

Try this:

Sample:

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();

Hope it helps.

Upvotes: 1

Related Questions