سؤال

I am trying to implement AutofillTextBox, My code is given below, here i am calling a function which returns a String to the OnCreate. But error occurs at the places which i showed in the comment. Actually is it wrong which i am doing now?

My code

/* 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();
}
}

Now the log cat is

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

Can you please tell me what is the cause of this error? So that i can learn

هل كانت مفيدة؟

المحلول

You're creating a new instance of your activity with new Catagories() and then invoke getAllCatagories() on that. You cannot instantiate activities with new like that - they will not be set up as Contexts.

Replace

cat.getAllCatagories()

with just

getAllCatagories()

to invoke the method on the current activity instance which is properly set up by the Android app framework.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top