gpt4 book ai didi

java - fragment 在应用程序中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:39 25 4
gpt4 key购买 nike

我有疑问,因为我不知道出了什么问题。不显示activity_main_fragment.xml的activity我也不知道为什么。您有什么建议要改变吗?我创建了两个 fragment fragment_a和fragment_b,当您单击 ListView 中的某个项目时,我想显示它们连接到fragment_a,信息在fragment_b中出现。

Communicator.java

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

}

字符串.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 。如果您的 fragment 还不是 the support library version 的实例,您也需要更改它。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com