Reputation: 195
Getting a nullpointerexception on line 42 of this, on the myListView.setAdapter(aa); The code should work but can't really see what's going on it that stops the adapter from populating. It works if I comment out the arrayadapter part, but doesn't work with it in, so any help would be appreciated.
I've tried changing the layout to just a ListView, and having the class extend ListActivity, but that didn't work either.
package ie.ucc.bis.is4432calculator;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class secondActivity extends Activity {
protected double latestResult = 0.0;
protected TextView Results;
public String ResultString;
ArrayList<String> ListItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
Results= (TextView)findViewById(R.id.textView1);
Intent i = this.getIntent(); /*delete "this."*/
latestResult = i.getDoubleExtra("latestResult", 0.0);
Results.setText(String.valueOf(latestResult));
ListItems = i.getStringArrayListExtra("ListItems");
Results.setText(getResultString());
ListView myListView = (ListView)findViewById(R.id.listView1);
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,ListItems);
myListView.setAdapter(aa);
}
public void setResultString(String ResultString) {
this.ResultString = ResultString;
}
public String getResultString(){
return ResultString;
}
}
Xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
Not really sure what's going wrong, I'm assuming the ArrayAdapter just won't populate, but can't really see why as this code works in a different project, but just not here. Logcat results are posted below.
03-15 16:11:12.104: E/AndroidRuntime(844): FATAL EXCEPTION: main
03-15 16:11:12.104: E/AndroidRuntime(844): java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.ucc.bis.is4432calculator/ie.ucc.bis.is4432calculator.secondActivity}: java.lang.NullPointerException
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.os.Looper.loop(Looper.java:123)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-15 16:11:12.104: E/AndroidRuntime(844): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 16:11:12.104: E/AndroidRuntime(844): at java.lang.reflect.Method.invoke(Method.java:507)
03-15 16:11:12.104: E/AndroidRuntime(844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-15 16:11:12.104: E/AndroidRuntime(844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-15 16:11:12.104: E/AndroidRuntime(844): at dalvik.system.NativeStart.main(Native Method)
03-15 16:11:12.104: E/AndroidRuntime(844): Caused by: java.lang.NullPointerException
03-15 16:11:12.104: E/AndroidRuntime(844): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.widget.ListView.setAdapter(ListView.java:454)
03-15 16:11:12.104: E/AndroidRuntime(844): at ie.ucc.bis.is4432calculator.secondActivity.onCreate(secondActivity.java:42)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-15 16:11:12.104: E/AndroidRuntime(844): ... 11 more
Thanks for any help
Upvotes: 4
Views: 6852
Reputation: 195
ListItems was null and was causing the issue, had forgot to set it as a value on the other activity.
Upvotes: 0
Reputation: 199805
logcat says "Caused by: java.lang.NullPointerException at android.widget.ArrayAdapter.getCount"
This would occur if the ArrayList you pass in is null. Put a null check for ListItems
before creating the ArrayAdapter.
Upvotes: 6