简体   繁体   中英

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground()

I am a beginner in programming and I just want to create an app that display some markers on map using some coordinates from a database( hosted by 000webhost), using PHP and JSON.The map is working, but there are no markers. Please help me because I really cannot figure it out.

This is the error message i get:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                   Process: com.kid.compaq.licenta, PID: 24949
                                                   java.lang.RuntimeException: An error occured while executing doInBackground()
                                                       at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                       at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                       at java.lang.Thread.run(Thread.java:818)
                                                    Caused by: java.lang.IllegalStateException: Not on the main thread
                                                       at maps.f.g.b(Unknown Source)
                                                       at maps.z.D.a(Unknown Source)
                                                       at maps.ag.t.a(Unknown Source)
                                                       at uz.onTransact(:com.google.android.gms.DynamiteModulesB:167)
                                                       at android.os.Binder.transact(Binder.java:385)
                                                       at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
                                                       at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
                                                       at com.kid.compaq.licenta.MapsActivity$MarkerTask.doInBackground(MapsActivity.java:106)
                                                       at com.kid.compaq.licenta.MapsActivity$MarkerTask.doInBackground(MapsActivity.java:62)
                                                       at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                       at java.lang.Thread.run(Thread.java:818) 
05-28 19:22:28.324 731-919/? E/InputDispatcher: channel 'd1579c5 com.kid.compaq.licenta/com.kid.compaq.licenta.MapsActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
05-28 19:23:01.289 731-1342/? E/Sensors: handleToDriver handle(0)
05-28 19:23:01.290 731-1342/? E/Sensors: handleToDriver handle(0)
05-28 19:23:01.290 731-1342/? E/Sensors: new setDelay handle(0),ns(20000000)m, error(0), index(2)
05-28 19:23:01.299 731-939/? E/WifiStateMachine:  DisconnectedState (when=0 what=131143 arg1=10009 arg2=682) CMD_START_SCAN 10009 682 ic=0 proc(ms):0 dur:692 cnt=2252 rssi=-64 f=-1 sc=0 link=-1 tx=0,0, 0,0, 0,0  rx=0,0 fiv=20000 [on:0 tx:0 rx:0 period:121028] from screen [on:0 period:-131266637]
05-28 19:23:01.299 731-939/? E/WifiStateMachine:  ConnectModeState (when=-1ms what=131143 arg1=10009 arg2=682) CMD_START_SCAN 10009 682 ic=0 proc(ms):1 dur:692 cnt=2252 rssi=-64 f=-1 sc=0 link=-1 tx=0,0, 0,0, 0,0  rx=0,0 fiv=20000 [on:0 tx:0 rx:0 period:0] from screen [on:0 period:-131266637]
05-28 19:23:01.300 731-939/? E/WifiStateMachine:  DriverStartedState (when=-2ms what=131143 arg1=10009 arg2=682) CMD_START_SCAN 10009 682 ic=0 proc(ms):2 dur:692 cnt=2252 rssi=-64 f=-1 sc=0 link=-1 tx=0,0, 0,0, 0,0  rx=0,0 fiv=20000 [on:0 tx:0 rx:0 period:1] from screen [on:0 period:-131266636]
05-28 19:23:01.300 731-939/? E/WifiStateMachine: stopReconnectWifi Wfd=false StopScan=false mDontReconnectAndScan=false
05-28 19:23:01.302 731-939/? E/WifiStateMachine: [1.464.452.581.302 ms] noteScanStartWorkSource{10009} uid 10009
05-28 19:23:01.304 731-10206/? E/WifiMonitor: handleEvent unknown: 15  CTRL-EVENT-SCAN-STARTED 
05-28 19:23:01.322 1716-1792/? E/CellLocation: create GsmCellLocation
05-28 19:23:01.358 1716-1792/? E/CellLocation: create GsmCellLocation
05-28 19:23:01.415 1716-25019/? E/MPlugin: Unsupported class: com.mediatek.common.telephony.IOnlyOwnerSimSupport

MapsActivity:

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    HttpURLConnection urlConnection= null;
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        new MarkerTask().execute();
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.getUiSettings().isCompassEnabled();
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setScrollGesturesEnabled(true);
        mMap.getUiSettings().setMapToolbarEnabled(true);


    }

    class MarkerTask extends AsyncTask<Void, String, Void> {



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {

            InputStream is = null;
            String result="";
            try {
                URL url = new URL("http://catcat.netne.net/get_mark.php");
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
                is = urlConnection.getInputStream();
//2 sec, tel
            } catch (Exception e) {
                Log.e("URL connection", e.toString());
            }
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String line= null;
                while( (line= reader.readLine())!=null){
                    result +=line;
                }
                is.close();
            }
            catch (Exception e){
                e.printStackTrace();
            }
            try{
                JSONArray jsonArray = new JSONArray(result);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = jsonArray.getJSONObject(i);

                    LatLng latLng = new LatLng(jsonObj.getJSONArray("latlng").getDouble(0),
                            jsonObj.getJSONArray("latlng").getDouble(1));


                    // Create a marker for each station in the JSON data.
                    mMap.addMarker(new MarkerOptions()
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                            .title(jsonObj.getString("nume"))
                            .position(latLng));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


            return null;

        }
            // Executed after the complete execution of doInBackground() method
            @Override
            protected void onPostExecute (Void result){


            }

    }
}

php file:

<?php



@$dbhandle = mysql_connect($host,$user,$pass)or die("Unable to connect to MySQL");

$selected = mysql_select_db($db,$dbhandle)or die("Could not select licenta");


    $sql="SELECT id_st, nume, CONCAT_WS(\",\",lat, lng) AS latlng FROM statii";
    $result = mysql_query($sql);
    $response= array();
    while($row= mysql_fetch_assoc($result)){

        $response[]=array(
                            'nume'          => $row['nume'],
                            'latlng'        => explode(',', $row['latlng']),
                            'id_st'         => $row['id_st']
                            );
    }

//$bd_json = json_encode($response);
echo json_encode($response);
exit();

?>

Json response:

[{
    "nume": "Merge in sfarsit",
    "latlng": ["45.659721", "25.606859"],
    "id_st": "5"
}, {
    "nume": "Memo Cantina",
    "latlng": ["45.655075", "25.581560"],
    "id_st": "6"
}]

One issue that I see is that in the code below you are adding a marker to the Google Map (on UI thread) from the background thread:

// Create a marker for each station in the JSON data.
                mMap.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                        .title(jsonObj.getString("nume"))
                        .position(latLng));

Either create an entire set of markers built in the loop first, maybe in a list, and then pass the list to the onPostExecute at the end where you would add them to the map there, or post each marker using the onProgressUpdate method like:

// Create a marker for each station in the JSON data.
mMarker = new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                        .title(jsonObj.getString("nume"))
                        .position(latLng);
onProgressUpdate(mMarker);

Make sure to add:

MarkerOptions mMarker;

below this line:

private GoogleMap mMap;

And then override the OnProgressUpdate method:

protected void onProgressUpdate(MarkerOptions marker) {
     mMap.addMarker(marker);
 }

Don't forget to change

class MarkerTask extends AsyncTask<Void, String, Void> {

to

class MarkerTask extends AsyncTask<Void, MarkerOptions, Void> {

While this might help you get past this issue, you really should review the AsyncTask class and get yourself familiar it.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM