Android 从片段调用活动
如果这是一个愚蠢的问题,我道歉。我一直在寻找我的问题的答案,虽然我发现有几次提到这个问题,但提出的解决方案都没有解决我的问题。 在我的应用程序中,我从一个片段调用一个活动,然后我想通过在活动中单击按钮返回到该片段。 1-在fragment Discover_fragment中,我有一个列表视图,当按下一个项目时,活动开始:Android 从片段调用活动,android,android-fragments,Android,Android Fragments,如果这是一个愚蠢的问题,我道歉。我一直在寻找我的问题的答案,虽然我发现有几次提到这个问题,但提出的解决方案都没有解决我的问题。 在我的应用程序中,我从一个片段调用一个活动,然后我想通过在活动中单击按钮返回到该片段。 1-在fragment Discover_fragment中,我有一个列表视图,当按下一个项目时,活动开始: lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Overrid
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Intent intent1 = new Intent(getActivity(), EditThought.class);
startActivity(intent1);
}
});
我得到一个错误:
java.lang.IllegalArgumentException: No view found for id 0x7f0e006e (com.appleia.vocapp:id/containerView) for fragment Discover_Fragment{f1e7d01 #0 id=0x7f0e006e}
R.id.containerView是活动\u main中框架布局的id:
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerView">
</FrameLayout>
Discover_片段的布局为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toplayout"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/topButtons"
android:gravity="center"
android:layout_weight="1">
<com.appleia.SegmentedButtons.SegmentedRadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:orientation="horizontal"
android:id="@+id/segment_text"
android:checkedButton="@+id/button_one"
android:gravity="center">
<RadioButton android:id="@id/button_one"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:minWidth="110dp"
android:minHeight="33dip"
android:text="@string/Articles"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="@null"
android:gravity="center"
android:textColor="@drawable/radio_colors" />
<RadioButton android:id="@+id/button_two"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:minWidth="0dp"
android:minHeight="33dip"
android:text="Two"
android:button="@null"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@drawable/radio_colors"
android:visibility="gone"/>
<RadioButton android:id="@+id/button_three"
android:layout_height="wrap_content"
android:layout_width="110dp"
android:minWidth="110dp"
android:minHeight="33dip"
android:text="@string/Toughts"
android:button="@null"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@drawable/radio_colors" />
</com.appleia.SegmentedButtons.SegmentedRadioGroup>
</LinearLayout>
</LinearLayout>
<ListView
android:layout_below="@+id/toplayout"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
我做错了什么?
提前谢谢。像这样做吧
FragmentManager fragmentManager = getSupportFragmentManager();
Discover_Fragment fragmentS1 = new Discover_Fragment();
fragmentManager.beginTransaction()
.replace(R.id.containerView, fragmentS1).addToBackStack("fragBack");
.commit();
addToBackStack的说明-将此事务添加到后堆栈。这意味着事务在提交后将被记住,并在稍后从堆栈中弹出时反转其操作。lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Intent intent1 = new Intent(getActivity(), EditThought.class);
getActivity().startActivity(intent1);
}
});
@凌驾
公共链接(AdapterView arg0、视图arg1、,
整数位置,长arg3){
Intent intent1=新的意图(getActivity(),editThink.class);
getActivity().startActivity(intent1);
}
});
这些代码行将正常工作,因此使用它您可以通过调用,
startActivityForResult(Intent-Intent,int-requestCode)
启动触觉。
之后,您可以使用onBackPressed
返回活动。如果要将任何数据传回活动,则可以使用setResult(int-resultCode,Intent-data)
函数
请注意,如果要将数据返回到Activity类,则应调用getActivity().startActivityForResult(Intent-Intent,int-requestCode)
或者,如果需要将数据返回到片段类本身,只需使用
startActivityForResult(Intent-Intent,int-requestCode)
。它将在该片段中返回到活动结果。出现错误的原因:
当您要从返回到片段(发现\u片段)时
活动EditThink,您使用了以下内容:
FragmentManager fragmentManager = getSupportFragmentManager();
Discover_Fragment fragmentS1 = new Discover_Fragment();
fragmentManager.beginTransaction()
.replace(R.id.containerView, fragmentS1)
.commit();
FragmentManager fragmentManager = getSupportFragmentManager();
Discover_Fragment fragmentS1 = new Discover_Fragment();
fragmentManager.beginTransaction()
.replace(R.id.containerView, fragmentS1)
.commit();
这里,在上面的代码中,看看R.id.containerView
,它
不是来自您当前的活动(editThink活动)
现在,让我们看看解决方案:
正如Alex Chengalan告诉我们的,您只需在backpressed()上调用
方法而不是上面的代码。就这样
希望有帮助 谢谢你的快速回复。尝试了你的建议,我得到了完全相同的错误。。。我需要改变从片段中调用活动的方式吗?Alex,非常感谢。我尝试过startActivityFroResult,但不知道如何在Backpressed上实现。我需要在那里添加什么代码?onBackPressed
将在您按下后退按钮时自动调用。如果您想手动执行此操作并避免按下后退按钮,则应调用super.onBackPressed()代码>你想什么时候都行。很高兴听到有帮助的消息。快乐编码;)谢谢你的链接!非常感谢你。是的,我可以看出问题在于containerView不在EditThink活动中,但我真的不知道如何返回调用片段。我忘记了onBackPressed()的“super”部分,但是使用它就像是一种享受。感谢您花时间解释问题所在。