在onCreate()中创建的类的Java.lang.VerifyError卡住

我很难试图解决这个可怕的VerifyError,并找不到解决方案。

我已经看到了这样或那样的一些线程,但是我没有使用第三方库,也没有按照建议调用String.isEmpty()。

我只为这些导入使用supportv4和v7:

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; 

我在MapActivity中的onCreate如下所示:

 @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由这个定义:

 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; } 

在这里,我有3个警告:类型安全性:为可变参数创建一个arrayList的通用数组。 一个是lAllStationsTemp,第二个是mAllStations,最后一个是lAllStationsTemp2。 我不确定这个警告是否会改变我的问题,但它在这里。

我的DataController包含这个:

 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; } } } 

我的堆栈跟踪如下:

 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) 

为了确保给出所有的细节,下面是我的构建路径的截图: 构建路径1建立路径2构建路径3

任何建议将不胜感激。 这个错误发生在Android 2.2上。 谢谢你的帮助。

    好的,我刚刚找到了这个错误的原因。 几个星期后,我不得不要求找到:)

    我正在关注发生此错误的常见情况,但是我的代码没有出错。 我没有看到这个警告:“VFY:任意拒绝大方法(regs = 93 count = 23019)”。

    看起来,我的构造函数有太多的变量声明,导致内存管理问题, 这里解释 。

    我的构造函数是这样的:

     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) )); } 

    我没有写所有我的变量,因为它是不可读的,但正如你所看到的,它会上升到lConnection_S2034,并引发Android 2.2上的VerifyError。

    我所做的解决我的问题是声明这些局部变量作为实例变量,并初始化它们在这样的构造函数:

     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")); } } 

    我在Kotlin项目中遇到了同样的错误。 彻底的让我疯狂找到原因。 这是我如何解决它

    坏的代码崩溃了:

     // MyClass is a class containing a String attribute called "text" // et_text is an EditText val c = MyClass( text = et_text.text?.toString()?.trim() ?: "" ) 

    解:

     val txt = et_text.text?.toString()?.trim() ?: "" val c = MyClass( text = txt ) 

    我不知道确切的原因,但我可以确保第一个代码崩溃,第二个不会。 必须在MyClass的初始化之前执行EditText字符串处理,而不是在它之内。

     Kotlin 1.1.4, build tools 25.0.3, support library 25.4.0 

    希望这可以节省时间给其他人:)