I have been using isSyncActive() to check is sync active. Everything working well on Android versions < 5.0 but on new android version 5.0 it throws exception java.lang.IllegalArgumentException: account must not be null. Is there any solution and why is this happening only in the above mentioned android version.
The code for the content resolver is open source: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/ContentResolver.java if you are interested. I'm also having trouble with other things associated with the 5.0 update, which is why I happened across this post. Looks like you need to pass a valid account object to the method to get it to work.
public static boolean isSyncActive(Account account, String authority) {
if (account == null) {
throw new IllegalArgumentException("account must not be null");
}
if (authority == null) {
throw new IllegalArgumentException("authority must not be null");
}
try {
return getContentService().isSyncActive(account, authority, null);
} catch (RemoteException e) {
throw new RuntimeException("the ContentService should always be reachable", e);
}
}
Related
I am making a app with Xamarin.Forms, the app in the iOS doesn't crash but in Android, Application is crashing randomly, even if I only switch the tabs.
What is the best way to find what is making the app to stop working?
Thanks
What is the best way to find what is making the app to stop working?
well to add exception handling
try {
// ...
} catch(Exception e) {
// ...
}
or like the below example
public static void main(String[] args) throws FileNotFoundException, IOException {
try{
ExceptionHandler(1);
ExceptionHandler(2);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
System.out.println(" error to be checked");
}
testException(0);
}
public static void ExceptionHandler(int i) throws FileNotFoundException, IOException{
if(i =1 ){
FileNotFoundException myException = new FileNotFoundException("error for code 1 "+i);
throw myException;
}else if(i =2){
throw new IOException("error on 2 ");
}
}
You can also look into a crash reporting service like Hockey App (https://hockeyapp.net/ -- the free level is enough for getting crash reports). You'll get crashes reported there, including crashes in code that you can't catch.
Crash reports aren't quite as handy has being able to break in the debugger, but it's often enough to point you in the right direction.
Instructions on integrating Hockey App to a Xamarin.Forms app: https://support.hockeyapp.net/kb/client-integration-cross-platform/how-to-integrate-hockeyapp-with-xamarin
I'm running into an issue similar to this one, except with java.util.Scanner. I have this static method:
public static void close(final Closeable c) {
if(c != null) {
Log.debug(TAG, "instance of " + c.getClass().getName());
try {
c.close();
} catch (IOException e) {
// ignore
}
}
}
When passed a Scanner on API 15, it crashes like so:
08-29 20:33:42.979: E/AndroidRuntime(2245): FATAL EXCEPTION: main
08-29 20:33:42.979: E/AndroidRuntime(2245): java.lang.IncompatibleClassChangeError: interface not implemented
08-29 20:33:42.979: E/AndroidRuntime(2245): at com.mycompany.myapp.IOUtil.close(IOUtil.java:36)
[more lines omitted]
The docs say that Scanner implements Closeable even if you set the doc API level below 15. Could this be a vendor-specific issue? I only have one API 15 device to test on, and nothing between that and API 19, which works fine.
Scanner implements Closeable since KitKat
You can check here
4.4 kitkat
https://android.googlesource.com/platform/libcore/+/kitkat-release/luni/src/main/java/java/util/Scanner.java
4.3_r3 https://android.googlesource.com/platform/libcore/+/android-4.3_r3/luni/src/main/java/java/util/Scanner.java
Not a direct answer to my question, but in light of Derek Fung's answer, my close() method now looks like this:
public static void close(final Closeable c) {
if(c != null) {
/* Several classes that were made Closeable in Java 1.7 also
* became Closeable in Android API 19, including java.net.Socket
* and java.util.Scanner. The fact that the minimum SDK version
* is lower than that does not cause a compiler error when objects
* of those types are passed to this method, resulting in a
* IncompatibleClassChangeError at runtime. Hence this seemingly
* pointless test. */
if(c instanceof Closeable) {
try {
c.close();
} catch (IOException e) {
// ignore
}
}
else {
Log.debug(TAG, c.getClass().getName() + " does not implement Closeable; attempting reflection");
try {
final Method m = c.getClass().getMethod("close");
m.invoke(c);
}
catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException e) {
// shouldn't happen
Log.warn(TAG, "could not close " + c.getClass().getName(), e);
} catch (InvocationTargetException e) {
// ignore
}
}
}
}
The reflection code is executed and seems to work fine on API 15.
I'm trying to build a plugin-System, where DexClassLoader is fetching code from other installed apks containing fragments(my plugins), and showing them in my host. This is working quite nice.
I also like to make the plugins hotswappable, this means I can change the code from a plugin, install it new and the host will notice and will load the new code. This also works, if I'm changing the code for the first time. (Although I thought it shouldn't, it seems I've got a wrong understanding of this code:
try {
requiredClass = Class.forName(fullName);
} catch(ClassNotFoundException e) {
isLoaded = false;
}
)
If i'm trying it a second time with the same plugin, the host shuts down at requiredClass = classLoader.loadClass(fullName); with something like
libc Fatal signal 7 (SIGBUS) at 0x596ed4d6 (code=2), thread 28814
(ctivityapp.host)
Does anybody has a deeper insight in the functionality of DexClassLoader and may tell me, what is happening here? I'm quite stuck at this.
Heres the full code of the method loading the foreign code:
/**
* takes the name of a package as String, and tries to load the code from the corresponding akp using DexclassLaoder.
* Checking if a package is a valid plugin must be done before calling this.
* The Plugin must contain a public class UI that extends Fragment and implements plugin as a starting point for loading
* #param packageName The full name of the package, as String
* #return the plugins object if loaded, null otherwise
*/
private Plugin attachPluginToHost(String packageName) {
try {
Class<?> requiredClass = null;
final ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,0);
final String apkPath = info.sourceDir;
final File dexTemp = context.getDir("temp_folder", 0);
final String fullName = packageName + ".UI";
boolean isLoaded = true;
// Check if class loaded
try {
requiredClass = Class.forName(fullName);
} catch(ClassNotFoundException e) {
isLoaded = false;
}
if (!isLoaded) {
final DexClassLoader classLoader = new DexClassLoader(apkPath, dexTemp.getAbsolutePath(), null, context.getApplicationContext().getClassLoader());
requiredClass = classLoader.loadClass(fullName);
}
if (null != requiredClass) {
// Try to cast to required interface to ensure that it's can be cast
final Plugin plugin = Plugin.class.cast(requiredClass.newInstance());
installedPlugins.put(plugin.getName(), plugin);
return plugin;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
Many thanks in advance!
Not that it really matters (As nobody is actually viewing this), or that I even understand what's going on, but deleting the corresponding file of the plugin in dexTemp.getAbsolutePath() before reloading it solves the problem.
PS: Tumbleweed-Badge, YAY!
I am building an Android Library and have a method getting some information about the device. Our target is to support 2.2 and up but was wondering if there is a way to collect information introduced in later versions (ex device serial in 2.3) and have the application set with version 2.2 to compile.
After searching around I found people using code like:
private static String getHardwareSerial() {
try {
return Build.SERIAL;
} catch (VerifyError e) {
//Android 8 and previous did not have this information
return Build.UNKNOWN;
}
}
However, with this code present, my sample application using our library fails to build when setting the build target to 8. Any suggestions or do we have to live with our clients setting their target to 9 to get this info?
You could do it through reflection:
public static String getHardwareSerial() {
try {
Field serialField = Build.class.getDeclaredField("SERIAL");
return (String)serialField.get(null);
}
catch (NoSuchFieldException nsf) {
}
catch (IllegalAccessException ia) {
}
return Build.UNKNOWN;
}
If the field isn't found (on earlier versions of the OS) it'll throw an exception that will be ignored and then fall through to return Build.UNKNOWN.
I've been working with Eclipse ADT for about 2 months. In that time, I have a small utility that allows me to select an IP Address and Port, and then send a file to that combo. The utility works as intended, but when I type in the wrong file name, the application hangs.
#Override
public void run() {
if (data != null) {
this.send(data);
} else if (this.file != null) {
if (file.exists()) {
this.send(file);
} else {
transferError = new FileNotFoundException("The specified file could not be found");
}
}
}
I've even tried to do the following in hopes that one or the other would throw, but I am unsuccessful in both.
public void run() {
if (data != null) {
this.send(data);
} else if (this.file != null) {
if (file.exists()) {
this.send(file);
} else {
transferError = new FileNotFoundException("The specified file could not be found");
}
}try {
throw new Exception("blah blah blah");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I've jockeyed around the exception, I've added the one above, I've tried placing it in different places, and all unsuccessful. Again, I'm exceptionally new to this, and got here from basically mincing various tcp client codes. Aside of creating a way to throw the exception correctly, please help me understand why the first one isn't working and why the one you suggest is.
in your else block you aren't throwin the transferError you create.
throw transferError;
However you probably won't be able to do that because FileNotFoundException is a checked exception and the run() method doesn't declare any thrown exceptions. You probably need to find a different way to present the error to the user, like with a Toast or something.
Your second block doesn't work because you are catching the exception you throw.