我的代码工作正常但是因为我实现了asynctask它显示以下错误。实际上我想显示进度条直到map加载这就是为什么putt所有代码都在asynctask
Java文件
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.database.CursorJoiner.Result;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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;
@SuppressLint("NewApi")
public class Tab_location extends FragmentActivity {
private GoogleMap mMap;
private ProgressBar myprogress;
Thread load;
int waittym= 1000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_location);
progress A1 = new progress();
A1.execute();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
progress A1 = new progress();
A1.execute();
}
public void loadMap( )
{
if(mMap!=null)
{
return;
}
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
//mMap.addMarker( new MarkerOptions().position(new LatLng( 28.585726000000000000, 77.311042000000040000)).title("Noida Sec 15").icon(BitmapDescriptorFactory.defaultMarker()));
if(mMap==null)
{
return;
}
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
mMap.setMyLocationEnabled(true);
}
public class progress extends AsyncTask<Void, Void, Void> implements LocationListener{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
loadMap();
Log.i("after","Loadmap");
LocationManager locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria= new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location=locationManager.getLastKnownLocation(provider);
if(location!=null)
{
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0,this);
return null;
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
myprogress =(ProgressBar)findViewById(R.id.progressBar1);
myprogress.setVisibility(View.VISIBLE);
super.onPreExecute();
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
myprogress.setVisibility(View.GONE);
super.onPostExecute(result);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng= new LatLng(latitude, longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14),2000,null);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
}
LOGCAT ERROR
09-08 03:01:30.232: E/AndroidRuntime(2536): FATAL EXCEPTION: AsyncTask #1
09-08 03:01:30.232: E/AndroidRuntime(2536): java.lang.RuntimeException: An error occured while executing doInBackground()
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-08 03:01:30.232: E/AndroidRuntime(2536): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-08 03:01:30.232: E/AndroidRuntime(2536): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-08 03:01:30.232: E/AndroidRuntime(2536): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-08 03:01:30.232: E/AndroidRuntime(2536): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-08 03:01:30.232: E/AndroidRuntime(2536): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-08 03:01:30.232: E/AndroidRuntime(2536): at java.lang.Thread.run(Thread.java:841)
09-08 03:01:30.232: E/AndroidRuntime(2536): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.os.Handler.<init>(Handler.java:197)
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.os.Handler.<init>(Handler.java:111)
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:197)
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:197)
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.location.LocationManager.wrapListener(LocationManager.java:820)
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:833)
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:430)
09-08 03:01:30.232: E/AndroidRuntime(2536): at com.example.tatadocomowi_fi.Tab_location$progress.doInBackground(Tab_location.java:84)
09-08 03:01:30.232: E/AndroidRuntime(2536): at com.example.tatadocomowi_fi.Tab_location$progress.doInBackground(Tab_location.java:1)
09-08 03:01:30.232: E/AndroidRuntime(2536): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-08 03:01:30.232: E/AndroidRuntime(2536): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-08 03:01:30.232: E/AndroidRuntime(2536): ... 4 more
答案 0 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
public class Tab_location extends FragmentActivity implements LocationListener {
private GoogleMap mMap;
private ProgressBar myprogress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_location);
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
mMap.setMyLocationEnabled(true);
LocationManager locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria= new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location=locationManager.getLastKnownLocation(provider);
if(location!=null)
{
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0,this);
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14),2000,null);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
答案 1 :(得分:0)
根据您的请求,此处更改了代码以使用进度条 它可能有矿工错误,因为它没有编译,只是移动代码
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.database.CursorJoiner.Result;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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;
@SuppressLint("NewApi")
public class Tab_location extends FragmentActivity {
private GoogleMap mMap;
private ProgressBar myprogress;
// Thread load;
// int waittym= 1000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_location);
progress A1 = new progress();
myprogress = (ProgressBar) findViewById(R.id.progressBar1);
myprogress.setVisibility(View.VISIBLE);
loadMap();
registerLocationListener();
}
private boolean registerLocationListener() {
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());
Log.i("after", "Loadmap");
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
return true;
myprogress.setVisibility(View.GONE);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
progress A1 = new progress();
A1.execute();
}
public void loadMap() {
if (mMap != null) {
return;
}
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// mMap.addMarker( new MarkerOptions().position(new LatLng(
// 28.585726000000000000,
// 77.311042000000040000)).title("Noida Sec 15").icon(BitmapDescriptorFactory.defaultMarker()));
if (mMap == null) {
return;
}
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
mMap.setMyLocationEnabled(true);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}