我很难解决这个可怕的VerifyError,并且找不到解决方案。
我见过一些像this one或this one的线程,但是我没有使用第三方库,也没有像建议的那样调用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条警告:类型安全:为varargs参数创建了arrayList的通用数组。一个用于lAllStationsTemp,第二个用于mAllStation,最后一个用于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)
并且确保我提供了所有详细信息,这是我的构建路径的屏幕截图:
任何建议将不胜感激。在Android 2.2上会发生此错误。
谢谢你的帮助。
最佳答案
好的,我只是找到了此错误的原因。几周后,我不得不要求找到它:)
我主要关注发生此错误的常见情况,但我的代码没有错。
我没有看到以下警告:“VFY:任意拒绝大型方法(regs = 93 count = 23019)”。
看来我的构造函数中声明了太多变量,导致内存管理问题as explained here。
我的构造函数是这样的:
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"));
}
}