I want to deserialize a JSON-String using the lift-json library. In my Android application I'm using Scala 2.9.0 and lift-json_2.9.0_2.4-M1.
I took a simple example from lift-json readme, but everytime I try to extract values from the JSON-String I get a net.liftweb.json.MappingException when calling the Activity. It seems there are no args delivered to "extract".
Here is my Activity:
import _root_.android.app.Activity
import _root_.android.os.Bundle
import net.liftweb.json._
class JsonTest extends Activity {
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.mainactivity)
implicit val formats = DefaultFormats
case class NumList(numbers: List[Int])
val json = parse(""" { "numbers" : [1, 2, 3, 4] } """)
json.extract[NumList]
}
}
and here the exception I get:
06-29 12:09:31.548: ERROR/AndroidRuntime(405): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.fhs.spirit/de.fhs.spirit.JsonTest}: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
args=
arg types=
constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
args=
arg types=
constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
at net.liftweb.json.Meta$.fail(Meta.scala:185)
at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:257)
at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:280)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:298)
at net.liftweb.json.Extraction$.extract0(Extraction.scala:345)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:194)
at net.liftweb.json.Extraction$.extract(Extraction.scala:42)
at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:290)
at de.fhs.spirit.JsonTest.onCreate(JsonTest.scala:16)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
Would be great if you have an idea how to solve my problem.
Thank you!
Greetings,
Illaz
I suspect Lift uses reflection in this mode. It might not work at all in Android, or it may require that some stuff that got removed not be removed. I suggest you just try to use some of other alternatives instead.
See also this question -- my suspicion comes from the remark that it doesn't work on REPL. Not that REPL doesn't have reflection, but its package structure might be confusing to the Lift library.
Related
I have ocassional crash at my emulator, but for now never on tablet on which I am testing.
java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:346)
at java.util.LinkedHashMap$EntryIterator.next(LinkedHashMap.java:375)
at java.util.LinkedHashMap$EntryIterator.next(LinkedHashMap.java:373)
at java.util.HashMap.writeObject(HashMap.java:1024)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1229)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1215)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
at android.os.Parcel.writeSerializable(Parcel.java:1155)
at android.os.Parcel.writeValue(Parcel.java:1114)
at android.os.Parcel.writeMapInternal(Parcel.java:479)
at android.os.Bundle.writeToParcel(Bundle.java:1552)
at android.os.Parcel.writeBundle(Parcel.java:493)
at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1574)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3292)
at android.app.ActivityThread.access$2500(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
It seems from the log it happens when the activity is going to pause.
All my activity are accessing a common super class static object (global data), which indeed has some linkedhashmap inside, and I am saving this globaldata with putSerializable inside onSaveInstanceState. I have tried to put save/restore of globaldata inside my base activity class inside sattic synchronized methods, but it didn't helped.
My I know from yours experiences if this is some internal android bug (maybe just emulator bug) or If I am managing saving of global data (which has LinkedHashMap inside) wrong. Or is there another way to share common data between activites?
Thanks
I have this exception that I get in my crash reports from Android:
java.lang.NoSuchMethodError: android.content.SharedPreferences$Editor.apply
at com.problemio.ProblemioActivity.first_time_check(ProblemioActivity.java:231)
at com.problemio.ProblemioActivity.onCreate(ProblemioActivity.java:47)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4668)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
I am pretty new to the crash reporting system in Android. Why would this crash happen? I can not reproduce it on my device or the simulator.
What other info is needed from the crash report to determine the cause and how to fix this?
Thanks!
apply() was introduced in API level 9, and some devices that runs your app may be with lower version. consider changing this call to commit. As stated in the docs:
The SharedPreferences.Editor interface isn't expected to be
implemented directly. However, if you previously did implement it and
are now getting errors about missing apply(), you can simply call
commit() from apply().
Read the documentation:
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()
Look at the api version that it was started with.
I am getting following Exception when trying to run this code.
java.lang.NoSuchMethodError: org.apache.poi.POIDocument.< init >
Code Snippet:
try {
File file = new File(externalPath + "/abc.doc");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!");
run.setFontSize(2 * 18);
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream out = new FileOutputStream(new File(externalPath + "/agnew.doc"));
doc.write(out);
out.flush();
out.close();
} catch (Exception ex) {
Log.e("Exception==","=="+ex.toString());
ex.printStackTrace();
}
Logcat:
Logcat : FATAL EXCEPTION: main : java.lang.NoSuchMethodError: org.apache.poi.POIDocument. :
at org.apache.poi.hwpf.HWPFDocumentCore.(HWPFDocumentCore.java:145) :
at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:218) :
at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:186) :
at com.vikas.prudent.CreateDocument.onCreate(CreateDocument.java:45) :
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) :
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) :
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) :
at android.app.ActivityThread.access$2300(ActivityThread.java:125) :
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) :
at android.os.Handler.dispatchMessage(Handler.java:99) :
at android.os.Looper.loop(Looper.java:123) :
at android.app.ActivityThread.main(ActivityThread.java:4627) :
at java.lang.reflect.Method.invokeNative(Native Method) :
at java.lang.reflect.Method.invoke(Method.java:521) :
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) :
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) :
at dalvik.system.NativeStart.main(Native Method)
Just looking at the exception, it appears to be lib version mis-match. Looks like HWPFDocumentCore.java:145 is trying to construct a new POIDocument but cannot find the expected constructor to call. I would take a look at line 145 of HWPFDocumentCore.java and see what constructor it is expecting. Then look for a POI library which has such a constructor in the POIDocument.
It sounds like you have two copies of Apache POI on your classpath, and old one and a new one. My hunch is that your HWPF jar (Scratchpad) is new, but it's picking up an old core POI jar, which is why you're getting the exception.
What you need to do is review all the jars on your classpath, and identify the POI related jars, then ensure you have a consistent set of them.
The POI FAQ has and entry on this very problem, along with some Java code you can use to print out which jar the POI classes come from. If you can't spot the wrong jars directly, try porting something like the code shown there to your android code to help you find the older jar.
I have followed the below steps to test my app.
I have created Android Project.
I have created the debug signature for my sample app and test app(which need to be tested)
Place the sampleapp_debug.apk in the bin folder (where my workspace is there)
Installed the testapp_debug.apk in the emulator.
Now i am running the project as a Android JunitTest.
I am getting the below error.. How to resolve this..
java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.NullPointerException: Method name must not be null.
at java.lang.ClassCache.findMethodByName(ClassCache.java:297)
at java.lang.Class.getMethod(Class.java:985)
at android.test.suitebuilder.TestMethod.getAnnotation(TestMethod.java:60)
at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:39)
at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:30)
at com.android.internal.util.Predicates$OrPredicate.apply(Predicates.java:106)
at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:42)
at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:31)
at com.android.internal.util.Predicates$NotPredicate.apply(Predicates.java:122)
at android.test.suitebuilder.TestSuiteBuilder.satisfiesAllPredicates(TestSuiteBuilder.java:254)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:190)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:373)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4218)
at android.app.ActivityThread.access$3000(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
I faced similar type of problem before. I resolved the same by using default constructor in Testcode rather than parametrized constructor. The following tutorial was useful for me
See this tutorial
I encounter the same error and this solution worked for me:
Do not use public TestTextView(String name) but public TestTextView()
as constructor.
as what W.Elsinga mention on his comment found here Link
Your referenced code was way old.
Just add default constructor.
public StartActivityTest() {
super(StartActivity.class);
}
I think the problem at method: android.test.suitebuilder.TestMethod.getAnnotation().
Try to call the method setName() at constructor of the activity.
See: RuntimeException while using ActivityUnitTestCase, but not while ActivityInstrumentationTestCase2
The exception is caused by android.test.suitebuilder.TestMethod.getAnnotation(), too.
I followed the advice given on the internet regarding working with Application classes in android.
I also added a field "android:name=.Session" to my Application object in the manifest.
The application is called "Session".
What do you think is the error? This is happening even before the my App starts.
This is the error stack:
**java.lang.RuntimeException: Unable to instantiate application com.appfire.Session: java.lang.InstantiationException: com.appfire.Session**
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:523)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3996)
at android.app.ActivityThread.access$2900(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
**Caused by: java.lang.InstantiationException: com.appfire.Session**
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1479)
at android.app.Instrumentation.newApplication(Instrumentation.java:957)
at android.app.Instrumentation.newApplication(Instrumentation.java:942)
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:518)
Make sure your Session class is public, is not static, and does not have a default constructor.
public class Session extends Application {}
In the Manifest application tag, make sure you include the fully-qualified class name of your custom Application object:
<application
android:name="com.mycompany.myapp.Session"
.... />
The Android docs do say the class name must be fully qualified:
http://developer.android.com/guide/topics/manifest/application-element.html#nm
(This is not the case for Activity class names that are included in the activity tag of the Manifest. Those can be relative, preceded with a '.').
My solution was to delete the whole project from Eclipse and load it again from SVN. Now it works