0

好吧,我在这里真的很困惑。这是正在发生的事情的一个小总结。

  1. 我通过 GPD 获取用户位置
  2. 在第一个 IF 语句中,我收集了“opzioni”和“图像”,我想将它们输入到我的适配器中,但我还需要第三个数组,我只能稍后在脚本中收集它的数据。

  3. 我在数据库中收集元素的坐标

  4. 运行返回用户和数据库元素之间的距离的 AsyncTask。
  5. DISTANCE 值放在距离数组内,循环进行五次。

现在我遇到的问题是在调用适配器之前收集了“opzioni”和“图像”,而我只能在每个 AsyncTask 之后访问距离数据。

我无法更新 ListView。

这是来源

package com.example.myapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;


//import com.example.nevianoapp.NevianoMapsV2.DownloadTask;
//import com.example.nevianoapp.NevianoMapsV2.ParserTask;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CategoryMenu extends Activity implements AdapterView.OnItemClickListener, LocationListener {

    ListView l;
    String[] opzioni;
    int[] images;
    String[] distance;
    String category;
    LatLng myPos;
    LatLng dest;
    String distanceAsync;
    int nextIndex = 0;
    NewAdapter adapter;
    List<List<HashMap<String, String>>> test;


    DatabaseHandler db = new DatabaseHandler(this);


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.category_menu_layout);

         // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location From GPS
        Location location = locationManager.getLastKnownLocation(provider);

        myPos = new LatLng(location.getLatitude(), location.getLongitude());


        Toast.makeText(this, String.valueOf(location.getLatitude())+ " " + String.valueOf(location.getLongitude()), Toast.LENGTH_SHORT).show();

        //for(int x = 1; x < 6; x = x+1) {
        if(getIntent().getIntExtra("str1", 0) == 0){
            opzioni = new String[5];
            images = new int[5];
            distance = new String[5];

            for (int x = 0; x < 5; x = x+1){
            opzioni[x] = db.getCultura(x+1, "cultura").getName();
            images[x] = (R.drawable.cultura);
            dest = new LatLng(db.getCultura(x+1, "cultura").getCoordLat(), db.getCultura(x+1, "cultura").getCoordLong());

            String url = getDirectionsUrl(myPos, dest);
            DownloadTask downloadTask = new DownloadTask();
            downloadTask.execute(url);

            }
        }
        if(getIntent().getIntExtra("str1", 0) == 1){
            opzioni = new String[13];
            images = new int[13];
            for (int x = 0; x < 13; x = x+1){
            opzioni[x] = db.getCultura(x+1, "ristoranti").getName();
            images[x] = (R.drawable.ristoranti);
            }
        }
        if(getIntent().getIntExtra("str1", 0) == 2){
            opzioni = new String[17];
            images = new int[17];
            for (int x = 0; x < 17; x = x+1){
            opzioni[x] = db.getCultura(x+1, "alberghi").getName();
            images[x] = (R.drawable.itinerari);
            }
        }


        l=(ListView) findViewById(R.id.listView2);

        NewAdapter adapter = new NewAdapter(this, opzioni, images, distance);
        l.setAdapter(adapter);
        l.setOnItemClickListener(this); 




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.category_menu, menu);
        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Intent i = new Intent(this, informazioni.class);
        category = getIntent().getStringExtra("category");
        i.putExtra("str1", arg2);
        i.putExtra("category", category);

        Toast.makeText(this, "hey " + arg2, Toast.LENGTH_SHORT).show();// TODO Auto-generated method stub

        startActivity(i);

    }

     private String getDirectionsUrl(LatLng origin,LatLng dest){

            // Origin of route
            String str_origin = "origin="+origin.latitude+","+origin.longitude;

            // Destination of route
            String str_dest = "destination="+dest.latitude+","+dest.longitude;

            // Sensor enabled
            String sensor = "sensor=false";

            // Building the parameters to the web service
            String parameters = str_origin+"&"+str_dest+"&"+sensor;

            // Output format
            String output = "json";

            // Building the url to the web service
            String url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;

            return url;
        }

        /** A method to download json data from url */
        private String downloadUrl(String strUrl) throws IOException{
            String data = "";
            InputStream iStream = null;
            HttpURLConnection urlConnection = null;
            try{
                URL url = new URL(strUrl);

                // Creating an http connection to communicate with url
                urlConnection = (HttpURLConnection) url.openConnection();

                // Connecting to url
                urlConnection.connect();

                // Reading data from url
                iStream = urlConnection.getInputStream();

                BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

                StringBuffer sb  = new StringBuffer();

                String line = "";
                while( ( line = br.readLine())  != null){
                    sb.append(line);
                }

                data = sb.toString();

                br.close();

            }catch(Exception e){
                Log.d("Exception while downloading url", e.toString());
            }finally{
                iStream.close();
                urlConnection.disconnect();
            }
            return data;
        }

        /** A class to download data from Google Directions URL */
        private class DownloadTask extends AsyncTask<String, Void, String>{

            // Downloading data in non-ui thread
            @Override
            protected String doInBackground(String... url) {

                // For storing data from web service
                String data = "";

                try{
                    // Fetching the data from web service
                    data = downloadUrl(url[0]);
                }catch(Exception e){
                    Log.d("Background Task",e.toString());
                }
                return data;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

                ParserTask parserTask = new ParserTask();

                // Invokes the thread for parsing the JSON data
                parserTask.execute(result);
            }
            // Executes in UI thread, after the execution of
            // doInBackground()

}

        private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{




            // Parsing the data in non-ui thread
            @Override
            protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

                JSONObject jObject;
                List<List<HashMap<String, String>>> routes = null;

                try{
                    jObject = new JSONObject(jsonData[0]);
                    DirectionsJSONParser parser = new DirectionsJSONParser();

                    // Starts parsing data
                    routes = parser.parse(jObject);
                }catch(Exception e){
                    e.printStackTrace();
                }
                return routes;
            }

            // Executes in UI thread, after the parsing process
            @Override
            protected void onPostExecute(List<List<HashMap<String, String>>> result) {
                List<HashMap<String, String>> path = result.get(0);
                HashMap<String, String> point = path.get(0);


                        distanceAsync = point.get("distance");
                        distance[nextIndex] = distanceAsync;
                        ++nextIndex;
                        test.clear();
                        test.addAll(result);
                        adapter.notifyDataSetChanged();
            }
        }

class NewAdapter extends ArrayAdapter<String>{
    Context context;
    int[] images;
    String[] title;
    String[] subTitle;

    NewAdapter(Context c, String[] opzioni, int[] imgs, String[] distanza){
        super(c, R.layout.single_row, R.id.descrizione, opzioni);
        this.context = c;
        this.images = imgs;
        this.title = opzioni;
        this.subTitle = distanza;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.single_row, parent, false);

        ImageView myImage = (ImageView) row.findViewById(R.id.imageView);
        TextView myOptions = (TextView) row.findViewById(R.id.descrizione);
        TextView mySubTitle = (TextView) row.findViewById(R.id.subTitle);

        myImage.setImageResource(images[position]);
        myOptions.setText(title[position]);
        mySubTitle.setText(subTitle[position]);


        return row;
    }




}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}


}

这是日志

10-15 17:40:47.677: E/AndroidRuntime(5237): FATAL EXCEPTION: main
10-15 17:40:47.677: E/AndroidRuntime(5237): java.lang.NullPointerException
10-15 17:40:47.677: E/AndroidRuntime(5237):     at com.example.nevianoapp.CategoryMenu$ParserTask.onPostExecute(CategoryMenu.java:298)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at com.example.nevianoapp.CategoryMenu$ParserTask.onPostExecute(CategoryMenu.java:1)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at android.os.Looper.loop(Looper.java:137)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at java.lang.reflect.Method.invoke(Method.java:525)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-15 17:40:47.677: E/AndroidRuntime(5237):     at dalvik.system.NativeStart.main(Native Method)

有没有人有同样的问题?我真的很挣扎

谢谢

4

3 回答 3

0

不知道你能不能解决这个问题。无论如何,今天我找到了一种方法来做你想做的事。

  1. 首先,您必须在接收 Activity 作为参数的 AsyncTask 中创建一个构造方法。在此构造函数中,您将使用接收到的 Activity 设置上下文。
  2. 在将调用 AsyncTask 的 Activity 中,您创建一个公共方法来更新视图中的数据。
  3. AsyncTask 类需要具有将调用 AsyncTask 的所有活动的特定实例(但您不必全部初始化它们);
  4. 在 doInBackground() 方法中,您必须使用执行后将使用的数据初始化 Object;
  5. 在 onPostExecute() 方法中,您必须验证哪个活动调用了任务,并将新数据设置到您的视图中。

看:

public class AnyActivity extends Activity {
    TextView txView;

    //onCreate...
    //onPause...
    //onResume...

    public updateData(Object object) {
    //update the data in the UI
    }
}


public class WSCallSoap extends AsyncTask<Object, Integer, Boolean> {

    AnyActivity anyActivity;
    Activity activity;
    DataModelClass dmc;
    private ProgressDialog dialog = null;

    public WSCallSoap (Activity activity){
        this.activity = activity;
        Context context = activity;
        dialog = new ProgressDialog(context);
    }

    public void onPreExecute(){
        this.dialog.setMessage("Sending");
        this.dialog.show();
    }

    public boolean doInBackground(Object... params){
        dmc = ... //initialize the object and do wherever you need
    }

    public void onPostExecute(boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (activity.class == AnyActivity.class) {
            anyActivity.updateData(dmc);
        }
    }

}
于 2013-10-18T23:37:19.087 回答
0

@Michael Corleone,几天前我遇到了类似的问题。问题是:当您调用 AsyncTask 时,您的主代码仍在运行。

为了“解决”这个问题,我通常使用两个活动和一个 AsyncTask 类。

所以,我的第一个活动调用 startActivityForResult()。

第二个活动将 ProgressDialog 和其他数据发送到我的 AsyncTask。然后,在 doInBackground() 执行后调用 onPostExecute(),我关闭 ProgressDialog 并将第二个 Activity 的 RESULT 设置为“OK”或“CANCELED”。在这两种情况下,我都会关闭 ProgressDialog 并完成()第二个活动。

第一个 Activity 收到第二个结果,我在需要的地方做任何事情。我希望它对你有帮助。

于 2013-10-15T17:02:03.123 回答
0

目前你在ParserTask课堂上有以下问题:

1.test在调用clear或添加元素到list之前忘记初始化List

2. 使用未初始化的实例,adapter因为您正在创建另一个同名的适配器实例onCreate

adapter = new NewAdapter(this, opzioni, images, distance); //<<
l.setAdapter(adapter);
l.setOnItemClickListener(this);
于 2013-10-15T16:56:53.340 回答