Friday, April 26, 2013

[android help] building ListView with Adapter


There is a main activity named CategoryListActivity in my project. Please see the following code:



public class CategoryListActivity extends Activity implements LoaderCallbacks> {
private ArrayAdapter mListAdapter;
private ListView listview;
private Categories categories = new Categories();
private static final int LOADER_ID_CHECKS = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listview = (ListView) findViewById(R.id.listview);
mListAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1,
categories.categoryArray);
listview.setOnItemClickListener(mOnItemClickListener);
listview.setAdapter(mListAdapter);

getLoaderManager().initLoader(LOADER_ID_CHECKS, null, this);
}
...
}

public class Categories {
public List categoryArray;
}
public class Category {
public int categoryId;
public String categoryName;
public List questions;
}
public class Questions {
public String question;
public String answer;
}


I've tried to debug the project and it always stop at listview.setAdapter(mListAdapter). It says source not found When running the project, it gives me the following error:



04-25 20:45:01.106: D/AndroidRuntime(19891): Shutting down VM


04-25 20:45:01.106: W/dalvikvm(19891): threadid=1: thread exiting with uncaught exception (group=0x41760930)


04-25 20:45:01.106: E/AndroidRuntime(19891): FATAL EXCEPTION: main


04-25 20:45:01.106: E/AndroidRuntime(19891): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flashcard/com.example.flashcard.CategoryListActivity}: java.lang.NullPointerException


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.app.ActivityThread.access$600(ActivityThread.java:141)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.os.Handler.dispatchMessage(Handler.java:99)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.os.Looper.loop(Looper.java:137)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.app.ActivityThread.main(ActivityThread.java:5041)


04-25 20:45:01.106: E/AndroidRuntime(19891): at java.lang.reflect.Method.invokeNative(Native Method)


04-25 20:45:01.106: E/AndroidRuntime(19891): at java.lang.reflect.Method.invoke(Method.java:511)


04-25 20:45:01.106: E/AndroidRuntime(19891): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)


04-25 20:45:01.106: E/AndroidRuntime(19891): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)


04-25 20:45:01.106: E/AndroidRuntime(19891): at dalvik.system.NativeStart.main(Native Method)


04-25 20:45:01.106: E/AndroidRuntime(19891): Caused by: java.lang.NullPointerException


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.widget.ListView.setAdapter(ListView.java:462)


04-25 20:45:01.106: E/AndroidRuntime(19891): at com.example.flashcard.CategoryListActivity.onCreate(CategoryListActivity.java:58)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.app.Activity.performCreate(Activity.java:5104)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)


04-25 20:45:01.106: E/AndroidRuntime(19891): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)


04-25 20:45:01.106: E/AndroidRuntime(19891): ... 11 more




.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...