gpt4 book ai didi

java - E/AndroidRuntime : FATAL EXCEPTION: AsyncTask #1 java. lang.RuntimeException:执行 doInBackground() 时发生错误

转载 作者:行者123 更新时间:2023-12-01 09:52:50 27 4
gpt4 key购买 nike

我是编程初学者,我只想创建一个应用程序,使用数据库(由 000webhost 托管)中的一些坐标,使用 PHP 和 JSON 在 map 上显示一些标记。 map 正在工作,但没有标记。请帮助我,因为我真的无法弄清楚。

这是我收到的错误消息:

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

map Activity :

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 文件:

<?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 响应:

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

最佳答案

我看到的一个问题是,在下面的代码中,您从后台线程向 Google map (在 UI 线程上)添加一个标记:

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

首先在循环中创建一整套标记(可能在列表中),然后将列表传递到 onPostExecute 最后将它们添加到那里的 map ,或者使用 onProgressUpdate 方法发布每个标记像:

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

请务必添加:

MarkerOptions mMarker;

此行下方:

private GoogleMap mMap;

然后重写OnProgressUpdate方法:

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

不要忘记改变

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

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

虽然这可能会帮助您解决这个问题,但您确实应该查看 AsyncTask 类并熟悉它。

祝你好运!

关于java - E/AndroidRuntime : FATAL EXCEPTION: AsyncTask #1 java. lang.RuntimeException:执行 doInBackground() 时发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37501410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com