【发布时间】:2014-03-18 10:26:16
【问题描述】:
我正在尝试实现 AutofillTextBox,我的代码如下所示,这里我正在调用一个函数,该函数将字符串返回给 OnCreate。但错误发生在我在评论中显示的地方。其实我现在做的有错吗?
我的代码
/* packages and imports */
public class Catagories extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.catagory);
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocompleteCountry);
Catagories cat=new Catagories();
String[] catagories = cat.getAllCatagories();//here
// Print out the values to the log
//for(int i = 0; i < countries.length; i++)
//{
// Log.i(this.toString(), countries[i]);
// }
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, catagories);
textView.setAdapter(adapter);
}
public String[] getAllCatagories()
{
String[] str = null;
SQLiteDatabase db;
db=openOrCreateDatabase("Quote.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);//here
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
Cursor c=null;
try
{
String selectQueryCat = "SELECT lastaddress,bookdir FROM QuoteConf";
c= db.rawQuery(selectQueryCat,null);
}
catch(Exception e)
{
System.out.println(e);
}
if(c.getCount() >0)
{
str = new String[c.getCount()];
int i = 0;
try
{
while (c.moveToNext())
{
str[i] = c.getString(c.getColumnIndex("Catagory"));
i++;
}
}
catch(Exception e)
{
System.out.println(e);
str[i]="nothing";
}
finally
{
db.close();
}
return str;
}
else
{
return new String[] {};
}
}
public void onDestroy()
{
super.onDestroy();
}
}
现在日志猫是
03-18 10:11:46.341: E/AndroidRuntime(9121): Caused by: java.lang.NullPointerException
03-18 10:11:46.341: E/AndroidRuntime(9121): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:223)
03-18 10:11:46.341: E/AndroidRuntime(9121): at com.GB.quatzapp.Catagories.getAllCatagories(Catagories.java:48)
03-18 10:11:46.341: E/AndroidRuntime(9121): at com.GB.quatzapp.Catagories.onCreate(Catagories.java:28)
03-18 10:11:46.341: E/AndroidRuntime(9121): at android.app.Activity.performCreate(Activity.java:5104)
03-18 10:11:46.341: E/AndroidRuntime(9121): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-18 10:11:46.341: E/AndroidRuntime(9121): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-18 10:11:46.341: E/AndroidRuntime(9121): ... 11 more
你能告诉我这个错误的原因是什么吗?这样我就可以学习了
【问题讨论】: