Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: java.lang.NoSuchMethodError on LinearLayout$LayoutParams.<init>

I'm trying to add margins around a TextView, and have written the following:

TextView t = (TextView)getLayoutInflater().inflate(R.layout.plaintexttable, null);
t.setText(new String(cp.decryptChar(words[i].charAt(l))+""));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new    LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
params.setMargins(2,2,2,2);
t.setLayoutParams(params);
plainRow.addView(t);

When running the code, I get an error on LinearLayout.LayoutParams definition as follows:

01-18 05:50:41.228: E/AndroidRuntime(1950): java.lang.NoSuchMethodError: android.widget.LinearLayout$LayoutParams.<init>
01-18 05:50:41.228: E/AndroidRuntime(1950):     at com.napoleonicmonkey.cryptopuzzle.PuzzleScreen.refreshPuzzle(PuzzleScreen.java:97)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at com.napoleonicmonkey.cryptopuzzle.PuzzleScreen.onGlobalLayout(PuzzleScreen.java:61)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:655)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1748)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.view.Choreographer.doFrame(Choreographer.java:532)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.os.Handler.handleCallback(Handler.java:725)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.os.Looper.loop(Looper.java:137)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at java.lang.reflect.Method.invoke(Method.java:511)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-18 05:50:41.228: E/AndroidRuntime(1950):     at dalvik.system.NativeStart.main(Native Method)

This error is typically related to methods being added in newer versions of Android, but as far as I can see LinearLayout.LayoutParams has been around since API 1.

What else could be causing it?

like image 579
gedditoffme Avatar asked Dec 01 '22 02:12

gedditoffme


1 Answers

I have faced a similar error. This answer might be useful for someone. Also It can be applied to FrameLayout for example.

I suppose that VM cannot find not LayoutParams(ViewGroup.LayoutParams p) but LayoutParams (LinearLayout.LayoutParams source) constructor which is available from API 19.

This situation can be if your PuzzleScreen exdends LinearLayout. The code can be compiled with build target 19, but crashes on older Android version. The compiler puts the second method signature into bytecode, but this method isn't really available on devices. When the target is less than 19 the compiler put the right signature (expected behaviour) so that the code runs well everywhere.

Thus, the solution is to use an older target API or to make cast manually, for instance:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((ViewGroup.MarginLayoutParams)(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)));
like image 144
matreshkin Avatar answered Dec 02 '22 15:12

matreshkin