我无法在doInBackground Android

时间:2015-11-15 18:39:22

标签: android google-maps android-asynctask

我是新来的。所以首先。谢谢你的帮助。

我把这个片段称为buscar_cancha:

emas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/map"
        tools:context="com.example.angel.kickup.Buscar_cancha"
        android:name="com.google.android.gms.maps.SupportMapFragment">

    </fragment>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:fillViewport="false">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                >
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:background="#005577"
                    android:id="@+id/cabeza">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Nombre de Cancha"
                        android:textSize="20dp"
                        android:layout_alignParentLeft="true"
                        android:id="@+id/cancha"
                        android:layout_centerVertical="true"
                        />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/money"
                        android:layout_alignParentRight="true"
                        android:id="@+id/money"
                        android:layout_marginLeft="10dp"
                     />
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/money"
                        android:layout_toLeftOf="@id/money"
                        />
                </RelativeLayout>
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/cuerpo"
                    android:layout_below="@+id/cabeza">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/estrella"
                        android:layout_alignParentLeft="true"
                        android:id="@+id/estrella1"/>
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/estrella"
                        android:id="@+id/estrella2"
                        android:layout_toRightOf="@+id/estrella1"/>
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/estrella"
                        android:id="@+id/estrella3"
                        android:layout_toRightOf="@+id/estrella2"/>
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/estrella"
                        android:id="@+id/estrella4"
                        android:layout_toRightOf="@+id/estrella3"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Clima 24°C"
                        android:textSize="20dp"
                        android:id="@+id/clima"
                        android:layout_below="@+id/estrella1"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/material"
                        android:text="Material: Grass Sintetico"
                        android:textSize="20dp"
                        android:layout_alignParentRight="true"
                        android:layout_below="@+id/estrella1"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/tamanio"
                        android:text="Tamaño: 5vs5"
                        android:textSize="20dp"
                        android:layout_below="@+id/material"
                        android:layout_alignParentRight="true"
                        android:layout_alignStart="@+id/material"
                        android:layout_alignLeft="@+id/material"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/direccion"
                        android:text="Paucarpata 123"
                        android:textSize="20dp"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentLeft="true"
                        android:layout_below="@+id/tamanio"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/precio"
                        android:layout_toRightOf="@+id/direccion"
                        android:layout_below="@+id/tamanio"
                        android:layout_alignParentBottom="true"
                        android:text="Precio: 50"
                        android:textSize="20dp"
                        android:layout_alignLeft="@+id/tamanio"
                        android:layout_alignStart="@+id/tamanio"/>
                </RelativeLayout>
            </RelativeLayout>



        </LinearLayout>
    </ScrollView>

</LinearLayout>

我把它叫做替换另一个片段。父片段是:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerlayout">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragmentholder">

    </FrameLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/navlist"
        android:background="#dedede"
        android:layout_gravity="start"
        >
    </ListView>
</android.support.v4.widget.DrawerLayout>

我的代码需要一些帮助。我不知道,但我无法在AsyncTask上获得我的位置。

        @Override
    protected Location doInBackground(Void... params) {
        //mMap.setMyLocationEnabled(true);
        loc = mMap.getMyLocation();
        return loc;
    }

我知道我无法在doInbackground更改我的用户界面,因此我在doInBackground中尝试了该操作系统并在onPostExecute上修改了我的用户界面。

@Override
    protected void onPostExecute(Location loc) {

        super.onPostExecute(loc);

        if (loc != null) {
            LatLng latLang = new LatLng(loc.getLatitude(), loc
                    .getLongitude());
            mMap.addMarker(new MarkerOptions().position(latLang).title("Here I am"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLang));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(13));

        }
        else{
            Toast my= Toast.makeText(getContext(), "Still doenst work",Toast.LENGTH_SHORT);
            my.show();
        }

但我得到了这个

 11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime: Process: com.example.angel.kickup, PID: 10376
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:300)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: Not on the main thread
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at com.google.a.a.ae.b(Unknown Source)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at com.google.maps.api.android.lib6.c.c.a(Unknown Source)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at com.google.maps.api.android.lib6.d.eu.j(Unknown Source)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at com.google.android.gms.maps.internal.j.onTransact(SourceFile:281)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at android.os.Binder.transact(Binder.java:361)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.getMyLocation(Unknown Source)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at com.google.android.gms.maps.GoogleMap.getMyLocation(Unknown Source)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at com.example.angel.kickup.Buscar_cancha$MyPosition.doInBackground(Buscar_cancha.java:132)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at com.example.angel.kickup.Buscar_cancha$MyPosition.doInBackground(Buscar_cancha.java:121)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
11-15 13:17:41.204 10376-11279/com.example.angel.kickup E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841) 
11-15 13:17:41.804 10376-10376/com.example.angel.kickup W/ManagedEGLContext: doTerminate failed: EGL count is 2 but managed count is 1
11-15 13:17:42.044 10376-11210/com.example.angel.kickup D/dalvikvm: GC_FOR_ALLOC freed 4081K, 38% free 7160K/11436K, paused 33ms, total 33ms
11-15 13:17:42.424 10376-11279/com.example.angel.kickup I/Process: Sending signal. PID: 10376 SIG: 9

当我将函数getMyLocation放在onPostExecute()方法上时。有用。但只有当我将电源设备底部按下两次时。我决定使用Asynctask因为我用OnReadyMap尝试了它并且它不起作用。所以我认为问题出在咨询时。我需要某种解决方案或提示。谢谢

0 个答案:

没有答案