これは、DB から値を取得するための databaseAdapter のコードです。
public ArrayList<TaskClass> getAllTaskCategoryWise(String categoryName)
{
ArrayList<TaskClass> getTaskListofSelectedCategory = new ArrayList<TaskClass>();
Log.d("query print:","");
Cursor cursor = db.rawQuery("SELECT * FROM " + tb_Name_Task + " WHERE " + KEY_TASKCATEGORY + " = '"+ categoryName.trim()+ "'"+"" + " ORDER BY " + KEY_TASKDATE +"" + " ASC",null);
//Cursor cursor = db.rawQuery("SELECT * FROM " + tb_Name_Task,null);
if(cursor!= null)
{
Log.d("cursor info:","cursor values:"+cursor);
cursor.moveToFirst();
while(!cursor.isAfterLast())
{
TaskClass taskClassInstance1 = new TaskClass();
taskClassInstance1.setTask_id(cursor.getInt(0));
taskClassInstance1.setTitle(cursor.getString(1));
taskClassInstance1.setDescription(cursor.getString(2));
taskClassInstance1.setTaskCategory(cursor.getString(3));
taskClassInstance1.setTaskDate(cursor.getString(4));
taskClassInstance1.setStatus(cursor.getInt(5));
// Adding task class instance to list
getTaskListofSelectedCategory.add(taskClassInstance1);
Log.d("task list:","values:+ taskClassInstance.setTitle(cursor.getString(1))");
cursor.moveToNext();
}
}
else
{
Log.d("values of getAllTaskCatgeory","cursor null");
}
Log.d("retrieving all task for selected category","getAllTaskCategoryWise function"+ cursor);
cursor.close();
return getTaskListofSelectedCategory;
}
関数内でこの関数を呼び出すと、null ポインター例外が返され、カスタム arrayadapter:カスタム配列アダプター コードも使用されます。
public void getAllTaskForSelectedCategory(String categorySelection)
{
Log.d("message for function call","getAllTaskForSelectedCategory function called:"+categorySelection);
try
{dataBaseAdapterInstance1 = dataBaseAdapterInstance1.openToRead();
taskClassListinstance = dataBaseAdapterInstance1.getAllTaskCategoryWise(categorySelection);
Log.d("LISTVALUES OF TASK TABLE:","VALUES:" + taskClassListinstance);
listViewInstance.setAdapter(new CustomListAdapter(getApplicationContext(),taskClassListinstance));
new CustomListAdapter(this,taskClassListinstance).notifyDataSetChanged();
listViewInstance.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
TaskClass classinstance = (TaskClass) taskClassListinstance.get(position);
Toast.makeText(getApplicationContext(),"values:" + classinstance,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"id values:"+classinstance.getTask_id(),Toast.LENGTH_SHORT).show();
}
});
dataBaseAdapterInstance1.close();
}
catch (Exception e) {
// TODO: handle exception
Log.d("exception in getAllTaskForSelectedCategory","error");
e.printStackTrace();
}
}
この " taskClassListinstance = dataBaseAdapterInstance1.getAllTaskCategoryWise(categorySelection); " は null を返し、ここに logcat があります。
06-01 16:39:11.375: E/AndroidRuntime(14691): FATAL EXCEPTION: main
06-01 16:39:11.375: E/AndroidRuntime(14691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolist/com.example.todolist.TaskListCategoryWise}: java.lang.NullPointerException
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.os.Looper.loop(Looper.java:137)
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-01 16:39:11.375: E/AndroidRuntime(14691): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 16:39:11.375: E/AndroidRuntime(14691): at java.lang.reflect.Method.invoke(Method.java:511)
06-01 16:39:11.375: E/AndroidRuntime(14691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-01 16:39:11.375: E/AndroidRuntime(14691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-01 16:39:11.375: E/AndroidRuntime(14691): at dalvik.system.NativeStart.main(Native Method)
06-01 16:39:11.375: E/AndroidRuntime(14691): Caused by: java.lang.NullPointerException
06-01 16:39:11.375: E/AndroidRuntime(14691): at com.example.todolist.TaskListCategoryWise.onCreate(TaskListCategoryWise.java:80)
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.Activity.performCreate(Activity.java:5104)
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-01 16:39:11.375: E/AndroidRuntime(14691): ... 11 more
taskListCategoryWise.java の完全なコード:
public class TaskListCategoryWise extends Activity
{ String selectedValueofList;
DatabaseAdapter dataBaseAdapterInstance1;
ListView listViewInstance;
CustomListAdapter instanceCustomAdapter;
ArrayList<TaskClass> taskClassListinstance;
CheckBox checkBoxInstance;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tasklist_categorywise);
dataBaseAdapterInstance1 = new DatabaseAdapter(this);
Button refreshTaskButton = (Button)findViewById(R.id.refreshBtnForTask);
listViewInstance = (ListView)findViewById(R.id.listViewForTask);
checkBoxInstance = (CheckBox)findViewById(R.id.checkboxForStatus);
try
{
Intent intent1 = getIntent();
Log.d("value of intent:","intent values"+intent1);
selectedValueofList = intent1.getExtras().getString("selectedItem");
Log.d("retrieving values of selected listItem:","selected value of listitem:"+selectedValueofList);
}
catch (Exception e) {
// TODO: handle exception
Log.d("esception of taskLISt","Exception in retrieving vlaues of previous activity");
e.printStackTrace();
}
getAllTaskForSelectedCategory(selectedValueofList);
refreshTaskButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getAllTaskForSelectedCategory(selectedValueofList);
}
});
Button addTaskButton = (Button) findViewById(R.id.addBtnForTask);
addTaskButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openTaskList = new Intent(TaskListCategoryWise.this,TaskList.class);
openTaskList.putExtra("ListitemValues",selectedValueofList);
startActivityForResult(openTaskList,0);
}
});
checkBoxInstance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if ((checkBoxInstance).isChecked()) {
Toast.makeText(getApplicationContext(),
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
}
});
}
public void getAllTaskForSelectedCategory(String categorySelection)
{
Log.d("message for function call","getAllTaskForSelectedCategory function called:"+categorySelection);
try
{dataBaseAdapterInstance1 = dataBaseAdapterInstance1.openToRead();
taskClassListinstance = dataBaseAdapterInstance1.getAllTaskCategoryWise(categorySelection);
Log.d("LISTVALUES OF TASK TABLE:","VALUES:" + taskClassListinstance);
listViewInstance.setAdapter(new CustomListAdapter(getApplicationContext(),taskClassListinstance));
new CustomListAdapter(this,taskClassListinstance).notifyDataSetChanged();
listViewInstance.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
TaskClass classinstance = (TaskClass) taskClassListinstance.get(position);
Toast.makeText(getApplicationContext(),"values:" + classinstance,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"id values:"+classinstance.getTask_id(),Toast.LENGTH_SHORT).show();}
});
dataBaseAdapterInstance1.close();
}
catch (Exception e) {
// TODO: handle exception
Log.d("exception in getAllTaskForSelectedCategory","error");
e.printStackTrace();
}
}
}