Android 可以从同一类中的Oncreate调用函数吗?
我正在尝试实现AutofillTextBox,下面给出了我的代码,在这里我调用了一个函数,该函数向OnCreate返回一个字符串。但是错误发生在我在评论中显示的地方。事实上,我现在所做的是错的吗 我的代码Android 可以从同一类中的Oncreate调用函数吗?,android,sqlite,oncreate,Android,Sqlite,Oncreate,我正在尝试实现AutofillTextBox,下面给出了我的代码,在这里我调用了一个函数,该函数向OnCreate返回一个字符串。但是错误发生在我在评论中显示的地方。事实上,我现在所做的是错的吗 我的代码 /* packages and imports */ public class Catagories extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCr
/* 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();
}
}
你能告诉我这个错误的原因吗?这样我就可以了解您正在使用
newcatagories()
创建活动的新实例,然后调用getAllCatagories()
。您不能像这样用new
实例化活动-它们不会被设置为Context
s
替换
cat.getAllCatagories()
仅仅
getAllCatagories()
调用由Android应用程序框架正确设置的当前活动实例上的方法
getAllCatagories()