Reputation: 121
Puzzled about some code that does not work.
I am attempted to pass selected items from a list to a new activity. So, I have the code to capture selected items:
@Override
//when listItem is clicked
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//in checked, in listview get Item positions for checked items
SparseBooleanArray checked = l.getCheckedItemPositions();
//assign selected items to new String ArrayList
ArrayList<String> selectedItems = new ArrayList<String>();
//determine size of items checked (number)
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
position = checked.keyAt(i);
// Add location if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
//add selected items to madapter
selectedItems.add(madapter.getItem(position));
}
//create string output array, store new string array with selected items
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
MidwifeResultList.class);
// Create a bundle object
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
// Add the bundle to the intent.
intent.putExtras(b);
// start the ResultActivity
startActivity(intent);
And I have an activity that retrieves the bundle:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_midwife_result_list);
Bundle b = getIntent().getExtras();
String[] resultArr = b.getStringArray("selectedItems");
ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultArr);
lv.setAdapter(adapter);
}
However, when I run the app, it stops at the point of attempting to get to the new activty (nullpointerexception, pointing to the lv.setAdapter(adapter) line.
From what I researched, that error refers to a variable not properly instantiated..is there something wrong with how I am doing that for adapter? My first guess was that the selected item was not being properly captured, so when the new activity was attempting to retrieve information, it was coming up null.
Any help would be appreciated..thank you.
List:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Log:
04-20 15:09:47.619 32662-32662/android.bignerdranch.com.mobilemidwife E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: android.bignerdranch.com.mobilemidwife, PID: 32662
java.lang.RuntimeException: Unable to start activity ComponentInfo{android.bignerdranch.com.mobilemidwife/android.bignerdranch.com.mobilemidwife.MidwifeResultList}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.bignerdranch.com.mobilemidwife.MidwifeResultList.onCreate(MidwifeResultList.java:39)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
Upvotes: 0
Views: 279
Reputation: 5892
If the error that is throwing is a NullPointerException
at lv.setAdapter(adapter)
that only means one thing: lv = null
. So the reason can be that you are referencing an id that is not in the view inflated in setContentView
.
Things to check:
Upvotes: 2
Reputation: 8058
Instead of the code :-
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
put this code:-
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list" //change on this line
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Upvotes: 0