我不知道如何解决这个问题,请帮助。我唯一能说的是,如果我注释掉setListAdapter(cursorAdapter);
,代码会运行,但我的ListView
. 可能有用的代码的任何其他部分让我知道。谢谢您的帮助。
08-06 03:21:50.435: E/AndroidRuntime(489): Uncaught handler: thread main exiting due to uncaught exception
08-06 03:21:50.565: E/AndroidRuntime(489): java.lang.ArrayIndexOutOfBoundsException
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:157)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.ListView.onMeasure(ListView.java:1060)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.View.measure(View.java:7964)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.View.measure(View.java:7964)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.View.measure(View.java:7964)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.View.measure(View.java:7964)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.View.measure(View.java:7964)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.os.Looper.loop(Looper.java:123)
08-06 03:21:50.565: E/AndroidRuntime(489): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-06 03:21:50.565: E/AndroidRuntime(489): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 03:21:50.565: E/AndroidRuntime(489): at java.lang.reflect.Method.invoke(Method.java:521)
08-06 03:21:50.565: E/AndroidRuntime(489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-06 03:21:50.565: E/AndroidRuntime(489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-06 03:21:50.565: E/AndroidRuntime(489): at dalvik.system.NativeStart.main(Native Method)
我的活动:
public class SearchActivity extends ListActivity {
final String APP_TAG = "RouteTracker";
boolean APP_DEBUG = true;
int searchType;
DbAdapter dbAdapter;
Context context = this;
@Override
public void onResume(){
dbAdapter.open();
super.onResume();
}
@Override
public void onPause(){
dbAdapter.close();
super.onPause();
}
@Override
public void onCreate(Bundle savedInstanceBundle){
super.onCreate(savedInstanceBundle);
if (APP_DEBUG) {Log.d(APP_TAG, "Inside onCreate for SearchActivity");}
dbAdapter = new DbAdapter(this);
setContentView(R.layout.search_activity);
Bundle searchActivityBundle = getIntent().getExtras();
searchType = searchActivityBundle.getInt("SearchType");
TextView titleLabel = (TextView) findViewById(R.id.search_activity_title_label);
titleLabel.setText(getString(R.string.search_activity_default_title_label));
final EditText search_criteria_edittext = (EditText) findViewById(R.id.search_activity_search_criteria_edittext);
Button search_button = (Button) findViewById(R.id.search_activity_search_button);
search_button.setText(getString(R.string.search_activity_search_button));
//ListView searchResultsList; // = (ListView) findViewById(android.R.id.list);
//searchResultsList = getListView();
switch (searchType) {
case RouteTrackerGlobal.SEARCH_NAME:
search_criteria_edittext.setHint(getString(R.string.search_activity_textview_company_name_hint));
break;
case RouteTrackerGlobal.SEARCH_STREET:
search_criteria_edittext.setHint(getString(R.string.search_activity_textview_street_hint));
break;
case RouteTrackerGlobal.SEARCH_CITY:
search_criteria_edittext.setHint(getString(R.string.search_activity_textview_city_hint));
break;
case RouteTrackerGlobal.SEARCH_STATE:
search_criteria_edittext.setHint(getString(R.string.search_activity_textview_state_hint));
break;
case RouteTrackerGlobal.SEARCH_ZIP:
search_criteria_edittext.setHint(getString(R.string.search_activity_textview_zip_hint));
break;
}
search_button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
String searchCriteria = search_criteria_edittext.getText().toString();
if (searchCriteria.trim().equals("")) {
Toast.makeText(getBaseContext(), "No data entered, try again",Toast.LENGTH_SHORT).show();
return;
}
String[] columns = new String[] {};
int[] to = new int[] {R.id.search_activity_details_search_results, R.id.search_activity_details_company_name};
Cursor dataCursor;
switch (searchType) {
case RouteTrackerGlobal.SEARCH_NAME:
dataCursor = dbAdapter.search(searchType, searchCriteria);
columns= new String[] {"CompanyName"};
break;
case RouteTrackerGlobal.SEARCH_STREET:
dataCursor = dbAdapter.search(searchType, searchCriteria);
columns = new String[] {"AddressStreet","CompanyName"};
break;
case RouteTrackerGlobal.SEARCH_CITY:
dataCursor = dbAdapter.search(searchType, searchCriteria);
columns = new String[] {"AddressCity", "CompanyName"};
break;
case RouteTrackerGlobal.SEARCH_STATE:
dataCursor = dbAdapter.search(searchType, searchCriteria);
columns = new String[] {"AddressState", "CompanyName"};
break;
case RouteTrackerGlobal.SEARCH_ZIP:
dataCursor = dbAdapter.search(searchType, searchCriteria);
columns = new String[] {"AddressZip", "CompanyName"};
break;
default:
dataCursor = dbAdapter.search(RouteTrackerGlobal.SEARCH_NAME, searchCriteria);
columns = new String[] {"", "CompanyName"};
break;
}
startManagingCursor(dataCursor);
if (dataCursor == null) {
Toast.makeText(getBaseContext(), "Something happened that failed", Toast.LENGTH_SHORT).show();
setListAdapter(null);
return;
}
if (dataCursor.getCount() == 0) {
Toast.makeText(getBaseContext(), getString(R.string.search_activity_no_results), Toast.LENGTH_SHORT).show();
setListAdapter(null);
return;
}
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, R.layout.search_activity_details, dataCursor, columns, to);
setListAdapter(cursorAdapter);
//Toast.makeText(getBaseContext(), dataCursor.getColumnName(1), Toast.LENGTH_SHORT).show();
}});
我加载了 android 2.1 源代码并且能够找到它失败的地方,但我仍然不明白它。
它在 bindView 内的 SimpleCursorAdapter 类中失败。下面是 bindView,它在 String text = cursor.getString(from[i]);中失败了
public void bindView(View view, Context context, Cursor cursor) {
final View[] holder = mHolders.get(view);
final ViewBinder binder = mViewBinder;
final int count = mTo.length;
final int[] from = mFrom;
for (int i = 0; i < count; i++) {
final View v = holder[i];
if (v != null) {
boolean bound = false;
if (binder != null) {
bound = binder.setViewValue(v, cursor, from[i]);
}
if (!bound) {
**String text = cursor.getString(from[i]);**
if (text == null) {
text = "";
}
if (v instanceof TextView) {
setViewText((TextView) v, text);
} else if (v instanceof ImageView) {
setViewImage((ImageView) v, text);
} else {
throw new IllegalStateException(v.getClass().getName() + " is not a " +
" view that can be bounds by this SimpleCursorAdapter");
}
}
}
}
}