我可以知道如何解决这个问题? 我得到致命异常:当我按下计算按钮时,Asynctask#1 然后我的应用程序马上崩溃了。 我错过了asynctask的东西吗? 这是我的mainactivity.java代码
package com.ccsy.mtravel;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText StartAdd;
private EditText EndAdd;
private Button calculate_Btn;
private TextView totaldistance;
private TextView totalamount;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartAdd = (EditText) findViewById(R.id.StartAdd);
EndAdd = (EditText) findViewById(R.id.EndAdd);
totaldistance = (TextView) findViewById(R.id.totaldistance);
totalamount = (TextView) findViewById(R.id.totalamount);
calculate_Btn = (Button) findViewById(R.id.calculate_Btn);
calculate_Btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new SearchTask().execute(new String[] {StartAdd.getText().toString(), EndAdd.getText().toString()});
}
});
if (!Geocoder.isPresent()) {
calculate_Btn.setEnabled(false);
Toast.makeText(this, "Geocoder methods getFromLocation and getFromLocationName are not implemented", Toast.LENGTH_LONG).show();
}
}
private class SearchTask extends AsyncTask<String, Void, List<Address>> {
@Override
protected List<Address> doInBackground(String... params) {
List<Address> addresses = null;
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
Address StartAdd = geocoder.getFromLocationName(params[0], 1).get(0);
Address EndAdd = geocoder.getFromLocationName(params[1], 1).get(0);
double StartAdd_Latitude = StartAdd.getLatitude();
double StartAdd_Longitude = StartAdd.getLongitude();
double EndAdd_Latitude = EndAdd.getLatitude();
double EndAdd_Longitude = EndAdd.getLongitude();
Location StartLocation = new Location("Starting_Point");
StartLocation.setLatitude(StartAdd_Latitude);
StartLocation.setLongitude(StartAdd_Longitude);
Location EndLocation = new Location("Ending_Point");
EndLocation.setLatitude(EndAdd_Latitude);
EndLocation.setLongitude(EndAdd_Longitude);
float distance = StartLocation.distanceTo(EndLocation);
totaldistance.setText(String.valueOf(distance));
double amount = distance * 1;
totalamount.setText(String.valueOf(amount));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return addresses;
}
}
}
这是我的日志猫
12-03 23:04:08.196: W/dalvikvm(7142): threadid=12: thread exiting with uncaught exception (group=0x41960700)
12-03 23:04:08.206: E/AndroidRuntime(7142): FATAL EXCEPTION: AsyncTask #1
12-03 23:04:08.206: E/AndroidRuntime(7142): java.lang.RuntimeException: An error occured while executing doInBackground()
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-03 23:04:08.206: E/AndroidRuntime(7142): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-03 23:04:08.206: E/AndroidRuntime(7142): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-03 23:04:08.206: E/AndroidRuntime(7142): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-03 23:04:08.206: E/AndroidRuntime(7142): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-03 23:04:08.206: E/AndroidRuntime(7142): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-03 23:04:08.206: E/AndroidRuntime(7142): at java.lang.Thread.run(Thread.java:841)
12-03 23:04:08.206: E/AndroidRuntime(7142): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6804)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1077)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.view.View.requestLayout(View.java:16775)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.view.View.requestLayout(View.java:16775)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.view.View.requestLayout(View.java:16775)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.view.View.requestLayout(View.java:16775)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:358)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.view.View.requestLayout(View.java:16775)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.widget.TextView.checkForRelayout(TextView.java:7646)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.widget.TextView.setText(TextView.java:4446)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.widget.TextView.setText(TextView.java:4283)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.widget.TextView.setText(TextView.java:4258)
12-03 23:04:08.206: E/AndroidRuntime(7142): at com.ccsy.mtravel.MainActivity$SearchTask.doInBackground(MainActivity.java:78)
12-03 23:04:08.206: E/AndroidRuntime(7142): at com.ccsy.mtravel.MainActivity$SearchTask.doInBackground(MainActivity.java:1)
12-03 23:04:08.206: E/AndroidRuntime(7142): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-03 23:04:08.206: E/AndroidRuntime(7142): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-03 23:04:08.206: E/AndroidRuntime(7142): ... 4 more
12-03 23:04:08.546: W/dalvikvm(7142): threadid=13: thread exiting with uncaught exception (group=0x41960700)
12-03 23:04:08.546: I/Process(7142): Sending signal. PID: 7142 SIG: 9
感谢您的帮助:D
答案 0 :(得分:4)
只有创建视图层次结构的原始线程才能触及其视图。
您无法在doInBackground
方法中修改用户界面/视图。使用onPostExecute
或onProgressUpdate
或某种处理程序......
请阅读the AsyncTask documentation,尤其是主题 4个步骤,以了解将在UI线程上调用哪种方法。