【问题标题】:Can a function be called from Oncreate which is in the same class?可以从同一类中的 Oncreate 调用函数吗?
【发布时间】: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

你能告诉我这个错误的原因是什么吗?这样我就可以学习了

【问题讨论】:

    标签: android sqlite oncreate


    【解决方案1】:

    您正在使用new Catagories() 创建一个新的活动实例,然后在那个 上调用getAllCatagories()。你不能像这样用new 实例化活动——它们不会被设置为Contexts。

    替换

    cat.getAllCatagories()
    

    只有

    getAllCatagories()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      相关资源
      最近更新 更多