开发者

Android On click listener in layout

Hi this is my custom view and I'm getting null pointer exception when I set on click listener.

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);

    }




}

The xml:

<?xml version="1.0" encoding="UTF-8"?>



<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <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"

/>



 </merge>

My useage layout:

<?xml version="1.0" e开发者_JAVA技巧ncoding="utf-8"?>
<AbsoluteLayout
    android:id="@+id/main"

android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<com.AgricultureTrading.PestButtons
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

<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"

/>

<TextView
android:id="@+id/weather"
android:layout_width="260px"
android:layout_height="wrap_content"
android:layout_x="140px"
android:layout_y="60px"
/>

<ImageView
android:id="@+id/loaderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="395px"
android:layout_y="60px"
/>

<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"
/>


<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"
/>

<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"
/>      
</AbsoluteLayout>

Error log:

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


I assume that you want to get img from inflated view:

         View v = inflater.inflate(R.layout.pestbuttons, this,true);
         ImageButton img = (ImageButton) v.findViewById(R.id.reportListBtn);
         if (img != null) {
             img.setOnClickListener(screenshotOnClickListener);
         }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜