venura venura - 7 months ago 9
Java Question

Android App stopped when custom list view calls

I tried to create a simple custom list view using a base adapter. When I run the app it stopped.When I check the error with getView method. I tried many times to fix but didn't work. Please help me to find a solution for this problem.

My custom adapter class.

public class activity_one extends Activity {



ListView list;

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

list=(ListView)findViewById(R.id.listView2);
list.setAdapter(new VivzAdapter(this));


}


class SingleRow{
ListView list;
String polices;
String policenum;
SingleRow(String polices, String policenum) {
this.polices = polices;
this.policenum=policenum;
}



}
class VivzAdapter extends BaseAdapter {

ArrayList<SingleRow> list;
Context context;
VivzAdapter(Context c){
list=new ArrayList<SingleRow>();

Resources res=c.getResources();
String[] polices=res.getStringArray(R.array.polices);
String[] policenum=res.getStringArray(R.array.policenum);
for(int i=0;i<10;i++)
{
list.add(new SingleRow(polices[i], policenum[i]));
}
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int i) {
return list.get(i);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {


LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row= inflater.inflate(R.layout.listview_singlerow,viewGroup,false);
TextView polices=(TextView) row.findViewById(R.id.org_name);
TextView policenum=(TextView)row.findViewById(R.id.cn_num);

SingleRow temp=list.get(i);

polices.setText(temp.polices);
policenum.setText(temp.policenum);

return row;
}
}}


Custom list view xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:src="@drawable/hello_avator"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Company Name"
android:id="@+id/org_name"
android:layout_alignTop="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="000-0000000"
android:id="@+id/cn_num"
android:layout_alignLeft="@+id/org_name"
android:layout_alignStart="@+id/org_name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/org_name"
android:layout_alignBottom="@+id/imageView2" />




Logcat

java.lang.NullPointerException
at com.example.abc.helloapp.activity_one$VivzAdapter.getView(activity_one.java:116)
at android.widget.AbsListView.obtainView(AbsListView.java:2267)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1156)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
at android.view.View.measure(View.java:15172)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Answer

you have missed initializing context variable in the adapter thus this line fails with NPE

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

because of context of course, to solve this update your adapter constructor to initialize the variable context using this line context = c; so the code should be like this :

VivzAdapter(Context c){
    list=new ArrayList<SingleRow>();
    context = c; // add this
    Resources res=c.getResources();
    String[] polices=res.getStringArray(R.array.polices);
    String[] policenum=res.getStringArray(R.array.policenum);
    for(int i=0;i<10;i++)
    {
        list.add(new SingleRow(polices[i], policenum[i]));
    }
}