gpt4 book ai didi

java - 致命异常 : Async Task #1 with geocoder

转载 作者:行者123 更新时间:2023-11-29 03:27:19 25 4
gpt4 key购买 nike

我可以知道如何解决这个问题吗?当我按下计算按钮时,出现致命异常:Asynctask #1 the screen shot of the app然后我的应用程序立即崩溃了。我错过了异步任务的东西吗?这是我的 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

最佳答案

Only the original thread that created a view hierarchy can touch its views.

您不能在 doInBackground 方法中修改 UI/ View 。使用 onPostExecuteonProgressUpdate 或某种处理程序...

请阅读the AsyncTask documentation ,尤其是主题4 个步骤,以查看将在 UI 线程上调用哪个方法。

关于java - 致命异常 : Async Task #1 with geocoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20354746/

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