我正在构建一个需要片段事务的android应用程序。为了在运行时稍后工作(替换,删除,添加片段),我被告知您需要动态地将原始片段添加到活动中,以便稍后操作它们。这看起来并不困难,但我遇到了一个令人困惑的错误:
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c008b (com.powerofpixels.magpie:id/auth_fragment_container) for fragment AuthOptionsFragment{fa0ffa7 #0 id=0x7f0c008b}
因此android无法找到要动态添加到活动的片段容器,但为什么呢?
这是我的AuthActivity.java:
package com.powerofpixels.magpie.ui.activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.powerofpixels.magpie.R;
import com.powerofpixels.magpie.ui.fragment.AuthOptionsFragment;
public class AuthActivity extends FragmentActivity {
private static final String TAG = AuthActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
getSupportFragmentManager().beginTransaction()
.add(R.id.auth_fragment_container, new AuthOptionsFragment()).commit();
}
}
以下是我的AuthActivity xml资源文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ui.activity.AuthActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/logo_image_view"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
和片段xml资源文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.powerofpixels.magpie.ui.fragment.AuthOptionsFragment"
android:id="@+id/auth_fragment_container">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:clickable="true"
android:id="@+id/sign_up_text_view"
android:textSize="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log in"
android:clickable="true"
android:id="@+id/log_in_text_view"
android:layout_marginTop="40dp"
android:textSize="22dp"
android:layout_below="@+id/sign_up_text_view"
android:layout_alignLeft="@+id/skip_text_view"
android:layout_alignStart="@+id/skip_text_view" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Skip"
android:clickable="true"
android:id="@+id/skip_text_view"
android:layout_marginTop="40dp"
android:textSize="22dp"
android:layout_below="@+id/log_in_text_view"
android:layout_alignLeft="@+id/sign_up_text_view"
android:layout_alignStart="@+id/sign_up_text_view" />
</RelativeLayout>
那么这里的交易是什么?
感谢。
答案 0 :(得分:0)
替换
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ui.activity.AuthActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/logo_image_view"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
与
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ui.activity.AuthActivity">
<FrameLayout
android:id="@+id/auth_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
答案 1 :(得分:0)
您的AuthActivity应该有一个片段容器(通常它是一个FrameLayout),您可以在其中定义容器ID。您将在Fragment事务中使用该ID。