-2
public class getLocationName extends Activity {
AutoCompleteTextView atvPlaces;
PlacesTask placesTask;
ParserTask parserTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getlocationname);
atvPlaces = (AutoCompleteTextView) findViewById(R.id.atv_places);
atvPlaces.setThreshold(1);
atvPlaces.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
AutoCompleteTextView a=(AutoCompleteTextView)view.findViewById(R.id.atv_places);
Log.d("TAG", a.getText().toString());
Intent i=new Intent(getApplicationContext(),Dashboard.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
}
});
atvPlaces.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
atvPlaces.showDropDown();
return false;
}
});
atvPlaces.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
placesTask = new PlacesTask();
placesTask.execute(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
/** 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;
}
// Fetches all places from GooglePlaces AutoComplete Web Service
private class PlacesTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... place) {
// For storing data from web service
String data = "";
// Obtain browser key from https://code.google.com/apis/console
String key = "key=sdhdjsdsddsldsdjs";
String input="";
try {
input = "input=" + URLEncoder.encode(place[0], "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// place type to be searched
String types = "types=geocode";
// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = input+"&"+types+"&"+sensor+"&"+key;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;
try{
// Fetching the data from web service in background
data = downloadUrl(url);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Creating ParserTask
parserTask = new ParserTask();
// Starting Parsing the JSON string returned by Web Service
parserTask.execute(result);
}
}
/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{
JSONObject jObject;
@Override
protected List<HashMap<String, String>> doInBackground(String... jsonData) {
List<HashMap<String, String>> places = null;
PlaceJSONParser placeJsonParser = new PlaceJSONParser();
try{
jObject = new JSONObject(jsonData[0]);
// Getting the parsed data as a List construct
places = placeJsonParser.parse(jObject);
}catch(Exception e){
Log.d("Exception",e.toString());
}
return places;
}
@Override
protected void onPostExecute(List<HashMap<String, String>> result) {
String[] from = new String[] { "description"};
int[] to = new int[] { android.R.id.text1 };
// Creating a SimpleAdapter for the AutoCompleteTextView
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);
// Setting the adapter
atvPlaces.setAdapter(adapter);
}
}
}
的logcat:的Android AutoCompleteTextview的OnClick
04-02 08:17:23.151: E/AndroidRuntime(3569): FATAL EXCEPTION: main
04-02 08:17:23.151: E/AndroidRuntime(3569): Process: com.example.remindme, PID: 3569
04-02 08:17:23.151: E/AndroidRuntime(3569): java.lang.NullPointerException
04-02 08:17:23.151: E/AndroidRuntime(3569): at com.example.remindme.getLocationName$1.onItemClick(getLocationName.java:53)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:902)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1192)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.widget.AbsListView$3.run(AbsListView.java:3638)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.os.Handler.handleCallback(Handler.java:733)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.os.Handler.dispatchMessage(Handler.java:95)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.os.Looper.loop(Looper.java:136)
04-02 08:17:23.151: E/AndroidRuntime(3569): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-02 08:17:23.151: E/AndroidRuntime(3569): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 08:17:23.151: E/AndroidRuntime(3569): at java.lang.reflect.Method.invoke(Method.java:515)
04-02 08:17:23.151: E/AndroidRuntime(3569): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-02 08:17:23.151: E/AndroidRuntime(3569): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-02 08:17:23.151: E/AndroidRuntime(3569): at dalvik.system.NativeStart.main(Native Method)
On Click事件我想點擊的項目,但它顯示錯誤