在应用程序中不起作用

在应用程序中不起作用

我有问题,因为我不知道出什么问题了。我不显示activity_main_fragment.xml的活动,我也不知道为什么。您有什么建议要更改吗?
我创建了两个fragment_a和fragment_b片段,当您在列表视图中单击某个项目时,我想显示它们与fragment_a连接的信息。

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

最佳答案

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

关于java - fragment 在应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27343936/

10-08 23:59