2014-12-07 34 views
1

我有問題,因爲我不知道什麼是錯的。不顯示activity_main_fragment.xml的活動,我不知道爲什麼。你有什麼建議改變? 我創建了兩個片段fragment_a和fragment_b,並且我想要顯示他們連接到fragment_a時,當您單擊列表視圖中的某個項目信息來到fragment_b中時。片段無法在應用程序中工作

Communicator.java

public interface Communicator { 
    public void respond(int i); 

} 

的strings.xml

<string-array name="titles"> 
     <item>Zmiany</item> 
     <item>Ilę</item> 
     <item>Trudne pytania</item> 
    </string-array> 

    <string-array name="descriptions"> 
     <item>Zmianysadas</item> 
     <item>Ilęvcxcvcx</item> 
     <item>Trudne pytaxcvcxvxcnia</item> 
    </string-array> 

fragment_a.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#FFBB00" > 

    <ListView 
     android:id="@+id/listView22" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 

fragment_b.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" 
    android:background="#99CC00"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

</LinearLayout> 

activity_main_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/my_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 


    <fragment 
     android:id="@+id/fragment1" 
     android:name="com.odpad.odpadygdansk.FragmentA" 
     android:layout_width="150dp" 
     android:layout_height="match_parent" /> 

    <fragment 
     android:id="@+id/fragment2" 
     android:name="com.odpad.odpadygdansk.FragmentB" 
     android:layout_width="470dp" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

FragmentA.java

public class FragmentA extends Fragment implements AdapterView.OnItemClickListener{ 

    ListView list; 
    Communicator communicator; 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
    ViewGroup container, Bundle savedInstanceState) { 
     communicator = (Communicator) getActivity(); 
     list = (ListView) getActivity().findViewById(R.id.listView22); 
     ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.titles, android.R.layout.simple_list_item_1); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(this); 
     return inflater.inflate(
       R.layout.fragment_a, container, false);  
    } 

    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){ 
     communicator.respond(i); 
    } 

} 

FragmentB.java

public class FragmentB extends Fragment{ 
    TextView text; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
    ViewGroup container, Bundle savedInstanceState) { 
     text = (TextView) getActivity().findViewById(R.id.textView); 
     return inflater.inflate(
       R.layout.fragment_b, container, false); 
    } 

    public void changeData(int i) 
    { 
     Resources res = getResources(); 
     String[] descriptions = res.getStringArray(R.array.descriptions); 
     text.setText(descriptions[i]); 
    } 
} 

EcologicalInformationActivity.java

public class EcologicalInformationActivity extends FragmentActivity implements Communicator{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main_fragment); 
    } 

    @Override 
    public void respond(int i){ 
     FragmentManager manager = getFragmentManager(); 
     FragmentB f2 = (FragmentB) manager.findFragmentById(R.id.fragment2); 
     f2.changeData(i); 
    } 
} 

回答

0

EcologicalInformationActivity延伸FragmentActivity因此您應該撥打getSupportFragmentManager()來代替getFragmentManager()。請參閱文檔here。如果你的碎片還不是the support library version的實例,你也需要改變它。

相關問題