2013-01-03 70 views
1

因此,在此代码中,我试图根据通过我的showResults()传递的查询来检索游标,然后创建adapterloadermanager。我觉得问题是由于我的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); 
     } 
    }); 
} 
+0

哪一行,你在得到错误? – mango

+1

也发布您的logcat输出。 – Kanth

+0

再次,我不完全确定是否是导致问题的id和布局,即使我这样做,我也不知道如何将布局设置为simple_list_item_1,类似于您对id中的id所做的操作xml文件 –

回答

1

由于您的代码就行了list.setAdapter(mAdapter);崩溃,list的是,这是有道理的,给你一个空指针的唯一对象。

经过进一步的检查,很明显,您在最初申报后并未指定list。您需要添加这样的事情在onCreate()setContentView()后:(这是你如何访问它,如果你的对象有android:id="@android:id/list";如果你指定你自己的ID,使用R.id.your_id_here代替)

list = (ListView) findViewById(android.R.id.list); 

+0

如果我在showResults()中做了它会有所作为吗? –

+0

@SaiValluri不,除了一般情况下,应尽快分配成员变量,以便所有方法都可以使用它们。通过将它放在'onCreate()'中,当'handleIntent()'之前有其他需要'list'的时候,你不必再移动它。 – Eric

+0

@SaiValluri我明白你为什么这么问了,现在。这里似乎有点不对劲 - 你使用'android.R.id.list'没有'ListFragment'。你可以发布你的'activity_search_results.xml'文件吗? – Eric

0

这可能是你错误的位置:

mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, 
      null, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0); 

该适配器需要在现场wher传递到构造一个Cursor è您传递null的参数,如:

mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, 
      cursor, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0); 
+0

似乎并不能解决这个错误,但我知道把光标放在参数中是必要的。 –

+0

一旦我解决了空例外,我将检查这个,因为这肯定会在将来成为一个问题。 –

+0

谢谢。但看起来埃里克得到了正确的解决方案,不是吗? – mango

0

试着先取数据库读取您的查询之前:

db = new DatabaseTable(this); 
Cursor cursor = db.getReadableDatabase().getContactMatches(query, null); 
+0

我认为只有DBHelper实际上可以获得可读的数据库。 –

+0

但我在我的数据库类中有一个open()方法,我应该这样做然后关闭它? –

+0

你有类似的东西: public SqliteDatabase openDB(){ return this.getReadableDatabase(); } – ashokgelal