我有一个在后台线程中获取 Google 地图坐标的活动,我想显示一个进度对话框以显示后台线程何时完成。
地图类
package com.example.androidbasic12;
public class map extends FragmentActivity{
private static LatLng SYDNEY = new LatLng(-33.88,151.21);
public static LatLng sala = new LatLng(59.91602, 16.594108);
int travel = 0;
int MapTypeInt = 0;
private GoogleMap mMap;
GoogleMapOptions options = new GoogleMapOptions();
String username;
String Lat1;
String Lng1;
Double lat;
Double lng;
ProgressDialog dialog = new ProgressDialog(this);
// JSON response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_NAME = "name";
private static String KEY_LAT = "lat";
private static String KEY_LNG = "lng";
String errorhandler = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
setUpMapIfNeeded();
UserFunctions userFunc = new UserFunctions();
ArrayList<String> cordarr = new ArrayList<String>();
cordarr = userFunc.getCoords(getApplicationContext(), Lat1, Lng1);
if(cordarr != null && cordarr.size()> 1 ){
Log.d("LOG", "I got lat long");
Lat1 = cordarr.get(0);
Lng1 = cordarr.get(1);
lat = goDouble(Lat1);
lng = goDouble(Lng1);
LatLng sala = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(sala).title("Coords: "
+ lat + ", "
+ lng));
options.mapType(GoogleMap.MAP_TYPE_NORMAL)
.compassEnabled(true)
.rotateGesturesEnabled(true)
.tiltGesturesEnabled(true);
} else {
Log.d("LOG", "no lat long found");
}
new getCord().execute();
//Button that sends the user to Sala by default, and to Sydney if Sala
Button btnSyd = (Button) findViewById(R.id.btnSyd);
btnSyd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stu
if (travel == 0){
travelToSala();
travel = 1;
} else if (travel == 1) {
travelToSydney();
travel = 0;
}
}
});
//Button for setting map type to satellite if normal, to normal if satellite
final ImageButton changeBtn =(ImageButton) findViewById(R.id.changeBtn);
changeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
changeMapType(changeBtn);
}
});
}
//method that changes maptype
private void changeMapType(ImageButton button){
if(MapTypeInt == 0){
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
button.setImageResource(R.drawable.normal_map);
MapTypeInt = 1;
} else if (MapTypeInt == 1) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
button.setImageResource(R.drawable.satellite);
MapTypeInt = 0;
}
}
//Function that travels user to Sydney
private void travelToSydney(){
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));
Button btnSyd = (Button) findViewById(R.id.btnSyd);
btnSyd.setText("travel to Sala");
}
//Function that travels user to Sala
private void travelToSala(){
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sala, 15));
Button btnSyd = (Button) findViewById(R.id.btnSyd);
btnSyd.setText("travel to Sydney!");
}
//Checks if map is setup, and if it isn't, set's up the map
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sala, 15));
if(mMap != null){
//The map is veriefied
}
}
}
class getCord extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
UserFunctions userFunction = new UserFunctions();
username = userFunction.getUsername(getApplicationContext(), "uname");
JSONObject json = userFunction.getCarcoord(username);
try {
if (json.getString(KEY_SUCCESS) != null){
String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res)==1){
//Successfully found users car coordinates
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_coord = json.getJSONObject("user");
//clear previous entries in table "TABLE_COORD"
userFunction.resetCarcoord(getApplicationContext());
db.addCoord(json_coord.getString(KEY_NAME), json_coord.getString(KEY_LAT), json_coord.getString(KEY_LNG));
errorhandler = "2";
}
} else if(json.getString(KEY_ERROR) != null) {
String res = json.getString(KEY_ERROR);
if(Integer.parseInt(res) == 1){
//Some error in getting registration, should not happen though
errorhandler = "1";
}
}
}catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setCancelable(true);
dialog.setMessage("Loading car coordinats");
dialog.show();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.cancel();
if(errorhandler == "1"){
Toast toast = Toast.makeText(getApplicationContext(),
"User has not got car", Toast.LENGTH_LONG);
toast.show();
errorhandler = "";
} else if (errorhandler == "2" ) {
// Error in login
Toast toast = Toast.makeText(getApplicationContext(),
"Successfully got coords for car", Toast.LENGTH_LONG);
toast.show();
errorhandler = "";
}
}
}
public Double goDouble(String Latlng) {
Double goDouble = Double.parseDouble(Latlng);
return goDouble;
}
} 错误信息
01-25 13:51:00.721: E/AndroidRuntime(7780): FATAL EXCEPTION: main
01-25 13:51:00.721: E/AndroidRuntime(7780): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidbasic12/com.example.androidbasic12.map}: java.lang.NullPointerException
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.os.Looper.loop(Looper.java:137)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-25 13:51:00.721: E/AndroidRuntime(7780): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 13:51:00.721: E/AndroidRuntime(7780): at java.lang.reflect.Method.invoke(Method.java:511)
01-25 13:51:00.721: E/AndroidRuntime(7780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-25 13:51:00.721: E/AndroidRuntime(7780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-25 13:51:00.721: E/AndroidRuntime(7780): at dalvik.system.NativeStart.main(Native Method)
01-25 13:51:00.721: E/AndroidRuntime(7780): Caused by: java.lang.NullPointerException
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:139)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.AlertDialog.<init>(AlertDialog.java:98)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
01-25 13:51:00.721: E/AndroidRuntime(7780): at com.example.androidbasic12.map.<init>(map.java:42)
01-25 13:51:00.721: E/AndroidRuntime(7780): at java.lang.Class.newInstanceImpl(Native Method)
01-25 13:51:00.721: E/AndroidRuntime(7780): at java.lang.Class.newInstance(Class.java:1319)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
01-25 13:51:00.721: E/AndroidRuntime(7780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
01-25 13:51:00.721: E/AndroidRuntime(7780): ... 11 more
任何人都可以看到问题是什么?