很多问题不是发生一遍,解决了日后就能记住,防止以后类似问题出现时忘记怎么解决,这里做下记录,日后会持续更新!
1、Android studio 错误: 程序包 不存在
这个问题比较有针对性,可能你也报的是这个错误,但是我的解决方法可能并不适合你
主模块引用了module A作为lib,但是在module A中有的类不能混淆,比如javaBean类等
解决:
先确定已经设置了module的依赖关系,请看这里
主module下的build.gradle 里有这样类似的代码
dependencies {
compile project(':module A')
}
在作为lib的module的build.gradle中将release部分注释:
buildTypes {
release {
// minifyEnabled true
proguardFiles 'proguard-project.txt'
}
}
2、IllegalStateException: Can not perform this action after onSaveInstanceState
错误为:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1888)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:790)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:173)
at android.app.Activity.onKeyUp(Activity.java:2465)
at android.view.KeyEvent.dispatch(KeyEvent.java:2643)
at android.app.Activity.dispatchKeyEvent(Activity.java:2716)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:540)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:317)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2305)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4037)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3999)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3561)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3614)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3580)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3697)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3588)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3754)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3561)
原因在于我们的activity在某种场景下处于被kill 掉的边缘,系统就调用了onSaveInstanceState 方法,这个方法里面会调用 FragmentManager saveAllState 方法,将fragment 的状态保存,在状态保存后用户又主动调了 onBackPressed ,而这个方法的超类super.onBackPressed 方法会判断FragmentManager 是否保存了状态,如果已经保存就会抛出IllegalStateException 的异常 。
那么:onSaveInstanceState什么时候调用?
关于这个问题?看(这里这里)。
此外,还有一个比较类似的异常堆栈信息:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1341)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1352)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
这一类异常原因是在activity 调用onSaveInstanceState后,调用了fragmenttransaction 的commit 方法所致,如果说onBackPressed 的异常是出现在用户按back 后,那么在何处调用commit会导致IllegalStateException异常呢?实际上在api 11 (Honeycomb)之前,如果onSaveInstanceState 方法被调用,那么肯定是在onPause 生命周期方法前,但api11 以后,却只能保证在onStop 生命周期方法前,和onPause 方法并没有明确的先后调用顺序,正是由于此处生命周期的微小变化,导致api11 后,如果在onPause 和 onStop 之间调用commit ,将有可能抛出一个IllegalStateException异常告知状态丢失。
关于这类崩溃问题可以参考:@AlexLockwood ‘s blog : Fragment Transactions & Activity State Loss
最后,谈谈如何避免这一类的崩溃问题。
1、关于commit 方法的调用异常处理方法
(1)在activity生命周期函数内谨慎使用commit 方法 ,一般情况下如果能在onCreate 中调用,基本不会出现问题,但是如果在onResume onStart 等方法中调用就需要格外注意,比如说FragmetActivity 的onResume 方法 ,在某些场景下onResume 方法被调用之前,可能依然保存着之前的状态导致异常 。
Dispatch onResume() to fragments.
Note that for better inter-operation with older versions of the platform, at the point of this call the fragments attached to the activity are not resumed.
This means that in some cases the previous state may still be saved, not allowing fragment transactions that modify the state.
(2)尽可能避免在一些和生命周期函数异步的方法中调用commit,如AsyncTask 等。
(3)实在没法确定调用时机时,可以用commitAllowingStateLoss 代替 commit ,commitAllowingStateLoss 在状态丢失时不会抛出任何异常,但也正因为如此在一些必须确保状态被保存的场合,最好不要使用 commitAllowingStateLoss 方法。
2、针对onbackpress 导致的异常,从错误信息我们可以看到,问题所在是Activity#onBackPressed()方法
/**
* Called when the activity has detected the user's press of the back
* key. The default implementation simply finishes the current activity,
* but you can override this to do whatever you want.
*/
public void onBackPressed() {
if (mActionBar != null && mActionBar.collapseActionView()) {
return;
}
if (!mFragments.getFragmentManager().popBackStackImmediate()) {
finishAfterTransition();
}
}
我们看到onBackPressed()方法执行了两个操作,第一个是获取当前的FragmentManager,并且执行退栈操作,第二个是在退栈完成之后,执行finish方法。
继续查看源码,关键是FragmentManager实现类的popBackStackImmediate方法
@Override
public boolean popBackStackImmediate() {
checkStateLoss();
return popBackStackImmediate(null, -1, 0);
}
我们看到,在执行退栈动作之前,这里还有一步检查操作
private void checkStateLoss() {
if (mStateSaved) {
throw new IllegalStateException(
"Can not perform this action after onSaveInstanceState");
}
if (mNoTransactionsBecause != null) {
throw new IllegalStateException(
"Can not perform this action inside of " + mNoTransactionsBecause);
}
}
从这里,我们终于找到了崩溃日志上的异常文案
针对这个问题 也有几种解决手段
(1)在api11 以上 不调用super 的onSaveInstanceState 方法
protected void onSaveInstanceState(Bundle outState) {
//No call for super(). Bug on API Level > 11.
}
这样做的后果会导致activity 和 fragment 的所有状态,在activity 被系统杀死掉后无法保存,所以如果有保存状态的需要,这个方法是不适用的。
(2)重写 onBackPressed 方法,不调用super.onBackPressed ,直接调用finish 。原因很简单,super.onBackPressed 里面会调用FragmentManager popBackStackImmediate() 方法,如果直接掉finish 就不会触发异常,但这种情况只建议在没有使用 Fragments api时调用。
(3)通过反射手段在onSaveInstanceState 方法里调用 FragmentManagerImpl noteStateNotSaved方法将 mStateSaved 变量置为false ,这样既不会导致activity状态丢失,也能确保退出时不会抛出异常,算是比较优雅的处理途径,代码如下:
private Method noteStateNotSavedMethod;
private Object fragmentMgr;
private String[] activityClassName = {"Activity", "FragmentActivity"};
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
invokeFragmentManagerNoteStateNotSaved();
}
private void invokeFragmentManagerNoteStateNotSaved() {
//java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return;
}
try {
if (noteStateNotSavedMethod != null && fragmentMgr != null) {
noteStateNotSavedMethod.invoke(fragmentMgr);
return;
}
Class cls = getClass();
do {
cls = cls.getSuperclass();
} while (!(activityClassName[0].equals(cls.getSimpleName())
|| activityClassName[1].equals(cls.getSimpleName())));
Field fragmentMgrField = prepareField(cls, "mFragments");
if (fragmentMgrField != null) {
fragmentMgr = fragmentMgrField.get(this);
noteStateNotSavedMethod = getDeclaredMethod(fragmentMgr, "noteStateNotSaved");
if (noteStateNotSavedMethod != null) {
noteStateNotSavedMethod.invoke(fragmentMgr);
}
}
} catch (Exception ex) {
}
}
private Field prepareField(Class<?> c, String fieldName) throws NoSuchFieldException {
while (c != null) {
try {
Field f = c.getDeclaredField(fieldName);
f.setAccessible(true);
return f;
} finally {
c = c.getSuperclass();
}
}
throw new NoSuchFieldException();
}
private Method getDeclaredMethod(Object object, String methodName, Class<?>... parameterTypes) {
Method method = null;
for (Class<?> clazz = object.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
try {
method = clazz.getDeclaredMethod(methodName, parameterTypes);
return method;
} catch (Exception e) {
}
}
return null;
}
该解决方法来自博客:解决 IllegalStateException: Can not perform this action after onSaveInstanceState
3、Android studio Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (26.1.0) and test app (27.1.1) differ
添加以下代码即可:
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:27.1.1'
}
dependencies{...}
4、Android camera2 + android opencv34 出现错误:程序包android.hardware.camera2不存在
使用Android studio,同时使用Android camera2 和Android opencv34,出现编译错误:
程序包android.hardware.camera2不存在
解决方案如下:
在as中file->project structure->opencvlibrary340->修改compile sdk version 为21以上版本和 build tools version到更高版本,如下图所示: