All Products
Search
Document Center

Mobile Testing - Deprecated:java_lang_IllegalArgumentException

Last Updated:Mar 14, 2022

Problem description

This exception occurs when the method receives an invalid parameter.

Solution

IllegalArgumentException occurs when the method receives an invalid parameter. Pass in correct parameters based on the invalid function parameters described in the stack information.

Sample code

  public static void getUserAgent(Context context) {
    WebView webview = new WebView(context);
    WebSettings settings = webview.getSettings();
    userAgent = settings.getUserAgentString();
    Log.i("UserAgent:" + userAgent);
  }

Example 1

  java.lang.IllegalArgumentException: HTTP parameters may not be null
    at org.apache.http.params.HttpProtocolParams.getUserAgent(HttpProtocolParams.java:150)
    at org.apache.http.impl.client.AbstractHttpClient.isMoMMS(AbstractHttpClient.java:790)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:563)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:520)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:498)
    at com.g.utils.HttpClientUtil.post(HttpClientUtil.java:99)
    at com.g.utils.LoadDataFromServer$2.run(LoadDataFromServer.java:195)

This exception occurs because the parameter is empty and the character set fails to be obtained.

In this example, the getUserAgent method reports an exception because the userAgent variable is empty. A user agent (UA) is a special string header that allows a server to identify the operating system and version, CPU type, browser type and version, browser rendering engine, browser language, and browser plug-ins.

Example 2

  java.lang.IllegalArgumentException
    at android.view.Surface.nativeLockCanvas(Native Method)
    at android.view.Surface.lockCanvas(Surface.java:243)
    at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2435)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2409)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2253)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1883)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    at android.view.Choreographer.doFrame(Choreographer.java:544)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)

This exception occurs in multiple Android versions. It is because a bug in the Android system causes improper management of the WebView memory and therefore memory leaks. If the memory is too low, no memory resources can be applied for drawing. This exception occurs and the app stops responding.

Perform the following steps:

1. Manage the lifecycle of WebView objects in a forced manner. Manage WebView objects in the lifecycle management of the activity for WebView objects. When the onPause() method is invoked for the activity, destroy WebView objects and clear their containers. The memory occupied by WebView objects will be reclaimed by the system during the next GC.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
        View v = super.onCreateView(inflater, container, saved);
        mActivity = (BaseActivity) getSupportActivity();
        // this is the framelayout which will contain our WebView
        mWebContainer = (FrameLayout) v.findViewById(R.id.web_container);
        return v;
    }
    public void onResume() {
        super.onResume();
        // create new WebView and set all its options.
        mWebView = new WebView(mActivity);
        mWebView....
        // add it to the container
        mWebContainer.addView(mWebView);
        // if data is available, display it immediately
        if(mTopic != null) {
            mWebView.loadDataWithBaseURL("file:///android_asset/", mTopic.getHtmlCache(),
                    "text/html", "UTF-8", null);
        }
    }
    @Override
    public void onPause() {
        super.onPause();
        // destroy the webview
        mWebView.destroy();
        mWebView = null;
        // remove the view from the container.
        mWebContainer.removeAllViews();
    }

2. Disable hardware acceleration for the app.

Add android:hardwareAccelerated="false" to the AndroidManifest.xml file to disable hardware acceleration. This method can avoid the bug in the Android system. However, we recommend that you do not use this method for games or apps that have high requirements for fluency, because it may cause apps to become sluggish.

Example 3

  01-17 00:19:44.703: E/Surface(9731): Surface::lock failed, already locked
  01-17 00:19:44.796: E/SurfaceHolder(9731): Exception locking surface
  01-17 00:19:44.796: E/SurfaceHolder(9731): java.lang.IllegalArgumentException
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.Surface.lockCanvasNative(Native Method)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.Surface.lockCanvas(Surface.java:314)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.SurfaceView$3.internalLockCanvas(SurfaceView.java:762)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.SurfaceView$3.lockCanvas(SurfaceView.java:741)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at com.frequency.FreqTapArea$2.onTouch(FreqTapArea.java:54)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.View.dispatchTouchEvent(View.java:3897)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1737)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1153)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1721)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2200)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1884)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.os.Handler.dispatchMessage(Handler.java:99)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.os.Looper.loop(Looper.java:130)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at android.app.ActivityThread.main(ActivityThread.java:3835)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at java.lang.reflect.Method.invokeNative(Native Method)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at java.lang.reflect.Method.invoke(Method.java:507)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
  01-17 00:19:44.796: E/SurfaceHolder(9731):  at dalvik.system.NativeStart.main(Native Method))

You must execute the lockCanvas function to lock Canvas before performing Canvas operations. After operations, execute the unlockCanvasAndPost function to immediately release lock resources. This exception occurs because lock resources are not released after you invoke the lockCanvas() method. The correct process is:

  1. Invoke the mSurfaceHolder.lockCanvas(); method to obtain a Canvas object.

  2. Invoke the Canvas method for drawing: canvas.drawXXX(arg, arg, arg, arg);

  3. After the drawing is complete, invoke the mSurfaceHolder.unlockCanvasAndPost(c); method to release lock resources.

Example:
  public boolean draw(View arg0, MotionEvent arg1) {
          Canvas c = mSurfaceHolder.lockCanvas();
          // Draw something
      c.drawCircle(args, ... ,arg);
          mSurfaceHolder.unlockCanvasAndPost(c);
          return true;
  }

References