MVP-Mosby-Api10: NoSuchMethodError android.support.v4.app.FragmentActivity.isChangingConfigurations - android

I get this error on crashlytics panel:
Fatal Exception: java.lang.NoSuchMethodError
android.support.v4.app.FragmentActivity.isChangingConfigurations
com.hannesdorfmann.mosby.mvp.MvpFragment.shouldInstanceBeRetained (MvpFragment.java:91)
com.hannesdorfmann.mosby.mvp.delegate.MvpInternalDelegate.detachView (MvpInternalDelegate.java:70)
com.hannesdorfmann.mosby.mvp.delegate.FragmentMvpDelegateImpl.onDestroyView (FragmentMvpDelegateImpl.java:73)
com.hannesdorfmann.mosby.mvp.MvpFragment.onDestroyView (MvpFragment.java:106)
com.hannesdorfmann.mosby.mvp.MvpFragment.shouldInstanceBeRetained (MvpFragment.java:91)
I override manifest for library to use it with api level 10 and I already test it on android 2.3.3 and it was working ok! but now I see this crash on crashlytics. Hi I can fix this for my version? is crash related to api 10? because the method is for support v4 library so I can't understand why this occurred.

yes the method isChangingConfigurations() has been introduced with API 11:
https://developer.android.com/reference/android/app/Activity.html#isChangingConfigurations()
as part of theandroid.app.Activity plattform class (and not as part android.support.v4.app.FragmentActivity, but FragmentActivity extends Activity).
Hence, this won't work on API < 11.
You could implement isChangingConfigurations() in your Activity and either call
super.isChangingConfigurations() if API >=11 or implement your own thing if (API < 11). You may want to take a look at Activities source code, but I'm not sure how this could be back ported. https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/Activity.java#L5152
You could try to just return false if API < 11 . That would mean that the View's state (and Presenter) will not survive screen orientation changes. DISCLAIMER: That might also cause some other unwanted side effects I'm not aware of right now and could break with any future release of Mosby or support library.

Related

Setting a button background resource with a .png from internal storage

I have a button that I want to set the background of using a png file from internal storage. For android api 16 and up, this works fine:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
temp.setBackground(Drawable.createFromPath(filePath.toString()));
When running on an android tablet with 4.0.4, this part crashes the app with a nosuchmethod error (setBackground). After a little research, I see that setBackground is only available for api 16+. After looking around on SO and a few other places, it looks like I need to use setBackgroundDrawable (deprecated) or setBackgroundResource. I tried this:
filePath = getActivity().getFileStreamPath(colorCodes.get(i-1));
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
When logging it out, it shows that setBackgroundDrawable is running and not setBackground, but I get the same nosuchmethod error (setBackground).
The other option is setBackgroundResource, but it accepts an int and not a drawable. Can I convert from drawable to int for this purpose?
What can I do here to set the background of the button to a file in internal storage for APIs < 16?
Thanks.
***EDIT - ok, this is working. just missed a little part elsewhere in the code that had the same problem. However, is using a deprecated method really the only way?
Deprecation is a status applied to a computer software feature,
characteristic, or practice indicating it should be avoided, typically
because of it being superseded. The term is also sometimes used for a
feature, design, or practice that is permitted but no longer
recommended in other areas, such as hardware design or compliance to
building codes. (source link)
Now we can answer your question.
Before API level 16 there is a method named setBackgroundDrawable. After API Level 16 google decided to write a new method setBackground for same purpose and recommend us to use new method. (Reason of this may be found by googling.)
You can use setBackgroundDrawable method for all api levels. There aren't any constraint for this. But using new method setBackground is recommended after API Level 16.
But you can only use setBackground method for devices which is running on API Level 16 or higher. So if you only implement setBackground method in your code, you are going to get MethodNotFoundException for devices which run below API Level 16.
To sum up; it is a best practice(for me it is a must) to use new methods then deprecated ones with supportted api version check such as;
if (android.os.Build.VERSION.SDK_INT < 16) {
temp.setBackgroundDrawable(Drawable.createFromPath(filePath.toString()));
} else {
temp.setBackground(Drawable.createFromPath(filePath.toString()));
}
I am not quite sure whether it is the only way to achieve this but in my opinion it is the correct one. Because the annotation #Deprecated defines the method to be superseded (in most cases) it automatically implies you can (I would even say should) use it to address older versions which are the targeted versions of this method.

How to avoid NoClassDefFoundError with different Android API versions

I'm developing an application for APIs from v10 to v17.
My activity implements OnDragListener which is available only from API v14.
public class MyActivity extends Activity implements View.OnDragListener {
....
}
So, when application is installed on device with API v10 it fails to load activity with java.lang.NoClassDefFoundError.
I understand that this won't work on API v10, that's OK. I'm handling this inside the activity.
I just want to know What is the practice to handle different API versions when it comes to features like this?
First of all you can check API levels and depending on that to execute or not your code. For example :
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
// don't use it.
} else {
// use the new API :
// myView.setOnDragListener(...);
}
You should not implement interfaces from new API's in Activity if you want to support old android versions too. Instead of that use the approach above.
In your case you should not declare the activity to implement View.OnDragListener. Just do the logic in another class and instantiate it only when you need it (I suppose you add the listener only for API >= 14).

API Level error

In an Activity I have a code that shows this error, but only if you press save.
Call requires API level 11 (current min is 8): android.widget.SearchView#setSearchableInfo
If I change the android:minSdkVersion to 7, it works, but when I save the code again, the same error is thrown. The minSdk must then be changed back to 8,...
What is wrong?
SearchView is available since API lvl 11.
Since your minimum sdk is 8 (lower than 11), Lint will give an error when using SearchView.
You can remove that error by using #TargetApi annotation before your method or class.
But you have to make sure you use a conditional statement before using SearchView to check if it is available, and provide an alternative for earlier versions.
Here's what your code should look like:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
void yourMethod(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
// use SearchView
} else {
// use some other backward compatible custom view
}
}
The SearchView exists in Android from the version 11 ant more.
So, if you would like to use the SearchView in your code, you have to put the minSdkVersion in your manifest to be 11. In the case you put a number smaller than 11, you will get an error, which is normal beause you are giving access to your app to some android versions which will not support your app.
This can be seen (thanks to #JesseJ) here: http://developer.android.com/reference/android/widget/SearchView.html
Added in API level 11

Conditional compiling in Android?

Is there any kind of conditional compiling for Android?
I had to make my project for Android 3 (API 11) just because ExifInterface has almost no useful attributes in Android 2.3 (API 10), despite the fact that it appeared in API 5 (!!??). I don't want to restrict my app to ICS users.
Thanks!
You can check dynamically the current API version of the device and do different stuff depending on that:
if(Build.VERSION.SDK_INT < 14) {
// Crappy stuff for old devices
}
else {
// Do awesome stuff on ICS
}
But be careful that if you need to instantiate classes that are not available for all APIs then you should do it in a runnable or in a separate wrapper class, e.g:
if(Build.VERSION.SDK_INT < 14) {
// Crappy stuff for old devices
}
else {
// Do awesome stuff on ICS
new Runnable() {
new AmazingClassAvailableOnICS();
(...)
}.run();
}
import android.annotation.TargetApi;
and then use annotations:
#TargetApi(11)
public void methodUsesAPI11()
{
...
Using this trick does a very simple thing: it allows compiling some code which contains API level 11 calls (classes, methods, etc) and still set android:minSdkVersion="8" in the manifest. Nothing more, nothing else.
The rest is up to you. You must check platform version before you call methodUsesAPI11() or you handle exceptions in order to prevent app crash and perform other action on older platforms.
Checking Build.VERSION.SDK_INT or using annotations should suffice, however, this link I'd bookmarked might be relevant to your case:
http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html?m=1
You can use what they describe there to have classes that may not be compatible, but will never be loaded. It's not conditional compilation, but it may be what you need, however, it is a bit more complex.

TextView.getAlpha doesnt exist in Android

I want to get current alpha of textview i am using the following code but i am getting an error that
java.lang.NoSuchMethodError: android.widget.TextView.getAlpha
Please guide me.
This method since API Level 11. Check your API version.
View.getAlpha() only exists since API level 11. You are trying to running your code on a too-old version of Android.
If you absolutely require this functionality, then update your app's minSdkVersion in AndroidManifest.xml to prevent it running on older Android versions. If you can live without it, do a runtime check to see if the API level is high enough.
you can use Alpha method like below
Textview tv_password;
tv_password =(TextView) findviewById(R.id.tv1);
tv_password.getBackground().setAlpha(50);
you can't use getAlpha() method with Textview
View.getAlpha() available from API level 11.But if you want to use this on older api then you may use NineOldAndroids library available for using the Honeycomb (Android 3.0) animation API on all versions of the platform back to 1.0!
So use need to change for e.g.
mAlpha = mView.getAlpha();
to
mAlpha = ViewHelper.getAlpha(mView);
where mView is your view.
Note : Don't forget to import com.nineoldandroids.view.ViewHelper;

Categories

Resources