4

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?

6
  • can you post full stack trace... point out which method exactly not found... Commented Jan 18, 2014 at 5:49
  • try with parent layout params. I mean if your text view inside the relative layout then try with relative layout. But I am not sure. Just try it and let me know if it is working or not Commented Jan 18, 2014 at 5:50
  • Can you post your onCreate menthod? Commented Jan 18, 2014 at 5:58
  • 1
    Why are you inflating layout in your TextView ? Commented Jan 18, 2014 at 6:00
  • The parent layout is a TableRow. Using TableRow.LayoutParams got rid of this error, but it also cleared the whole TableRow. ie:the text is deleted and rows below it shifted up 1 row. Commented Jan 18, 2014 at 6:01

2 Answers 2

19

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)));
Sign up to request clarification or add additional context in comments.

2 Comments

But this solution is not working if we want to set layout_margins or layout_gravity programmatically. Any suggestion?
What do you mean? The question and the answer are both about setting LP programmatically.
1

I have temp (source) LayoutParams, then i want to copy values of source LayoutParams to new LayoutParams. My Solution:

        LayoutParams newParams = null;
        if(AppUtils.getCurrentSdkVersion() < 19) {
            MarginLayoutParams p = sourceParams;
            newParams = new LayoutParams(p);
            newParams.width = sourceParams.width;
            newParams.height = sourceParams.height;
        } else {
            newParams = new LayoutParams(sourceParams);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.