是否可以从同一类中的Oncreate调用函数?

时间:2014-03-18 10:26:16

标签: android sqlite oncreate

我正在尝试实现AutofillTextBox,下面给出了我的代码,这里我调用的函数将一个String返回给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

请告诉我这个错误的原因是什么?这样我才能学习

1 个答案:

答案 0 :(得分:1)

您正在使用new Catagories()创建活动的新实例,然后在 上调用getAllCatagories()。您无法使用new实例化活动 - 它们不会设置为Context

替换

cat.getAllCatagories()

只是

getAllCatagories()

在Android应用框架正确设置的当前活动实例上调用方法。