Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button Click inside Fragment to open up new fragment

I don't know how to open up a new fragment on button click, I only know how to open new Activity using intent but when I try to open up new fragment my project app crashes hope someone can help me with my problem

this is what I have:

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

}

@Override
public 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: main
04-01 03:45:25.508: E/AndroidRuntime(1618): java.lang.NullPointerException
04-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$1.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)
like image 974
Ken Hollis Avatar asked Dec 13 '25 13:12

Ken Hollis


2 Answers

Try this:

From below code change id show_fragment to match with your layout 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>
like image 60
Vamshi Avatar answered Dec 16 '25 11:12

Vamshi


I hope this helps you

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
like image 23
Ranjith Avatar answered Dec 16 '25 10:12

Ranjith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!