我正在尝试实现Commonsware CWAC相机,我遇到了一个问题,将它合并到现有的片段中。
我遇到了一个不能使用.add或.replace的问题,它希望我将CameraFragment更改为片段。
错误:
在类型FragmentTransaction中添加(int、片段、字符串)的方法不适用于参数(int、CameraFragment、String)
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="21" />
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import com.commonsware.cwac.camera.CameraFragment;
public void takePicture() {
CameraFragment f = new CameraFragment();
getFragmentManager().beginTransaction()
.add(R.id.contentFragment, f, TAG_CAMERA_FRAGMENT)
.commit();
}以前有人经历过这种事吗?这是整个片段。
public class FeedActivity extends Fragment implements OnClickListener {
ImageButton btnCamera, btnGallery;
private final String TAG_CAMERA_FRAGMENT = "camera_fragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_feed, container, false);
btnCamera = (ImageButton) view.findViewById(R.id.btn_Camera);
btnCamera.setOnClickListener(this);
btnGallery = (ImageButton) view.findViewById(R.id.btn_Gallery);
btnGallery.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_Camera:
Log.e("CAMERA", "CAMERA BUTTON PRESSED");
//takePicture();
break;
case R.id.btn_Gallery:
Log.e("Gallery", "GALLERY BUTTON PRESSED");
break;
}
}
public void takePicture() {
CameraFragment f = new CameraFragment();
getFragmentManager().beginTransaction()
.add(R.id.contentFragment, f, TAG_CAMERA_FRAGMENT)
.commit();
}
}发布于 2014-09-23 13:55:28
import android.support.v4.app.Fragment;以及:
import com.commonsware.cwac.camera.CameraFragment;是不相容的。您需要决定是使用来自Android支持包的片段,还是使用本地API级别的11+片段。您需要修改整个活动以支持您的选择(例如,如果您正在使用后端端口,则从FragmentActivity继承)。
如果您想使用backport,您将需要使用camera-v9库并导入com.commonsware.cwac.camera.acl.CameraFragment,尽管这也使用了ActionBarSherlock。如果您想要片段的后端(而不是ActionBarSherlock ),则需要分叉我的CameraFragment实现之一以支持该组合。
发布于 2014-12-12 07:54:41
发行
我想向您展示解决CameraFragment不兼容问题的方法。我的应用程序使用V4支持库(不知为什么),但没有使用ActionBarSherlock。因为我的目标平台是V11及以上版本(是的,不支持2.3和更低版本)。
com.commonsware.cwac.camera.CameraFragment ->不兼容V4支持库,因此不兼容我们的V4 FragmentActivity和其他片段。com.commonsware.cwac.camera.acl.CameraFragment ->不会工作,因为我没有ActionBarSherlock。通用解决方案
通常,应用程序会有很多片段,有些带有列表或网格。它们都有一些共同的功能。我们可能使用静态utils类,但它可能会造成比它解决的更多的问题。例如,它需要在函数调用中传递this片段指针。最后我们有了这些:
java.lang.Object
↳ android.support.v4.app.Fragment
↳ com.example.app.BaseFragment <- with common functions
java.lang.Object
↳ android.app.Fragment
↳ android.app.ListFragment
↳ com.example.app.BaseListFragment <- with common functions
java.lang.Object
↳ android.app.Fragment
↳ com.commonsware.cwac.camera.CameraFragment
↳ com.example.app.BaseCameraFragment <- with common functions问题
但是,我注意到一些关于我们扩展的片段的事情:
溶液
因此,我们可以(a)更改它们的父类,或者(b)克隆它们并从我们喜欢的任何片段扩展,而不是从它们扩展:
java.lang.Object
↳ android.support.v4.app.Fragment
↳ com.example.app.BaseFragment <- with common functions
↳ com.example.app.BaseListFragment
// with content from android.app.ListFragment
↳ com.example.app.BaseCameraFragment
// with content from com.commonsware.cwac.camera.CameraFragment(请注意,在适用的情况下,我们仍然必须遵守GNU许可证)
通常,我不喜欢改变他们的源代码,所以我可以很容易地更新到最新的版本时,可用。因此,我将保留原来的应用程序和使用方法b。唯一的工作是区别他们的版本与我的克隆。
福利
camera-v9的ActionBarSherlock,而且我有从v4片段扩展的所有片段。享受~
https://stackoverflow.com/questions/25995991
复制相似问题