我使用以下代码获得给定的异常。虽然我将相同的资源ID传递给适配器,但为什么我会得到给定的异常?
代码:
public class MainActivity extends Activity {
private static final String LOG_TAG = "ExampleApp";
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "mykey";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
PlacesAutoCompleteAdapter adapter = new PlacesAutoCompleteAdapter(this, R.id.autoCompleteTextView1);
textView.setAdapter(adapter);
}
private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?sensor=false&key=" + API_KEY);
sb.append("&components=country:uk");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<String>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}
return resultList;
}
class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> resultList;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
}
}
例外:
02-07 23:28:23.390: W/IInputConnectionWrapper(28967): showStatusIcon on inactive InputConnection
02-07 23:28:38.520: E/Trace(29242): error opening trace file: No such file or directory (2)
02-07 23:28:38.530: D/ActivityThread(29242): setTargetHeapUtilization:0.25
02-07 23:28:38.530: D/ActivityThread(29242): setTargetHeapIdealFree:8388608
02-07 23:28:38.530: D/ActivityThread(29242): setTargetHeapConcurrentStart:2097152
02-07 23:28:38.700: D/libEGL(29242): loaded /system/lib/egl/libEGL_adreno200.so
02-07 23:28:38.700: D/libEGL(29242): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
02-07 23:28:38.700: D/libEGL(29242): loaded /system/lib/egl/libGLESv2_adreno200.so
02-07 23:28:38.710: I/Adreno200-EGL(29242): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: (Merge)
02-07 23:28:38.710: I/Adreno200-EGL(29242): Build Date: 07/09/13 Tue
02-07 23:28:38.710: I/Adreno200-EGL(29242): Local Branch: AU_41
02-07 23:28:38.710: I/Adreno200-EGL(29242): Remote Branch:
02-07 23:28:38.710: I/Adreno200-EGL(29242): Local Patches:
02-07 23:28:38.710: I/Adreno200-EGL(29242): Reconstruct Branch:
02-07 23:28:38.740: D/OpenGLRenderer(29242): Enabling debug mode 0
02-07 23:28:49.860: D/AndroidRuntime(29242): Shutting down VM
02-07 23:28:49.860: W/dalvikvm(29242): threadid=1: thread exiting with uncaught exception (group=0x40d65378)
02-07 23:28:49.870: E/AndroidRuntime(29242): FATAL EXCEPTION: main
02-07 23:28:49.870: E/AndroidRuntime(29242): android.content.res.Resources$NotFoundException: Resource ID #0x7f070000 type #0x12 is not valid
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2108)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.content.res.Resources.getLayout(Resources.java:857)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.AbsListView.obtainView(AbsListView.java:2271)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1188)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1115)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.ListPopupWindow.show(ListPopupWindow.java:524)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1081)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:956)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:938)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.os.Looper.loop(Looper.java:213)
02-07 23:28:49.870: E/AndroidRuntime(29242): at android.app.ActivityThread.main(ActivityThread.java:4787)
02-07 23:28:49.870: E/AndroidRuntime(29242): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 23:28:49.870: E/AndroidRuntime(29242): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 23:28:49.870: E/AndroidRuntime(29242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-07 23:28:49.870: E/AndroidRuntime(29242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
02-07 23:28:49.870: E/AndroidRuntime(29242): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
打开您的/gen/R.java
文件,找到哪个元素的ID为#0x7f070000
。
我认为问题在于:
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
您的布局autoCompleteTextView1
中有一个元素activity_main.xml
该元素实际上是一种类型AutoCompleteTextView
?
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"