0

databaseadapter クラスの関数からすべてのカテゴリを取得するコードは次のとおりです。

public List<CategoryClass> getAllCategory()
    {
        List<CategoryClass> getCategoryList = new ArrayList<CategoryClass>();
        dataAdapter = dataAdapter.openToWrite();
        Cursor cursor = db.rawQuery("SELECT * FROM " + tb_Name,null);
        if (cursor.moveToFirst()) {
            do {
                CategoryClass categoryInstance = new CategoryClass();
                categoryInstance.setCategory(cursor.getString(1));

                // Adding contact to list
                getCategoryList.add(categoryInstance);
                cursor.close();
            }
            while (cursor.moveToNext());
        }
        Log.d("retrieving all categories","count < 0");

        return getCategoryList;


    }

アクティビティを開始すると、リストビューに値を表示する必要があり、アプリがデータベースのすべての値をリストビューに表示する必要があります。コードは次のとおりです。

public class Categories extends Activity

{
    DatabaseAdapter dataAdapterInstance;

    @Override
    public void onCreate(Bundle savedInstanceState)

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categories_list);
        ListView list = (ListView) findViewById(R.id.listView);
        dataAdapterInstance = new DatabaseAdapter(this);
        dataAdapterInstance = dataAdapterInstance.openToWrite();
        List<CategoryClass> category = dataAdapterInstance.getAllCategory();

        for (CategoryClass cn : category) {

            cn.getCategory();
            category.add(cn);
            ArrayAdapter<CategoryClass> arrayAdapter = new ArrayAdapter<CategoryClass>(this,android.R.layout.simple_list_item_1,category);


            list.setAdapter(arrayAdapter);
            arrayAdapter.notifyDataSetChanged();

         }
}
}

しかし、ボタンをクリックして上記のコードを含むこのアクティビティを開こうとすると、null ポインター例外が発生します: pls は、この問題を解決する方法を教えてください。

06-11 20:46:09.531: W/System.err(17223): java.lang.NullPointerException
06-11 20:46:09.531: W/System.err(17223):    at com.example.todolist.DatabaseAdapter.getAllCategory(DatabaseAdapter.java:88)
06-11 20:46:09.531: W/System.err(17223):    at com.example.todolist.Categories$1.onClick(Categories.java:40)
06-11 20:46:09.531: W/System.err(17223):    at android.view.View.performClick(View.java:3558)
06-11 20:46:09.531: W/System.err(17223):    at android.view.View$PerformClick.run(View.java:14152)
06-11 20:46:09.531: W/System.err(17223):    at android.os.Handler.handleCallback(Handler.java:605)
06-11 20:46:09.531: W/System.err(17223):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 20:46:09.531: W/System.err(17223):    at android.os.Looper.loop(Looper.java:137)
06-11 20:46:09.531: W/System.err(17223):    at android.app.ActivityThread.main(ActivityThread.java:4514)
06-11 20:46:09.531: W/System.err(17223):    at java.lang.reflect.Method.invokeNative(Native Method)
06-11 20:46:09.531: W/System.err(17223):    at java.lang.reflect.Method.invoke(Method.java:511)
06-11 20:46:09.531: W/System.err(17223):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-11 20:46:09.531: W/System.err(17223):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-11 20:46:09.531: W/System.err(17223):    at dalvik.system.NativeStart.main(Native Method)
4

2 に答える 2

1
public List<CategoryClass> getAllCategory()
{
      List<CategoryClass> getCategoryList = new ArrayList<CategoryClass>();
      dataAdapter = dataAdapter.openToWrite();
      Cursor cursor = db.rawQuery("SELECT * FROM " + tb_Name,null);
      if(cursor!=null){
             cursor.moveToFirst(); 
             while(!cursor.isAfterLast()){
                 CategoryClass categoryInstance = new CategoryClass();
                 categoryInstance.setCategory(cursor.getString(1));

            // Adding contact to list
                 getCategoryList.add(categoryInstance);

                 cursor.moveToNext();
              }
           }

       Log.d("retrieving all categories","count < 0");

       return getCategoryList;
 }
于 2013-06-11T11:15:09.200 に答える