(Android) ANTLRStringStream throwing exception - android

I've been trying to get ANTLR-3.5-complete.jar to work on an Android app, but I can't get it to work! Bart Kiers's answer HERE has helped me a lot, but I still can't get it to work.
I'm trying to make a Button that, when clicked, will validate whether the text in the EditText view is correct according to my grammar.g, but it doesn't matter whether the text I input is correct or not, my app always crashes when I click the button while instantiating the ANTLRStringStream. Here's what I have:
public void onClickVerify(View v) throws TypeNotPresentException {
String source = "foobar";
Log.e("TestDebug", "Instantiating views");
TextView out = (TextView) findViewById(R.id.textView1);
EditText in = (EditText) findViewById(R.id.editText1);
try {
Log.e("TestDebug", "Getting source from EditText");
source = in.getText().toString();
Log.e("TestDebug", "source = " + source);
Log.e("TestDebug", "Instantiating stream");
ANTLRStringStream stream = new ANTLRStringStream(source);
Log.e("TestDebug", "Instantiating lexer");
grammarLexer lexer = new grammarLexer(stream);
Log.e("TestDebug", "Instantiating parser");
grammarParser parser = new grammarParser(
new BufferedTokenStream(lexer));
Log.e("TestDebug", "Applying rule to parser");
out.setText(source + " = " + !parser.failed());
} catch (IllegalStateException ise) {
out.setText("Exception caught");
}
}
And here's what I see in the logs:
Instantiating views
Getting source from EditText
source = test33
Instantiating stream
And then the "Unfortunately, TestAntlrApp has stopped." window pops up.
EDIT: Here's more of my logcat.
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
... 11 more
Caused by: java.lang.NoClassDefFoundError: org.antlr.runtime.ANTLRStringStream
at com.test.antlr.MainActivity.onClickVerify(MainActivity.java:89)
Something about not finding the class ANTLRStringStream class? However there seems to be no errors/warnings in the code and I can even see the class's hierarchy.

Due to a recent ADT update, you can't just add external libraries to the Build Path using Eclipse's conventional way of doing it. Instead, you have to put it inside the libs folder for it to be packed within the .apk. If you simply add it to the build path, it will be ignored when building your .apk.
Symptom : The app was not recognizing external library classes.
Solution : Put the .jar file in the libs folder.

Related

Android 4 returns null to download folder

i'm using the DownloadManager to download files and save in a folder inside downloads folder in my application context folder on internal storage, this works fine in Android 6 but in android 4 the path returns null.
This code works in android 6 but not in android 4
File p = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
i have change to this and works
File p = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if(p == null){
p = getApplicationContext().getDir(Environment.DIRECTORY_DOWNLOADS, MODE_PRIVATE);
}
My problem is in DownloadManager, when i use the follow code an NullPointerReference occurs
req.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS + "/viewer", fileName);
can anyone help me?
EDIT
This is the stack trace
FATAL EXCEPTION: main
java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:441)
at android.app.DownloadManager$Request.setDestinationFromBase(DownloadManager.java:508)
at android.app.DownloadManager$Request.setDestinationInExternalFilesDir(DownloadManager.java:470)
at br.com.digitaldoc.ddv4.activities.MainActivity$7.onClick(MainActivity.java:405)
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:958)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1088)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2861)
at android.widget.AbsListView$1.run(AbsListView.java:3535)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4931)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
at dalvik.system.NativeStart.main(Native Method)
and this line MainActivity.java:405 is
req.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS + "/viewer", fileName);

Why does Application created twice?

My application is doing quite a bit of initialization in the Application::onCreate() method, and I have a check that will throw an exception if it is called twice.
However, in some specific circumstance, the Application::onCreate() gets called twice. I added the following Log statement in onCreate(): (instance is a static variable)
Log.i(TAG, "Application::ctor, this=" + this + " instance=" + instance + " pid="
+ android.os.Process.myPid());
I got two logs (normally, instance == null):
09-20 19:14:42.151 {main} [INFO] [Application] Application::ctor, this=com.shawnma.MyApplication#428886b0 instance=null pid=10741
09-20 19:14:50.116 {main} [INFO] [Application] Application::ctor, this=com.shawnma.MyApplication#42923e00 instance=com.shawnma.MyApplication#428886b0 pid=10741
In this case, I returned from the onCreate() method - it won't hurt since everything should always be in memory. However, another exception shows immediately after that from Android system:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.innopath.mobilemd/com.innopath.mobilemd.ui.LauncherActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:100)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:51)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2019)
... 11 more
It seems the second Application creation is triggered by an ALARM_MANAGER wake up. But still, the first creation of the app should have been done and saved in a memory location?
Check if there is another process specified in your AndroidManifest. If you app contains two processes that use the same Application, the the onCreate method in Application will be invoked twice.

Error Loading Runtime library in Android Studio

In my android project I am using an external jar library which utilizes a runtime library libiconv.so. I have the library included in my project's lib directory. The library is included for all three architectures in the following directory hirarcy.
libs>
armeabi>libiconv.so
armeabi-v7a>libiconv.so
x86>libiconv.so
But I am getting Exceptions as logged by log cat:
05-23 12:18:58.857 3081-3081/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load libiconv.so from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.tariq.buynow-1.apk,libraryPath=/data/app-lib/com.tariq.buynow-1]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at com.tariq.buynow.CameraActivity.<clinit>(CameraActivity.java:30)
Where at CameraActivity.java:30 is:
static { System.loadLibrary("libiconv.so"); }
I have also tried
static { System.loadLibrary("iconv"); }
Is it some thing to do with gradle configuration, as I am new to Android Studio, or the error source is something else?
I had this same problem trying to use ZBar SDK. You most likely need to edit your build.gradle to make sure that the native libraries are included in your APK. This is how I fixed your problem: https://stackoverflow.com/a/16993006/2221876
System.loadLibrary() looks in in the lib directories of the device, not the application. To do what you want you'll need to copy the lib from the app onto the phone's file system, and load it from there. I would suggest putting the libs in /res/raw/ and do it this way:
try {
// Point the input stream to the library in /res/raw
InputStream is = context.getResources().openRawResource(R.raw.libiconv);
// Create a BufferedInputStream from the InputStream
BufferedInputStream bis = new BufferedInputStream(is);
// Set the FileOutputStream to the app's data directory
FileOutputStream fos = context.openFileOutput("libiconv.so", Context.MODE_PRIVATE);
byte data[] = new byte[1024];
int count;
while ((count = bis.read(data, 0, 1024)) != -1) {
fos.write(data, 0, count);
}
fos.close();
bis.close();
is.close();
System.load(context.getFilesDir() + "/libiconv.so");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
This may not work exactly, but the general gist of this should.
Add the following line into your build.gradle scripts
jniLibs.srcDirs = ['libs'] // copy the *.so to package
In the project view, you must can see :
Module/libs/ABI NAME/*.so

NoClassDefFoundError occured while initiate fragment

java.lang.NoClassDefFoundError occured at the first line:
FirstFragment firstTab = FirstFragment.newInstance(10);
mTabsAdapter.addTab(generateTabSpec(0, mTabHost),
firstTab.getClass(), null);
But I'm pretty sure the Fragment exist! (it's a fragment so don't need to check if declared in Manifest.) Even refactoring its name or move to other package didn't fix the problem. Here's the stacktrace:
ERROR/AndroidRuntime(23608): FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: dev.twothree.android.wtf.FirstFragment
at dev.twothree.android.activity.FragmentTab.setupTabHost(FragmentTab.java:110)
at dev.twothree.android.activity.FragmentTab.onCreate(FragmentTab.java:75)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2207)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4899)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
at dalvik.system.NativeStart.main(Native Method)
Is it a bug of Intell J 12.0.4? My version is Build #IC-123.169. Later I'll try it in the Eclipse, but still hope it can be resolved in Intelli J.
After open the whole project with Eclipse, config all the dependencies ONCE AGAIN, Eclipse point out that I accidentally CAST one of the custom library class to another class. Maybe that's why no successful compiled class file existed for later invoke.
Anyway, it seems Intelli-J didn't found that error while 3rd party library is involved, it could be a bug. But the blame is also on me for not find that so obvious error.

NoSuchMethod exception while creating and formatting .doc file using Apache poi-3.8 beta hwpf

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.

Categories

Resources