Android On click listener in layout
<p>Hi this is my custom view and I'm getting null pointer exception when I set on click listener.</p>

<pre><code>public class PestButtons extends AbsoluteLayout{

    private LayoutInflater inflater;

    private OnClickListener screenshotOnClickListener = new OnClickListener() {
        public void onClick(View v) {
      //      getContext().startActivity(new Intent(getContext(), ScreenshotActivity.class));
        Log.i("a", "a");
        }
    };  


    public PestButtons(Context context, AttributeSet attrs) {
        super(context, attrs);

        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.pestbuttons, this,true);
         ImageButton img = (ImageButton) this.findViewById(R.id.reportListBtn);
              img.setOnClickListener(screenshotOnClickListener);

    }




}
</code></pre>

<p>The xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;



&lt;merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"&gt;


    &lt;ImageButton id="@+id/reportListBtn"
    android:src="@drawable/reportlist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_x="5px"
    android:layout_y="20px"

/&gt;



 &lt;/merge&gt;
</code></pre>

<p>My useage layout:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;AbsoluteLayout
    android:id="@+id/main"

android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
&gt;

&lt;com.AgricultureTrading.PestButtons
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" /&gt;

&lt;TextView
android:id="@+id/err"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:layout_x="320px"
android:layout_y="25px"

/&gt;

&lt;TextView
android:id="@+id/weather"
android:layout_width="260px"
android:layout_height="wrap_content"
android:layout_x="140px"
android:layout_y="60px"
/&gt;

&lt;ImageView
android:id="@+id/loaderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="395px"
android:layout_y="60px"
/&gt;

&lt;Button
android:id="@+id/playBtn"
android:layout_width="93px"
android:layout_height="wrap_content"
android:text="Play sound"
android:layout_x="140px"
android:layout_y="120px"
android:onClick="playBtn_click"
/&gt;


&lt;ImageButton
android:id="@+id/userimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="150px"
android:layout_y="5px"
android:onClick="changeimage_click"
/&gt;

&lt;TextView
android:id="@+id/weatherLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="140px"
android:layout_y="96px"
/&gt;      
&lt;/AbsoluteLayout&gt;
</code></pre>

<p>Error log:</p>

<p>09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime  FATAL EXCEPTION: main
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AgricultureTrading/com.AgricultureTrading.Tabs}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AgricultureTrading/com.AgricultureTrading.ProfileActivity}: android.view.InflateException: Binary XML file line #68: Error inflating class com.AgricultureTrading.PestButtons
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.ActivityThread.access$500(ActivityThread.java:122)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.os.Handler.dispatchMessage(Handler.java:99)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.os.Looper.loop(Looper.java:132)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.ActivityThread.main(ActivityThread.java:4123)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at java.lang.reflect.Method.invokeNative(Native Method)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:491)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at dalvik.system.NativeStart.main(Native Method)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime  Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AgricultureTrading/com.AgricultureTrading.ProfileActivity}: android.view.InflateException: Binary XML file line #68: Error inflating class com.AgricultureTrading.PestButtons
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.ActivityThread.startActivityNow(ActivityThread.java:1656)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.widget.TabHost.setCurrentTab(TabHost.java:345)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at com.AgricultureTrading.Tabs.onCreate(Tabs.java:34)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.Activity.performCreate(Activity.java:4397)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      ... 11 more
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime  Caused by: android.view.InflateException: Binary XML file line #68: Error inflating class com.AgricultureTrading.PestButtons
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.view.LayoutInflater.createView(LayoutInflater.java:596)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:671)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.app.Activity.setContentView(Activity.java:1786)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at com.AgricultureTrading.ProfileActivity.onCreate(ProfileActivity.java:43)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      ... 20 more
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime  Caused by: java.lang.reflect.InvocationTargetException
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at java.lang.reflect.Constructor.constructNative(Native Method)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at java.lang.reflect.Constructor.newInstance(Constructor.java:416)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at android.view.LayoutInflater.createView(LayoutInflater.java:576)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      ... 31 more
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime  Caused by: java.lang.NullPointerException
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      at com.AgricultureTrading.PestButtons.(PestButtons.java:43)
09:51:56.153    717 com.AgricultureTrading  ERROR   AndroidRuntime      ... 34 more</p>
