Layoutinflater nullpointer exception in BaseAdapter class getView()

325 views Asked by At

I tried solutions from others' questions but didn't work. I am getting the following error in BaseAdapter class for LayoutInflater:

06-21 17:24:38.808: E/AndroidRuntime(23959): FATAL EXCEPTION: main
06-21 17:24:38.808: E/AndroidRuntime(23959): Process: org.nick.yarntrading, PID: 23959
06-21 17:24:38.808: E/AndroidRuntime(23959): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nick.yarntrading/org.nick.yarntrading.MainActivity}: java.lang.NullPointerException
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
 06-21 17:24:38.808: E/AndroidRuntime(23959):   at android.os.Handler.dispatchMessage(Handler.java:110)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.os.Looper.loop(Looper.java:193)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.app.ActivityThread.main(ActivityThread.java:5292)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at java.lang.reflect.Method.invokeNative(Native Method)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at java.lang.reflect.Method.invoke(Method.java:515)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at dalvik.system.NativeStart.main(Native Method)
06-21 17:24:38.808: E/AndroidRuntime(23959): Caused by: java.lang.NullPointerException
06-21 17:24:38.808: E/AndroidRuntime(23959):    at org.nick.card_view.DataListAdapter.<init>(DataListAdapter.java:34)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at org.nick.yarntrading.Home.onCreateView(Home.java:36)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
 06-21 17:24:38.808: E/AndroidRuntime(23959):   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.app.Activity.performStart(Activity.java:5274)
06-21 17:24:38.808: E/AndroidRuntime(23959):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
06-21 17:24:38.808: E/AndroidRuntime(23959):    ... 11 more

Here is the DataAdapter class that extends BaseAdapter :

public class DataListAdapter extends BaseAdapter {

App app;
ArrayList<HashMap<String,String>> listdata = null;
FragmentManager fm;
LayoutInflater inflater = null;
LinearLayout lnlyt;


//ProductList prodctlst;
public DataListAdapter(App a, FragmentManager f)
{
    this.app =a;
    this.fm = f;
    **inflater = (LayoutInflater) app.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);**
    listdata = new ProductList(this.app).returnList();
}
@Override
public int getCount() {

    return listdata.size();
}

@Override
public Object getItem(int position) {
    return listdata.get(position);
}

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

public class ViewHolder{
    TextView prod_name,prod_price,prod_descrp;

}
@Override
public View getView(int position, View v, ViewGroup parent) {
   final ViewHolder h;
    if(v==null)
    {

        v = inflater.inflate(R.layout.card_view,null);
        h = new ViewHolder();
        h.prod_name =(TextView) lnlyt.findViewById(R.id.prod_name);
        h.prod_price = (TextView)lnlyt.findViewById(R.id.prod_price);
        h.prod_descrp = (TextView) lnlyt.findViewById(R.id.prod_description);
        v.setTag(h);
    }else
        h = (ViewHolder) v.getTag();
        h.prod_name.setText(listdata.get(position).get("prodctName"));
        h.prod_price.setText(listdata.get(position).get("prodctPrice"));
        h.prod_descrp.setText(listdata.get(position).get("prodctDescription"));

    return v;
 }

}

Here is the xml file:

<?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">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="@dimen/feed_item_margin"
    android:layout_marginRight="@dimen/feed_item_margin"
    android:layout_marginTop="@dimen/feed_item_margin"
    android:background="@drawable/bg_parent_rounded_corner"
    android:orientation="vertical"
    android:paddingTop="@dimen/feed_item_padding_top_bottom" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/feed_item_padding_left_right"
            android:paddingRight="@dimen/feed_item_padding_left_right" >
                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="@dimen/feed_item_profile_info_padd" >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/prod_name"
                        android:textStyle="bold"
                        android:text="Mobile"
                        android:textSize="20sp"/>

                    <TextView
                        android:id="@+id/prod_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginStart="140dp"
                        android:layout_toEndOf="@+id/prod_name"
                        android:text="15.2641"
                        android:textSize="20sp" />

                </RelativeLayout>

            </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="15dp"
        android:layout_marginTop="8dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/prod_description"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:text="Description"
            />

    </LinearLayout>
    </LinearLayout>

Here is the code of Home class:

package org.nick.yarntrading;
 import android.annotation.SuppressLint;
 import android.os.Bundle;
 import android.support.v4.app.ListFragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ListView;
 import android.widget.RelativeLayout;

 import org.nick.card_view.DataListAdapter;


 public class Home extends ListFragment {
App a;
ListView card_list;
DataListAdapter adapter;
RelativeLayout prodctView;
public void home(App a)
{
this.a = a;
}
@Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}
@SuppressLint("InflateParams") @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     prodctView = (RelativeLayout)inflater.inflate(R.layout.home_listview,null);
        card_list = (ListView)prodctView.findViewById(android.R.id.list);
    adapter = new DataListAdapter(a,getActivity().getSupportFragmentManager(),getActivity());
    card_list.setAdapter(adapter);
    return prodctView;
}

}

And the App class extends the Applications. I tried the other solutions provided at the stackoverflow but it didn't worked. Here what I am trying to do is show a list \view after logging in to app.

Any Idea is appreciated. Thanks in advance.

2

There are 2 answers

0
Neerav Shah On

Here is the solution that worked for me I passed the getActivity() to the DataListAdapter and used it as Context like

Context cont;
//ProductList prodctlst;
public DataListAdapter(App a, FragmentManager f,Context context)

{
    this.app =a;
    this.fm = f;
    this.cont = context;
    inflater = ((Activity) cont).getLayoutInflater();
    listdata = new ProductList(this.app).returnList();
}
2
mfaisalhyder On

Hye there,

    v = inflater.inflate(R.layout.card_view,null);
    h = new ViewHolder();
    h.prod_name =(TextView) *lnlyt*.findViewById(R.id.prod_name);
    v.setTag(h);
}else
    h = (ViewHolder) v.getTag();
    h.prod_name.setText(listdata.get(position).get("prodctName"));

  return v;
}

You are referencing your views via LynLyt and you haven't even initialized it , try this :

@Override
public View getView(int position, View v, ViewGroup parent) {
//final ViewHolder h;// you have just declared it , initialize it first
final ViewHolder h = new ViewHolder();
if(v==null)
{

    v = inflater.inflate(R.layout.card_view,null);
    h = new ViewHolder();
    h.prod_name =(TextView) v.findViewById(R.id.prod_name);
    h.prod_price = (TextView)v.findViewById(R.id.prod_price);
    h.prod_descrp = (TextView) v.findViewById(R.id.prod_description);
    v.setTag(h);
}else
    h = (ViewHolder) v.getTag();
    h.prod_name.setText(listdata.get(position).get("prodctName"));
    h.prod_price.setText(listdata.get(position).get("prodctPrice"));
    h.prod_descrp.setText(listdata.get(position).get("prodctDescription"));

return v;
}

Logic is this , once you inflated your layout like this,

v = inflater.inflate(R.layout.card_view,null); now you have contained your layout with elements in view 'V' now find views with the reference of 'v' now.!

Hope i helped you.