- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
您好,我使用了 fragment A,第二个 fragment 使用了 b。调用 b 并使用了 asynctask
方法,但我第一次使用它时效果很好,但第二次后应用程序崩溃,我的错误日志在下面::: 我工作三星平板电脑完美工作,但三星核心移动应用程序崩溃。
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
E/AndroidRuntime: at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
E/AndroidRuntime: at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
E/AndroidRuntime: at com.buymysari.fragment.SendImageServerFragment$SendImageServerTask.onPostExecute(SendImageServerFragment.java:158)
E/AndroidRuntime: at com.buymysari.fragment.SendImageServerFragment$SendImageServerTask.onPostExecute(SendImageServerFragment.java:1)
E/AndroidRuntime: at android.os.AsyncTask.finish(AsyncTask.java:631)
E/AndroidRuntime: at android.os.AsyncTask.access$600(AsyncTask.java:177)
E/AndroidRuntime: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4960)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
我的类(class)用于:
public class TakeCameraFragment extends Fragment {
Camera mCamera = null;
private CameraPreview mCameraPreview;
protected static final int MEDIA_TYPE_IMAGE = 0;
static String FilePAth = "";
Button takePicture;
static String base64string = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.camerafragment,
container, false);
mCamera = getCameraInstance();
Log.v("log_tag", "mCamera :: " + mCamera);
mCameraPreview = new CameraPreview(getActivity(), mCamera);
FrameLayout preview = (FrameLayout) rootView
.findViewById(R.id.camera_preview_fragment);
preview.addView(mCameraPreview);
takePicture = (Button) rootView
.findViewById(R.id.btnTakePicturefragment);
takePicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCamera.takePicture(null, null, mPictureframent);
}
});
return rootView;
}
public boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
releaseCamera();
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
private Camera getCameraInstance() {
try {
Log.v("log_tag", "camera try:::" + mCamera);
mCamera = Camera.open();
} catch (Exception e) {
// cannot get camera or does not exist
Log.v("log_tag", "camera catch:::" + mCamera);
releaseCamera();
}
return mCamera;
}
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraAppFragment");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
FilePAth = mediaStorageDir.getPath() + File.separator + "IMG_fragment_"
+ timeStamp + ".jpg";
Log.v("log", " FilePAth " + FilePAth);
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_fragment_" + timeStamp + ".jpg");
return mediaFile;
}
PictureCallback mPictureframent = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
SetPictureImageFragment fm2 = new SetPictureImageFragment();
fragmentTransaction.replace(R.id.relative_camerafragment_id,
fm2, "HELLO");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Bundle bundle = new Bundle();
bundle.putByteArray("position", data);
fm2.setArguments(bundle);
mCamera.startPreview();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
}
使用的第二个 fragment :
public class SetPictureImageFragment extends Fragment {
ImageView img;
Bundle bundle;
byte[] path;
byte[] byteArrayimage;
Button conform;
float x;
Bitmap b;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.capturepicturefragment, null);
Log.v("log_tag","SetPictureImageFragment");
bundle = this.getArguments();
path = bundle.getByteArray("position");
Log.v("log_tag","SetPictureImageFragment ::: Path :: "+path);
img = (ImageView) view.findViewById(R.id.camera_preview_fragment_imageview);
conform=(Button)view.findViewById(R.id.conform);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true; // inPurgeable is used to free up
// memory while required
Bitmap b = BitmapFactory.decodeByteArray(path, 0,path.length,options);
int width = b.getWidth();
int height = b.getHeight();
int newWidth = 500;
int newHeight = 500;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
int finalDegree = 0;
if(rotation == 0) {
finalDegree = 90;
}
if(rotation == 1) {
finalDegree = 270;
}
if(rotation == 2) {
finalDegree = 180;
}
if(rotation == 3) {
finalDegree = 90;
}
matrix.postRotate(finalDegree);
Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0,width, height, matrix, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArrayimage = stream.toByteArray();
img.setScaleType(ScaleType.CENTER);
img.setImageBitmap(resizedBitmap);
conform.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
SendImageServerFragment fm2 = new SendImageServerFragment();
// CreateStoreFragment fm2 = new CreateStoreFragment();
fragmentTransaction.replace(R.id.relative_cameraimageview_fragment, fm2,
"HELLO");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Bundle bundle = new Bundle();
bundle.putByteArray("position", byteArrayimage);
fm2.setArguments(bundle);
}
});
return view;
}
}
使用的第三个 fragment:::
public class SendImageServerFragment extends Fragment {
SegmentedRadioGroup segmentText;
SegmentedRadioGroupMale segmentTextMale;
Button sendImg;
EditText edt_txt;
MyApplication app;
View view;
Bundle bundle;
byte[] path;
String base64string = "";
Bitmap b;
String cat_id = "";
String gender = "";
private ProgressDialog progress;
InputMethodManager mgr;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
view = inflater.inflate(R.layout.sendimageserver, null);
// view.setFocusableInTouchMode(true);
// view.requestFocus();
bundle = this.getArguments();
path = bundle.getByteArray("position");
segmentText = (SegmentedRadioGroup) view
.findViewById(R.id.segment_text);
segmentTextMale = (SegmentedRadioGroupMale) view
.findViewById(R.id.segment_text_male);
sendImg = (Button) view.findViewById(R.id.btn_send_image);
edt_txt = (EditText) view.findViewById(R.id.edt_text_store_name);
edt_txt.requestFocus();
mgr = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(edt_txt, InputMethodManager.SHOW_IMPLICIT);
app = (MyApplication) getActivity().getApplicationContext();
sendImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int segTxt = segmentText.getCheckedRadioButtonId();
RadioButton radiocatButton = (RadioButton) view
.findViewById(segTxt);
int segmentTextMaleTxt = segmentTextMale
.getCheckedRadioButtonId();
RadioButton radioSexButton = (RadioButton) view
.findViewById(segmentTextMaleTxt);
// b = BitmapFactory.decodeByteArray(path, 0, path.length);
base64string = Base64.encodeToString(path, Base64.DEFAULT);
if (radiocatButton.getText().toString().equals("S")) {
cat_id = "4";
} else if (radiocatButton.getText().toString().equals("C")) {
cat_id = "1";
} else if (radiocatButton.getText().toString().equals("A")) {
cat_id = "5";
}
if (radioSexButton.getText().toString().equals("M")) {
gender = "Male";
} else if (radioSexButton.getText().toString().equals("F")) {
gender = "Female";
}
progress = new ProgressDialog(getActivity());
progress.setMessage("Loading...");
new SendImageServerTask(progress).execute("Home");
}
});
return view;
}
public class SendImageServerTask extends AsyncTask<String, Void, String> {
public SendImageServerTask(ProgressDialog progress) {
progress = progress;
}
public void onPreExecute() {
progress.show();
}
@Override
protected String doInBackground(String... arg) {
String msg = DBAdpter.userUpdateImageStore(app.getStoreId(),
edt_txt.getText().toString(), cat_id, base64string, gender);
Log.v("log_tag", " Msg " + msg);
return msg;
}
@Override
protected void onPostExecute(String result) {
// Create here your JSONObject...
progress.dismiss();
Toast.makeText(getActivity().getApplicationContext(), result,
Toast.LENGTH_LONG).show();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
StoreProfileGridFragment fm2 = new StoreProfileGridFragment();
fragmentTransaction.replace(R.id.relative_sendimage_send, fm2,
"HELLO");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
hideKeybord(edt_txt);
}
}
public void hideKeybord(View view) {
mgr.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
}
最佳答案
它被称为 state loss .你碰巧从 AsyncTask 提交了一个 FragmentTransaction。这是框架本身禁止的。
如果您可以接受状态丢失和用户意外行为的可能性,您可以使用
FragmentTransaction#commitAllowingStateLoss()
而不是
FragmentTransaction#commit()
无论如何,考虑阅读我提供的链接,这是一种很常见的情况。
编辑:另外,您的问题有一个解决方法。但我想,这样做的后果也可能导致状态损失。尽管如此:
在你的Fragment
中声明一个Handler
:
private Handler handler = new Handler();
然后,在您的 onPostExecute(..)
中执行以下操作:
@Override
protected void onPostExecute(String result) {
progress.dismiss();
Toast.makeText(getActivity().getApplicationContext(), result,
Toast.LENGTH_LONG).show();
handler.post(new Runnable() {
@Override
public void run() {
hideKeybord(edt_txt);
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
StoreProfileGridFragment fm2 = new StoreProfileGridFragment();
fragmentTransaction.replace(R.id.relative_sendimage_send, fm2,
"HELLO");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return;
}
关于java.lang.IllegalStateException : Can not perform this action after onSaveInstanceState:- Error in Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22713002/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 8年前关闭。 Improve t
暂时忘记能力的定义,只关注能力的“检查”(使用“授权!”),我看到 CanCan 添加了大约 400 毫秒,用于简单地检查用户是否具有特定的能力主题/模型。 这是预期的吗(我假设不是)?或者,有没有可
我正在阅读有关 Swift 的教程 ( http://www.raywenderlich.com/74438/swift-tutorial-a-quick-start ),它预定义为不显式设置类型,因
这主要是由于对 SQL 问题的回答。由于性能原因,有意省略了 UDF 和子查询。我没有包括可靠性并不是说它应该被视为理所当然,但代码必须工作。 性能永远是第一位的吗?提供了许多以性能为主要优先事项的答
我已经编写了一个简单的测试平台来测量三种阶乘实现的性能:基于循环的,非尾递归的和尾递归的。 Surprisingly to me the worst performant was the loop o
我已将 ui-performance 插件应用到我的应用程序中。不幸的是,在开发模式下运行应用程序时它似乎不起作用。例如,我的 javascript 导入是用“vnull”版本呈现的。 例如 不会
我有一个我操作的 F# 引用(我在各处添加对象池以回收经常创建和删除的短期对象)。我想运行结果报价;现在我使用了 F# PowerPack,它提供了将引用转换为表达式树和委托(delegate)的方法
我正在尝试在 Spark 服务器上运行 SparklyR 库中的机器学习算法。 1 个簇 8 核 24G内存 Ubuntu 16.04 星火2.2 独立配置 1名师傅/2名 worker 每个执行器的
我有一个数据库(准确地说是在 postgres 上运行),具有以下结构: user1 (schema) | - cars (table) - airplanes (table, again) .
我的应用程序在我的 iPad 上运行。但它的表现非常糟糕——我的速度低于 15fps。谁能帮我优化一下? 它基本上是一个轮子(派生自 UIView),包含 12 个按钮(派生自 UIControl)。
在完成“Scala 中的函数式编程原则”@coursera 类(class)第 3 周的作业时,我发现当我实现视频类(class)中所示的函数联合时: override def union(tha
我正在重构我的一个 Controller 以使其成为一项服务,我想知道不将整个服务容器注入(inject)我的 Controller 是否会对性能产生影响。 这样效率更高吗: innova.path.
我有一个要显示的内容很大的文件。例如在显示用户配置文件时, 中的每个 EL 表达式需要一个 userId 作为 bean 的参数,该参数取自 session 上下文。我在 xhtml 文件中将这个 u
我非常了解 mipmapping。我不明白(在硬件/驱动程序级别)是 mipmapping 如何提高应用程序的性能(至少这是经常声称的)。在执行片段着色器之前,驱动程序不知道要访问哪个 mipmap
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicate: What's the (hidden) cost of lazy val? (Scala) Scala 允许定义惰
一些文章建议现在 build() 包含在 perform() 本身中,而其他人则建议当要链接多个操作时使用 build().perform()一起。 最佳答案 build() 包含在 perform(
Postgres docs说 For best optimization results, you should label your functions with the strictest vol
阅读Zero-cost abstractions看着 Introduction to rust: a low-level language with high-level abstractions我尝
我想在 MQ 服务器上部署 SSL,但我想知道我当前的 CPU 容量是否支持 SSL。 (我没有预算增加 CPU 内核和 MQ PVU 的数量) 我的规范: Windows 2003 服务器 SP2,
因此,我在 Chrome 开发者工具 的性能 选项卡内的时间 部分成功地监控了我的 React Native 应用程序的性能。 突然在应用程序的特定重新加载时,Timings 标签丢失。 我已尝试重置
我是一名优秀的程序员,十分优秀!