片段在app中不起作用

时间:2014-12-07 14:59:36

标签: java android xml android-fragments

我有疑问,因为我不知道出了什么问题。不显示activity_main_fragment.xml的活动,我不知道为什么。你有什么建议可以改变吗? 我创建了两个片段fragment_a和fragment_b,当我点击list_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);
    }
}

1 个答案:

答案 0 :(得分:0)

EcologicalInformationActivity扩展FragmentActivity,因此您应拨打getSupportFragmentManager()来代替getFragmentManager()。请参阅文档here。如果您的片段不是the support library version的实例,那么您也需要更改它。