如何将片段重定向到新片段?

时间:2015-08-30 10:57:31

标签: android android-fragments

我对Android编程很新,我只想知道如何将Fragment重定向到新的。这是我的代码:

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;
 }
}

例如,我想去生物危害&#34;生物危害&#34;当我点击列表时片段。我该怎么做?有谁能告诉我怎么样?

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)

2 个答案:

答案 0 :(得分:3)

首先在FrameLayout

中创建xml
<FrameLayout
        android:id="@+id/child_fragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

然后在 Listview点击事件

中添加此内容
 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();


            }

        });

答案 1 :(得分:0)

我已经弄清楚了,我在我的一个XML中犯了一个错误。我一直在引用listview而不是framelayout。对不起,我是Android / Java编程的新手。它现在正在运作。上面的代码被编辑为新的,有效的。