3

我正在尝试为 goofle 地图开发一个自动完成位置。但是,我在开发过程中遇到了一些错误。我不知道错误的原因是什么。

下面是我的代码。

public class MainActivity extends Activity {
/** Called when the activity is first created. */
ArrayAdapter<String> adapter;
AutoCompleteTextView textView;
Object[] arg0 = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    adapter = new ArrayAdapter<String>(this, R.layout.item_list);
    textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    adapter.setNotifyOnChange(true);
    textView.setAdapter(adapter);
    textView.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (count % 3 == 1) {
                adapter.clear();
                // GetPlaces task = new GetPlaces();
                // now pass the argument in the textview to the task
                new GetPlaces().execute(textView.getText().toString());
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void afterTextChanged(Editable s) {

        }
    });
}

class GetPlaces extends AsyncTask<String, Void, ArrayList<String>> {

    @Override
    // three dots is java for an array of strings
    protected ArrayList<String> doInBackground(String... args) {

        Log.d("gottaGo", "doInBackground");

        ArrayList<String> predictionsArr = new ArrayList<String>();

        try {


            URL googlePlaces = new URL(
            // URLEncoder.encode(url,"UTF-8");
                    "https://maps.googleapis.com/maps/api/place/autocomplete/json?input="
                            + URLEncoder.encode(arg0[0].toString(), "UTF-8")
                            + "&types=geocode&language=en&sensor=true&key=<API-key here>"); 
            URLConnection tc = googlePlaces.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));

            String line;
            StringBuffer sb = new StringBuffer();
            // take Google's legible JSON and turn it into one big
            // string.
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            // turn that string into a JSON object
            JSONObject predictions = new JSONObject(sb.toString());
            // now get the JSON array that's inside that object
            JSONArray ja = new JSONArray(
                    predictions.getString("predictions"));

            for (int i = 0; i < ja.length(); i++) {
                JSONObject jo = (JSONObject) ja.get(i);
                // add each entry to our array
                predictionsArr.add(jo.getString("description"));
            }
        } catch (IOException e) {

            Log.e("YourApp", "GetPlaces : doInBackground", e);

        } catch (JSONException e) {

            Log.e("YourApp", "GetPlaces : doInBackground", e);

        }

        return predictionsArr;

    }

    // then our post

    @Override
    protected void onPostExecute(ArrayList<String> result) {

        Log.d("YourApp", "onPostExecute : " + result.size());
        // update the adapter
        adapter = new ArrayAdapter<String>(getBaseContext(),
                R.layout.item_list);
        adapter.setNotifyOnChange(true);
        // attach the adapter to textview
        textView.setAdapter(adapter);

        for (String string : result) {

            Log.d("YourApp", "onPostExecute : result = " + string);
            adapter.add(string);
            adapter.notifyDataSetChanged();

        }

        Log.d("YourApp",
                "onPostExecute : autoCompleteAdapter" + adapter.getCount());

    }

这是 logcat 错误:

10-24 15:10:54.609: E/AndroidRuntime(14346): FATAL EXCEPTION: AsyncTask #1
10-24 15:10:54.609: E/AndroidRuntime(14346): java.lang.RuntimeException: An error occured while executing doInBackground()
10-24 15:10:54.609: E/AndroidRuntime(14346):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at java.lang.Thread.run(Thread.java:856)
10-24 15:10:54.609: E/AndroidRuntime(14346): Caused by: java.lang.NullPointerException
10-24 15:10:54.609: E/AndroidRuntime(14346):    at com.test.main.MainActivity$GetPlaces.doInBackground(MainActivity.java:76)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at com.test.main.MainActivity$GetPlaces.doInBackground(MainActivity.java:1)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-24 15:10:54.609: E/AndroidRuntime(14346):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
4

2 回答 2

0

在第 76 行.. 使用“URLEncoder.encode(args[0].toString()”而不是“URLEncoder.encode(arg0[0].toString()”

于 2013-05-03T06:29:55.003 回答
0

错误的根源显然在 MainActivity 第 76 行,您正在访问一个空对象

于 2012-10-25T05:12:04.290 回答