MainActivity.java
AutoCompleteTextView location, destination;
public Places place1, place2;
Button calculate;
TextView result_a, result_b;
String result_main;
//default values for taxi fare
int basePrice = 10;
double priceMeter = 5.80;
double fare;
int parseFare;
int roundedValue;
String textvalue;
String actual_distance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location=(AutoCompleteTextView)findViewById(R.id.locale);
location.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
location.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// TODO Auto-generated method stub
place1 = (Places) adapterView.getItemAtPosition(position);
location.setText(place1.description);
//location.clearFocus();
Log.d("AutoCompleteTask", location.getText().toString());
}
});
destination = (AutoCompleteTextView)findViewById(R.id.destination);
destination.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
destination.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// TODO Auto-generated method stub
place2 = (Places) adapterView.getItemAtPosition(position);
destination.setText(place2.description);
//destination.clearFocus();
Log.d("AutoCompleteTask2", destination.getText().toString());
}
});
calculate = (Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new ReadDistanceJSONFeedTask().execute("http://maps.googleapis.com/maps/api/distancematrix/json?origins="+location.getText().toString()+"&destinations="+destination.getText().toString()+"&sensor=false&units=metric&mode=driving");
//result_a.setText("ROUTE A "+result_main +"\n"+ "The estimated fare is Rs."+textvalue);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class ReadDistanceJSONFeedTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... urls) {
// TODO Auto-generated method stub
return readJSONFeed(urls[0]);
}
@Override
protected void onPostExecute(String result)
{
try
{ //Accessing the JSON Results
JSONObject jsonObject = new JSONObject(result);
JSONArray rows = jsonObject.getJSONArray("rows");
Log.i("json", rows.toString());
JSONObject obj = rows.getJSONObject(0);
JSONArray elements = obj.getJSONArray("elements");
JSONObject ini_dis = elements.getJSONObject(0);
JSONObject distance = ini_dis.getJSONObject("distance");
Log.i("json", distance.toString());
//Distance to send
actual_distance = distance.getString("text");
//Calculation part
String solve = distance.getString("value");
int parseFare = Integer.parseInt(solve);
fare = basePrice + ((parseFare/200)*priceMeter);
roundedValue = (int)Math.round(fare);
//another activity
Intent i = new Intent(MainActivity.this,ResultsDisplay.class);
i.putExtra("Display",actual_distance);
i.putExtra("Display_b",textvalue);
startActivity(i);
}
catch(Exception e)
{
Log.d("ReadDistanceJSONFeedTask", e.getLocalizedMessage());
}
}
public String readJSONFeed(String URL){
//Initial steps to getting the JSON Result
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try
{
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
//statusCodes at 200 are OK_Statuses
if(statusCode==200)
{
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line=reader.readLine())!=null)
{
stringBuilder.append(line);
}
inputStream.close();
}
else
{
Log.d("readJSONFeed","Failed to download file");
}
}
catch(Exception e)
{
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
}
場所と目的地のユーザー入力を受け取り、パス間の距離と推定タクシー料金を返すアプリを作成しています。Google-Places
また、このプログラムには、 APIを利用したオートコンプリート テキスト ビュー機能があります。このアプリケーションは、Direction-Matrix-API
現在問題が発生している を使用していAsyncTask
ます...「doInBackGround()」メソッドで実行時に logcat がエラーを生成しています...助けてください。
LogCat エラー
03-27 09:29:03.785: 私/Choreographer(3468): 123 フレームスキップしました! アプリケーションがメイン スレッドで処理しすぎている可能性があります。03-27 09:29:05.485: W/dalvikvm(3468): threadid=12: キャッチされない例外で終了するスレッド (group=0x40a71930) 03-27 09:29:05.525: E/AndroidRuntime(3468): 致命的な例外: AsyncTask #2 03-27 09:29:05.525: E/AndroidRuntime(3468): java.lang.RuntimeException: doInBackground()の実行中にエラーが発生しました 03-27 09:29:05.525: E/AndroidRuntime(3468): at android .os.AsyncTask$3.done(AsyncTask.java:299) 03-27 09:29:05.525: E/AndroidRuntime(3468): java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 03-27 09:29:05.525: E/AndroidRuntime(3468): java.util.concurrent.FutureTask.setException(FutureTask.java:219) 03-27 09:29:05.525: E/AndroidRuntime(3468): java.util で. http://maps.googleapis.com/maps/api/distancematrix/json?origins=ラガンヘル Rd、パタン、中央地域、ネパール & 宛先 = タメル、カトマンズ、中央地域、ネパール & センサー = false & 単位 = メトリック & モード = 運転 03-27 09:29:05.525: E/AndroidRuntime(3468): java.net.URI.create(URI. java:727) 03-27 09:29:05.525: E/AndroidRuntime(3468): org.apache.http.client.methods.HttpGet.(HttpGet.java:75) 03-27 09:29:05.525: E /AndroidRuntime(3468): com.example.calculator_taxi_fare.MainActivity$ReadDistanceJSONFeedTask.readJSONFeed(MainActivity.java:159) 03-27 09:29:05.525: E/AndroidRuntime(3468): com.example.calculator_taxi_fare.MainActivity$ でReadDistanceJSONFeedTask.doInBackground(MainActivity.java:111) 03-27 09:29:05.525: E/AndroidRuntime(3468): com.example.calculator_taxi_fare.MainActivity$ReadDistanceJSONFeedTask.doInBackground(MainActivity.java:1) 03-27 09: 29:05.525:E/AndroidRuntime(3468): android.os.AsyncTask$2.call(AsyncTask.java:287) 03-27 09:29:05.525: E/AndroidRuntime(3468): java.util.concurrent.FutureTask.run( FutureTask.java:234) 03-27 09:29:05.525: E/AndroidRuntime(3468): ... 4 もっと見る