javaandroidexceptioncrashverifyerror

Stuck with Java.lang.VerifyError for a class created in onCreate()


I'm having a hard time trying to resolve this awful VerifyError, and can't find a solution.

I've seen a few threads like this one or this one but I'm not using 3rd party libraries nor calls to String.isEmpty() as it is suggested.

I just use supportv4 and v7 for these imports:

import android.support.v7.app.ActionBarActivity;  
import android.support.v4.app.FragmentManager;  
import android.support.v4.app.FragmentTransaction;  
import android.support.v4.app.LoaderManager;  
import android.support.v4.app.TaskStackBuilder;  
import android.support.v4.content.Loader;  

My onCreate in MapActivity looks like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_frame);

    if (mDataController == null) {
        mDataController = new DataController();
    }

    initData();

    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container, mMapViewFragment, "MapViewFragment");
    fragmentTransaction.commit();
}

initData is defined by this:

public void initData() {

    mListLines = Arrays.asList("ALL",
                  DataController.LINE_NAME_TRAM_1,
                  DataController.LINE_NAME_TRAM_2,
                  DataController.LINE_NAME_BUS_1);

    List<ArrayList<StationItem>> lAllStationsTemp = Arrays.asList(
                                        DataController.mTram1Stations,
                                        DataController.mTram2Stations,
                                        DataController.mBus1Stations);

    mAllStationsTramsBus = new ArrayList<StationItem>(DataController.mTram1Stations);
    for (int i=0; i<lAllStationsTemp.size(); i++) {
        List<StationItem> lCurrentLineStations = lAllStationsTemp.get(i);
        for (final StationItem lStationToInsert : lCurrentLineStations)
        {
            for (int j=0; j<mAllStationsTramsBus.size(); j++) {
                StationItem lCurrentStation = mAllStationsTramsBus.get(j);
                if (lStationToInsert.mStationName.equals(lCurrentStation.mStationName)) {
                    mAllStationsTramsBus.remove(j);
                }
            }
            mAllStationsTramsBus.add(lStationToInsert);
        }
    }

    //Sort results by station name property of object StationItem

    Collections.sort(mAllStationsTramsBus);

    mAllStations = new ArrayList<ArrayList<StationItem>>(Arrays.asList(
                    mAllStationsTramsBus,
                    mDataController.getStationListFromList(DataController.mTram1Stations),
                    mDataController.getStationListFromList(DataController.mTram2Stations),
                    mDataController.getStationListFromList(DataController.mBus1Stations)
                    ));


    List<ArrayList<StationItem>> lAllStationsTemp2 = Arrays.asList(
            DataController.mTram1Stations,
            DataController.mTram2Stations
            );


    mAllStationsTrams = new ArrayList<StationItem>(DataController.mTram1Stations);
    for (int i=0; i<lAllStationsTemp2.size(); i++) {
        List<StationItem> lCurrentLineStations = lAllStationsTemp2.get(i);
        for (final StationItem lStationToInsert : lCurrentLineStations)
        {
            for (int j=0; j<mAllStationsTrams.size(); j++) {
                StationItem lCurrentStation = mAllStationsTrams.get(j);
                if (lStationToInsert.mStationName.equals(lCurrentStation.mStationName)) {
                    mAllStationsTrams.remove(j);
                }
            }
            mAllStationsTrams.add(lStationToInsert);
        }
    }

    //Sort results by station name property of object StationItem

    Collections.sort(mAllStationsTrams);

    mMapViewFragment.mAllStations = mAllStations;
}

Here, I have 3 warnings: Type safety: a generic array of arrayList is created for a varargs parameter. One is for lAllStationsTemp, the second is for mAllStations, and the last one is for lAllStationsTemp2. I'm not sure wether this warning changes anything to my problem, but it's here.

And my DataController contains this:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;

import android.graphics.Color;

public class DataController {

    public static final String STATION_1 = "Station 1";
    public static final String STATION_2 = "Station 2";

    public static final String LINE_NAME_TRAM_1 = "T1";
    public static final String LINE_NAME_TRAM_2 = "T2";

    public static final String LINE_NAME_BUS_1 = "B1";

    protected static ArrayList<StationItem>  mTram1Stations;
    protected static ArrayList<StationItem>  mTram2Stations;

    protected static ArrayList<StationItem>  mBus1Stations;

    protected static ArrayList<LatLng> mTram1Locations;
    protected static ArrayList<LatLng> mTram2Locations;

    public DataController() {

            ArrayList<String> lConnection_S1 = new ArrayList<String>(Arrays.asList("T1"));
            ArrayList<String> lConnection_S2 = new ArrayList<String>(Arrays.asList("T1", "T2"));

            mTram1Stations = new ArrayList<StationItem>(Arrays.asList(
            new StationItem("Station 1","T1",0,0,lConnection_S1),
            new StationItem("Station 2","T1",0,0,lConnection_S2)
            ));

            mTram2Stations = new ArrayList<StationItem>(Arrays.asList(
            new StationItem("Station 1","T2",0,0,lConnection_S1),
            new StationItem("Station 2","T2",0,0,lConnection_S2)
            ));

            mBus1Stations = new ArrayList<StationItem>(Arrays.asList(
            new StationItem("Station 1","B1",0,0,lConnection_S1),
            new StationItem("Station 2","B1",0,0,lConnection_S2)
            ));

            mTram1Locations = new ArrayList<LatLng>(Arrays.asList(
            new LatLng(40.190, 3.20),
            new LatLng(40.191, 3.21),
            new LatLng(40.192, 3.22)
            ));

            mTram2Locations = new ArrayList<LatLng>(Arrays.asList(
            new LatLng(40.190, 3.20),
            new LatLng(40.191, 3.21),
            new LatLng(40.192, 3.22)
            ));
    }

    public static ArrayList<StationItem> getmTram1Stations() {
            return mTram1Stations;
    }

    public static void setmTram1Stations(ArrayList<StationItem> mTram1Stations) {
            DataController.mTram1Stations = mTram1Stations;
    }

    public static ArrayList<StationItem> getmTram2Stations() {
            return mTram2Stations;
    }

    public static void setmTram2Stations(ArrayList<StationItem> mTram2Stations) {
            DataController.mTram2Stations = mTram2Stations;
    }

    public static ArrayList<StationItem> getmBus1Stations() {
            return mBus1Stations;
    }

    public static void setmBus1Stations(ArrayList<StationItem> mBus1Stations) {
            DataController.mBus1Stations = mBus1Stations;
    }

    public static PolylineOptions createPolylinefromArray(ArrayList<LatLng> pArray, String pLineName, int pZIndex) {
    if (pArray.size() > 1) {

        PolylineOptions lPolyline = new PolylineOptions();

        for (int i = 0; i < pArray.size(); i++) {
            lPolyline.add(pArray.get(i));
        }

        lPolyline.color(DataController.getColorForLine(pLineName));

        if (pLineName.equals("1")) {
            lPolyline.width(12);
        } else {
            lPolyline.width(7);
        } 
        lPolyline.zIndex(pZIndex);

        return lPolyline;
    }
    return null;
    }

    public ArrayList<StationItem> getStationListFromList(List<StationItem> pStationArray) {

        ArrayList<StationItem> lResult = new ArrayList<StationItem>();

        for (int i=0; i<pStationArray.size(); i++) {
            if (i == 0) {
                lResult.add(pStationArray.get(i));
            } else {
                StationItem lStation1 = pStationArray.get(i-1);
                StationItem lStation2 = pStationArray.get(i);
                if (!lStation1.mStationName.equals(lStation2.mStationName)) {
                    lResult.add(pStationArray.get(i));
                }
            }
        }
        return lResult;
    }

    public static final int getColorForLine(String pLine) {
        if (pLine.equals("1")) {
            return Color.rgb(65, 117, 189);
        } else {
            return Color.rgb(169, 169, 169);
        }
    }

    public static final int getStationDrawableForLine(String pLine) {

        if (pLine.equals("1")) {
            return R.drawable.station_1;
        } else {
            return R.drawable.station;
        }
    }
}

And my stack trace is as follows:

java.lang.VerifyError: com.test.DataController
at com.test.MapActivity.onCreate(MapActivity.java:426)
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:876)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
at dalvik.system.NativeStart.main(Native Method)

And to be sure I give you all the details, here are the screenshots of my build path: Build Path 1 Build Path 2 Build Path 3

Any suggestion would be greatly appreciated. This error occurs on Android 2.2. Thanks for your help.


Solution

  • Ok, I just found the cause of this error. After several weeks, I had to ask to find it :)

    I was focusing on usual cases where this error occurs, but my code wasn't at fault. I didn't see this warning: "VFY: arbitrarily rejecting large method (regs=93 count=23019)".

    It appears that my constructor had too many variables declared in it, resulting in a memory management problem, as explained here.

    My constructor was like this:

    public DataController() {
    
        ArrayList<String> lConnection_S1 = new ArrayList<String>(Arrays.asList("T1"));
        ArrayList<String> lConnection_S2 = new ArrayList<String>(Arrays.asList("T1", "T2"));
        ...
        ArrayList<String> lConnection_S2034 = new ArrayList<String>(Arrays.asList("T1", "T2"));
    
        mTram1Stations = new ArrayList<StationItem>(Arrays.asList(
            new StationItem("Station 1","T1",0,0,lConnection_S1),
            new StationItem("Station 2","T1",0,0,lConnection_S2)
        ));
    
        mBus1Stations = new ArrayList<StationItem>(Arrays.asList(
            new StationItem("Station 1","B1",0,0,lConnection_S1),
            new StationItem("Station 2","B1",0,0,lConnection_S2)
        ));
    
        mTram1Locations = new ArrayList<LatLng>(Arrays.asList(
            new LatLng(40.190, 3.20),
            new LatLng(40.191, 3.21),
            new LatLng(40.192, 3.22)
        ));
    }
    

    I didn't write all my variables as it wouldn't be readable, but as you can see, it goes up to lConnection_S2034, and raises the VerifyError on Android 2.2.

    What I did to solve my problem was declaring these local variables as instance variables, and initialising them in the constructor like this:

    public class DataController {
    
        ArrayList<String> mConnection_S1;
        ArrayList<String> mConnection_S2;
        ...
        ArrayList<String> mConnection_S2034;
    
        public DataController() {
    
            mConnection_S1 = new ArrayList<String>(Arrays.asList("T1"));
            mConnection_S2 = new ArrayList<String>(Arrays.asList("T1", "T2"));
            ...
            mConnection_S2034 = new ArrayList<String>(Arrays.asList("T1", "T2"));
        }
    }