因此,在此代碼中,我試圖根據通過我的showResults()
傳遞的查詢來檢索遊標,然後創建adapter
和loadermanager
。我覺得問題是由於我的SimpleCursorAdapter
構造函數中的佈局和id導致的,因爲在創建佈局和標識時錯誤開始了。我使用Log和if語句來查看它是否爲null,並且沒有任何內容顯示在logcat上,因此必須使用光標來表示。NullPointerException當我運行此代碼
public class SearchResultsActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private ListView list;
private DatabaseTable db;
private SimpleCursorAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
db = new DatabaseTable(this);
handleIntent(getIntent());
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
db = new DatabaseTable(this);
Cursor cursor = db.getContactMatches(query, null);
list = (ListView)findViewById(android.R.id.list);
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
null, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0);
getLoaderManager().initLoader(0, null, this);
list.setAdapter(mAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent contactIntent = new Intent(getApplicationContext(), ContactActivity.class);
contactIntent.setData(getIntent().getData());
startActivity(contactIntent);
}
});
}
哪一行,你在得到錯誤? – mango
也發佈您的logcat輸出。 – Kanth
再次,我不完全確定是否是導致問題的id和佈局,即使我這樣做,我也不知道如何將佈局設置爲simple_list_item_1,類似於您對id中的id所做的操作xml文件 –