NewGuy117
NewGuy117

Reputation: 89

How do I redirect a Fragment to new Fragment?

I'm fairly new to Android programming and I just want to know how could I redirect a Fragment to a new one. Here is my code:

package com.example.samsung.drawer;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class First_frag_o extends Fragment {

final String[] items = new String[]{"Natural Hazards", "Biological Hazards", "Chemical Hazards"
        };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.first0, container, false);

    ListView list = (ListView) view.findViewById(R.id.first123);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {

            Fragment myFragment = null;
            switch (arg2){
                case 0:
                    myFragment = new NaturalHazards();
                    break;
                case 1:
                    myFragment = new BiologicalHazards();
                    break;
                case 2:
                    myFragment = new ChemicalHazards();
                    break;

            }
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container1, myFragment)
                    .commit();
        }
    });
    return view;
}
}

first0.xml

<android.support.v4.widget.DrawerLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"         android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity">
    <FrameLayout android:id="@+id/container1"     android:layout_width="match_parent"
         android:layout_height="match_parent" android:background="#ff7f7f">
<ListView
  android:id="@+id/first123"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"

  xmlns:android="http://schemas.android.com/apk/res/android"
  android:choiceMode="singleChoice"
  android:descendantFocusability="blocksDescendants"    >

</ListView>
    </FrameLayout>
</android.support.v4.widget.DrawerLayout>

BiologicalHazards.Java:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class BiologicalHazards extends Fragment {

 View myView;
 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.haz,container,false);

     return myView;
 }
}

For example, I want to go to the "Biological Hazards" fragment when I click on the list. How do I do it? Could anyone please show me how?

Logcat:

08-30 19:46:57.633  31775-31775/com.example.samsung.drawer E/AndroidRuntime﹕      FATAL EXCEPTION: main
    Process: com.example.samsung.drawer, PID: 31775
    java.lang.IllegalArgumentException: No view found for id 0x7f0d006e (com.example.samsung.drawer:id/haztext) for fragment BiologicalHazards{21ddec38 #2 id=0x7f0d006e}
        at     android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
        at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 4252

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

At first create FrameLayout in your xml

<FrameLayout
        android:id="@+id/child_fragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

Then add this in your Listview Click events

 Your_Listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {

                /*
                *  Added Nested Fragment
                * */

                // Create new fragment and transaction
                Fragment newFragment = new ChildFragment();
                // consider using Java coding conventions (upper first char class names!!!)
                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.child_fragment, newFragment);
                transaction.addToBackStack(null);
                // Commit the transaction
                transaction.commit();


            }

        });

Upvotes: 3

NewGuy117
NewGuy117

Reputation: 89

I've figured it out, I've made a mistake in one of my XML. I've been referring the listview instead of the framelayout. Sorry about that, I'm really new to Android/Java programming. It's working now. Code above is edited to the new, working one.

Upvotes: 0

Related Questions