1

我把它Action Sherlock Bar放在我的应用程序(Android)中,我可以在我的设备4.2.2上成功运行,但是当我尝试在我的 Android 4.1.2上安装时,它崩溃了......

这是堆栈跟踪:

03-01 17:42:00.953: E/AndroidRuntime(15780): FATAL EXCEPTION: Timer-0
03-01 17:42:00.953: E/AndroidRuntime(15780): java.lang.IllegalStateException: Not on the main thread
03-01 17:42:00.953: E/AndroidRuntime(15780):    at maps.ap.q.b(Unknown Source)
03-01 17:42:00.953: E/AndroidRuntime(15780):    at maps.au.e.b(Unknown Source)
03-01 17:42:00.953: E/AndroidRuntime(15780):    at maps.z.ag.moveCamera(Unknown Source)
03-01 17:42:00.953: E/AndroidRuntime(15780):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:83)
03-01 17:42:00.953: E/AndroidRuntime(15780):    at android.os.Binder.transact(Binder.java:326)
03-01 17:42:00.953: E/AndroidRuntime(15780):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
03-01 17:42:00.953: E/AndroidRuntime(15780):    at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
03-01 17:42:00.953: E/AndroidRuntime(15780):    at com.axousis.realweather.MainActivity$2.gotLocation(MainActivity.java:134)
03-01 17:42:00.953: E/AndroidRuntime(15780):    at com.axousis.realweather.util.MyLocation$GetLastLocation.run(MyLocation.java:92)
03-01 17:42:00.953: E/AndroidRuntime(15780):    at java.util.Timer$TimerImpl.run(Timer.java:284)

编码:

public class MainActivity extends MySuperClass{

    private GoogleMap mapa = null;
    private int vista = 0;
    private Button checkButton; 
    private LatLng myCoords;
    private ProgressDialog pleaseWaitDialog;
    private LoadTask loadTask;
    private List<Marcador> marcadores = new ArrayList<Marcador>();
    private Geocoder geocoder;
    private SharedPreferences settings;
    private AutoCompleteTextView editTextAddress;

    private ActionBar actionBar;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_main);

        actionBar = getSupportActionBar();

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped);
            bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            getSupportActionBar().setBackgroundDrawable(bg);

            BitmapDrawable bgSplit = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped_split_img);
            bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
        }




        // Retrieve the shared preferences
        settings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);

        //Init geocoder google
        geocoder = new Geocoder(this, Locale.getDefault());      

        //Inicializamos la busqueda de la posicion
        LocationResult locationResult = new LocationResult(){
            @Override
            public void gotLocation(Location location){
                //Got the location!
                location.getLongitude();
                location.getLatitude();
                myCoords = new LatLng(location.getLatitude(),location.getLongitude());

                //Move camera
                LatLng userLocation = MAP_INITIAL_LAT_LON;              
                if (myCoords!=null) {
                    userLocation = myCoords;
                }                       
                CameraUpdate posicionInicial = CameraUpdateFactory.newLatLngZoom(userLocation, MAP_INITIAL_ZOOM_LEVEL);
                mapa.moveCamera(posicionInicial);

            }
        };
        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(this, locationResult);

        //Cargamos el mapa
        mapa = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        mapa.setMyLocationEnabled(true);
....

崩溃出现在

mapa.moveCamera(posicionInicial);

我不知道为什么...

我只知道在我的 android 4.2.2中它可以工作,但在我的 android 4.1.2中它崩溃了......

任何想法?

4

1 回答 1

0

您正在尝试从主线程中访问 UI 元素。

尝试这个

MainActivity.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
               mapa.moveCamera(posicionInicial);
            }
        });
于 2013-03-01T19:59:28.680 回答