Java 片段内部类错误引发致命异常,该异常正在使用AsyncTask更新UI视图
下面是我使用内部类片段的代码。另外,我正在使用AsyncTask执行一些后台操作。它在执行时抛出一个致命的异常Java 片段内部类错误引发致命异常,该异常正在使用AsyncTask更新UI视图,java,android,android-asynctask,Java,Android,Android Asynctask,下面是我使用内部类片段的代码。另外,我正在使用AsyncTask执行一些后台操作。它在执行时抛出一个致命的异常 public class LocationFragment extends Fragment { // Progress Dialog private ProgressDialog pDialog; // JSON parser class JSONParser jsonParser = new JSONParser();
public class LocationFragment extends Fragment {
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//gps class
GPSTracker gps;
double latitude,longitude;
public TextView current_location;
private static final String BC_URL = "http://manfredinfotech.com/projects/workshop/get_location.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
public LocationFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// create class object
gps = new GPSTracker(getActivity());
// check if GPS enabled
View rootView = inflater.inflate(R.layout.fragment_location, container, false);
current_location = (TextView) rootView.findViewById(R.id.current_location);
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
// \n is for new line
Toast.makeText(getActivity(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
new GetLocation().execute();
}
return rootView;
}
class GetLocation extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Processing...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args){
int success;
try{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("lat",String.valueOf(latitude)));
params.add(new BasicNameValuePair("lon", String.valueOf(longitude)));
JSONObject json = jsonParser.makeHttpRequest(BC_URL, "POST", params);
System.out.println("JSON" + json);
// check your log for json response
Log.d("Here", json.toString());
success = json.getInt(TAG_SUCCESS);
if(success == 1){
if(pDialog.isShowing())pDialog.dismiss();
System.out.println(json.getString(TAG_MESSAGE));
current_location.setText(json.getString(TAG_MESSAGE));
//return json.getString(TAG_MESSAGE);
} else {
if(pDialog.isShowing())pDialog.dismiss();
current_location.setText("Not location found");
System.out.println("Failed!");
//return json.getString(TAG_MESSAGE);
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
请帮助您必须将后台任务中更新ui的部分移动到主线程。这里有一段简单的代码:
runOnUiThread(new Runnable() {
@Override
public void run() {
//Update your UI
}
});
或者您可以在Asynctask的
onPostExecute
中编写代码。您正在操作布局,即从后台线程查看当前位置文本(doInBackground)。这是不允许的,错误说明了这一点。对于操作视图元素,您应该使用onPreExecute或onPostExecute方法。无论何时success==1,错误都在doInBackground()
中,因为您试图从线程编辑textview当前位置。
因此,简单的解决方案是在GetLocation
类中重写onPostExecute()
方法,返回您想要的文本并在那里设置它。在doInBackground()中编辑此内容
并将此新方法添加到afterdoInBackground()
runOnUiThread(new Runnable() {
@Override
public void run() {
//Update your UI
}
});
if(success == 1){
if(pDialog.isShowing())pDialog.dismiss();
System.out.println(json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
} else {
if(pDialog.isShowing())pDialog.dismiss();
System.out.println("Failed!");
return "Not location found";
}
@Override
protected void onPostExecute(String result) {
if(result != null)
current_location.setText(result);
}