如何替换android中现有片段的片段

时间:2013-06-26 16:28:20

标签: android android-fragments android-fragmentactivity

我试图放置片段来替换布局中已经存在的片段

这是我的mainactivity.xml布局:

  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 
android:baselineAligned="false">

<LinearLayout
    android:id="@+id/listviewFragment"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"

  ></LinearLayout>

<LinearLayout
    android:id="@+id/detailFragment"
    android:layout_width="0dp"
    android:layout_weight="2"
    android:layout_height="match_parent"

   ></LinearLayout>
</LinearLayout>

这是我的主要活动课程:

 public class MainActivity extends FragmentActivity implements 

  FragmentListViewActivity.OnItemSelectedListener {

/* (non-Javadoc)
 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.activity_main);

    FragmentTransaction transaction =
     getSupportFragmentManager().beginTransaction();
FragmentListViewActivity toadd = new FragmentListViewActivity();
    transaction.add(R.id.listviewFragment, toadd);
    transaction.addToBackStack(null);
    transaction.commit();

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    DetailFragmentActivity detail = new DetailFragmentActivity();
    ft.add(R.id.detailFragment, detail);
    ft.addToBackStack(null);
    ft.commit();

}

@Override
public void onItemSelected(String link) {
    // TODO Auto-generated method stub
    if(link != null && link.equalsIgnoreCase("Monday")){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    MondayActivity detail = new MondayActivity();
    ft.replace(R.id.detailFragment, detail);
    ft.addToBackStack(null);
    ft.commit();
    }
      if(link != null && link.equalsIgnoreCase("Tuesday")){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    TuesdayActivity detail = new TuesdayActivity();
    ft.replace(R.id.detailFragment, detail);
    ft.addToBackStack(null);
    ft.commit();
    }
       if(link != null && link.equalsIgnoreCase("Wednesday")){
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    WednesdayActivity detail = new WednesdayActivity();
    ft.replace(R.id.detailFragment, detail);
    ft.addToBackStack(null);
    ft.commit();
    }

}   
}

这是我的FragmentlistViewActivity

  public class FragmentListViewActivity extends Fragment {

private OnItemSelectedListener listener;

  ListView listview;

String[] list = new String[] {"Monday","Tuesday","Wednesday"};

/* (non-Javadoc)
 * @see android.app.ListFragment#onCreateView(android.view.LayoutInflater,  
     android.view.ViewGroup, android.os.Bundle)
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
       View view =  
             inflater.inflate(R.layout.layout_fragment_listviewitems_screen,
                container, false);


        listview = (ListView)  
            view.findViewById(R.id.layout_frgment_listitems_listview);


        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
           (getActivity(), android.R.layout.simple_list_item_1,list);

        listview.setAdapter(arrayAdapter);
        listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> av, View v, int position,long itemId) {
    // TODO Auto-generated method stub
    String link = (String) listview.getItemAtPosition(position);
                 listener.onItemSelected(link);

            }
        });
        return view;
}

 public interface OnItemSelectedListener {
      public void onItemSelected(String link);
    }

  @Override
    public void onAttach(Activity activity) {
      super.onAttach(activity);
      if (activity instanceof OnItemSelectedListener) {
        listener = (OnItemSelectedListener) activity;
      } else {
        throw new ClassCastException(activity.toString()
            + " must implemenet FragmentListViewActivity.OnItemSelectedListener");
      }
    }


 }

这是我的DetailFragmentActivity:

   public class DetailFragmentActivity extends Fragment   {

/* (non-Javadoc)
 * @see android.app.Fragment#onCreateView(android.view.LayoutInflater,  
    android.view.ViewGroup, android.os.Bundle)
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
     View view = inflater.inflate(R.layout.layout_fragment_items_screen,
                container, false);
            return view;
}


 }

logcat的:

06-27 00:35:26.024: W/dalvikvm(25210): threadid=1: thread exiting with uncaught   
exception (group=0x40a551f8)
06-27 00:35:26.064: E/AndroidRuntime(25210): FATAL EXCEPTION: main
06-27 00:35:26.064: E/AndroidRuntime(25210): android.view.InflateException: Binary XML 
file line #2: Error inflating class fragment
 06-27 00:35:26.064: E/AndroidRuntime(25210):   at     
 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
 06-27 00:35:26.064: E/AndroidRuntime(25210):   at  
 android.view.LayoutInflater.inflate(LayoutInflater.java:466)
 06-27 00:35:26.064: E/AndroidRuntime(25210):   at     
 android.view.LayoutInflater.inflate(LayoutInflater.java:396)
 06-27 00:35:26.064: E/AndroidRuntime(25210):   at 
 com.farmer.financial.estimation.MondayActivity.onCreateView(MondayActivity.java:26)
 06-27 00:35:26.064: E/AndroidRuntime(25210):   at 
 android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at    
 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
 06-27 00:35:26.064: E/AndroidRuntime(25210):   at 
 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
 06-27 00:35:26.064: E/AndroidRuntime(25210):   at  
 android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
 06-27 00:35:26.064: E/AndroidRuntime(25210):   at 
 android.support.v4.app.FragmentManagerImpl.execPendingActions   
 (FragmentManager.java:1444)
 06-27 00:35:26.064: E/AndroidRuntime(25210):   at 
  android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at 
  android.os.Handler.handleCallback(Handler.java:605)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at 
  android.os.Handler.dispatchMessage(Handler.java:92)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at 
   android.os.Looper.loop(Looper.java:137)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at 
   android.app.ActivityThread.main(ActivityThread.java:4424)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at 
  java.lang.reflect.Method.invokeNative(Native Method)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at   
  java.lang.reflect.Method.invoke(Method.java:511)
   06-27 00:35:26.064: E/AndroidRuntime(25210):     at 
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   06-27 00:35:26.064: E/AndroidRuntime(25210):     at  
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   06-27 00:35:26.064: E/AndroidRuntime(25210):     at  
   dalvik.system.NativeStart.main(Native Method)
  06-27 00:35:26.064: E/AndroidRuntime(25210): Caused by:  
  java.lang.NullPointerException: name == null
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at 
  java.lang.VMClassLoader.findLoadedClass(Native Method)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at  
  java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at 
   java.lang.ClassLoader.loadClass(ClassLoader.java:491)
  06-27 00:35:26.064: E/AndroidRuntime(25210):  at  1
   java.lang.ClassLoader.loadClass(ClassLoader.java:461)
   06-27 00:35:26.064: E/AndroidRuntime(25210):     at 
   android.support.v4.app.Fragment.instantiate(Fragment.java:391)
   06-27 00:35:26.064: E/AndroidRuntime(25210):     at 
   android.support.v4.app.Fragment.instantiate(Fragment.java:369)
   06-27 00:35:26.064: E/AndroidRuntime(25210):     at  
   android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
    06-27 00:35:26.064: E/AndroidRuntime(25210):    at 
   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
   06-27 00:35:26.064: E/AndroidRuntime(25210):     ... 18 more

我尝试了所有可能性,但我错了一些在哪里请给我一些解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:0)

这里不完全确定,但是,如果您使用的是v4支持片段,我建议您在活动中以这种方式实例化片段:

Fragment.instantiate(this, MyFragment.class.getName());

无论如何,检查您是否从支持v4片段扩展,因为您正在使用supportFragmentManager

编辑:

首先检查您的片段是否从支持片段扩展。您可以检查观看片段导入。这是导入:

import android.support.v4.app.Fragment;

如果你有这个导入import android.app.Fragment;,你正在将正常片段和支持片段混合在一起。

然后当您更换片段或添加使用supportFragmentmanager而不是使用

FragmentListViewActivity toadd = new FragmentListViewActivity();

使用此:

FragmentListViewActivity toadd = Fragment.instantiante(this,FragmentListViewActivity.class.getName());