我似乎能够让我的片段工作。 R文件中有一个条目。当然,更改ID并不能解决问题。关于如何解决这个问题的任何想法?
我尝试在Activity中创建它。但我只得到错误。
Caused by: java.lang.IllegalArgumentException: No view found for
id 0x7f080000 (test.jk.com.cameratest:id/relativeLayout) for
fragment SimpleCameraIntentFragment{42749310 #0 id=0x7f080000}
有我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
FragmentManager fragmentManager = getFragmentManager();
BaseFragment targetFragment = null;
targetFragment = SimpleCameraIntentFragment.newInstance(1);
fragmentManager.beginTransaction()
.add(R.id.relativeLayout, targetFragment)
.commit();
}
这是片段
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take picture"
android:id="@+id/buttonTakePicture"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:id="@+id/imageViewThumb"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="32dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewFullSize"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_above="@+id/buttonTakePicture" />
</RelativeLayout>
片段代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
view = inflater.inflate(R.layout.fragment_camera, container, false);
// Set the image view
mImageView = (ImageView)view.findViewById(R.id.imageViewFullSize);
mThumbnailImageView = (ImageView)view.findViewById(R.id.imageViewThumb);
Button takePictureButton = (Button)view.findViewById(R.id.buttonTakePicture);
// Set OnItemClickListener so we can be notified on button clicks
takePictureButton.setOnClickListener(this);
return view;
}
public static SimpleCameraIntentFragment newInstance(int sectionNumber) {
SimpleCameraIntentFragment fragment = new SimpleCameraIntentFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
答案 0 :(得分:0)
因为我从特定的Activity获取了fragmentManager。我无法发送片段,它必须是发送的活动ID。
这是活动的ID,这是正确的做法。
fragmentManager.beginTransaction()
.add(**R.id.camActivity**, targetFragment)
.commit();
此ID无效,因为它是片段的ID
fragmentManager.beginTransaction()
.add(**R.id.relativeLayout**, targetFragment)
.commit();