Reputation: 137
EDIT: so apparently my question was tagged as duplicate, the thing is I spent a good chunk of time on stackoverflow looking for a solution before posting my question.my question is not "what is null pointer exception" please help
noob here, I am learning how to use adapters to populate ListView, I am having problem with my custom adapter when I try to use it the App crashes and gives me this error:
java.lang.NullPointerException
at android.widget.AbsListView.obtainView(AbsListView.java:2265)
however when I try to use an ArrayAdatper it works just fine. I suspect that I am passing the wrong context.
note that I using a fragment instead of an activity
the code supposed to populate a listview myCustomListview
from a dummy string array dummyValues
into a Textfield location
in the custom_row
layout
Spendiing
package app.hujair.android.example.com.aou_project_nm;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Spendings extends Fragment {
View view;
public Spendings() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,Bundle savedInstanceState) {
//inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.fragment_spendings, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView listView = (ListView) view.findViewById(R.id.myCustomListview);
String[] dummyValues = new String[]{"hello", "something"};
listView = (ListView) view.findViewById(R.id.myCustomListview);
//CustomAdapter adapter = new CustomAdapter(getActivity(),dummyValues);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getActivity(),R.layout.custom_row,R.id.location, dummyValues);
listView.setAdapter(adapter);
}
}
CustomAdapter
package app.hujair.android.example.com.aou_project_nm;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] transcation_type) {
super(context,R.layout.custom_row ,transcation_type);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.custom_row, parent, false);
String singleLocation = getItem(position);
TextView location = (TextView)customView.findViewById(R.id.location);
location.setText(singleLocation);
return convertView;
}
}
fragment_spending
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.hujair.android.example.com.aou_project_nm.Spendings">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total spendings goes here."
android:gravity="center"
android:textSize="35dp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:id="@+id/Spending_text"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myCustomListview"
android:layout_marginTop="15dp"
android:layout_below="@+id/Spending_text"
android:divider="@null"
></ListView>
</RelativeLayout>
custom_row
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<ImageView
android:layout_marginTop="35dp"
android:layout_marginLeft="25dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/custom_row_icon"
android:src="@drawable/minus_circle"/>
<TextView
android:layout_marginTop="-2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="note note note note ..."
android:textSize="15dp"
android:id="@+id/note"
android:layout_alignTop="@+id/amount"
android:layout_alignStart="@+id/location" />
<TextView
android:layout_alignParentRight="true"
android:gravity="right"
android:layout_marginTop="32dp"
android:layout_marginRight="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Amount"
android:textSize="18dp"
android:id="@+id/amount"/>
<TextView
android:layout_marginTop="-8dp"
android:layout_below="@+id/amount"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/custom_row_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="location"
android:textSize="12dp"
android:id="@+id/location" />
<TextView
android:layout_marginTop="-2dp"
android:layout_below="@+id/location"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/custom_row_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time"
android:textSize="12dp"
android:id="@+id/time" />
</RelativeLayout>
Upvotes: 2
Views: 440
Reputation: 1178
you are returning null from getView, you must return customView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.custom_row, parent, false);
String singleLocation = getItem(position);
TextView location = (TextView)customView.findViewById(R.id.location);
location.setText(singleLocation);
return customView;
}
Upvotes: 1