2014-11-20 58 views
0

中将一个片段切换到另一个片段错误显示无法将高光突出显示为片段,并且无法将片段概览转换为片段,并且仅在selectFrag()方法中出现错误。请任何人帮我解决这个问题。当我点击textview项目时,我想切换一个片段到其他片段。无法在android

public class MainActivity extends Activity { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
     }   

    public void selectFrag(View view){  
     Fragment fr; 
     if (view == findViewById(R.id.txt_highlite)) {   
     fr = (Fragment) new Highlight(); 
     } 
     else{ 
      fr = (Fragment) new Overview(); 
     }  
     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
     fragmentTransaction.replace(R.id.place_holder, fr); 
     fragmentTransaction.commit(); 
} 
    } 

//亮点类

public class Highlight extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, Bundle savedInstanceState) {  
     return inflater.inflate(R.layout.highlight, container, false); 
    } 
} 

//概述类

public class Overview extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, Bundle savedInstanceState) 
    { 
     return inflater.inflate(R.layout.overview, container, false); 
    } 
} 

//activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" >  
     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="100dp" > 

     <LinearLayout 
      android:id="@+id/lin_projectoverview" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dp" > 

      <TextView 
       android:id="@+id/text_Projectoverview" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="center" 
       android:text="Project Overview" 
       android:textSize="26sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/project_name" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginTop="65dp"   
      android:gravity="center" > 

      <TextView 
       android:id="@+id/txt_overview" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:textStyle="bold" 
       android:layout_marginLeft="12dp" 
       android:text="Overview" /> 

      <TextView 
       android:id="@+id/txt_highlite" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:textStyle="bold" 
       android:layout_marginLeft="12dp" 
       android:text="Highlight" />    
     </LinearLayout>   
    </RelativeLayout> 

    <fragment 
      android:name="com.example.fragment_move.Overview" 
      android:id="@+id/place_holder" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/>  
</RelativeLayout> 
+0

请贴'你的类import's。 – 2014-11-20 18:30:59

回答

0

检查import语句。根据您的getFragmentManager(),你的每一个扩展Fragment应该只有该片段导入类:

import android.app.Fragment; 
0

突出显示并概述从片段已经继承,那么,为什么ü他们再次铸造片段? 我会建议你这样做。

public void selectFrag(View view){  
    Fragment fr; 
    if (view.getId() == R.id.txt_highlite) {   
    fr = new Highlight(); 
    } 
    else{ 
     fr = new Overview(); 
    }  
    FragmentManager fm = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
    fragmentTransaction.replace(R.id.place_holder, fr); 
    fragmentTransaction.commit(); 
    } 

,并确保您的片段输入情况一贯无论是从支持库或以其他方式