0

当我尝试使图像按钮的透明区域不可点击时出现异常。当我尝试实现 OnTouchListener 时引发运行时异常。请帮我解决这个问题。

ImageButton imgView= (ImageButton) findViewById(R.id.imageButton1);
     imgView.setDrawingCacheEnabled(true);
     imgView.setOnTouchListener(new OnTouchListener(){


    //private final OnTouchListener changeColorListener = new OnTouchListener() {      
         @Override     
         public boolean onTouch(View v, MotionEvent event) {        
          Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());        
          int color = bmp.getPixel((int) event.getX(),(int) event.getY());         
         if (color == Color.TRANSPARENT)            
          return false;        
          else {             
         //code to execute
         Toast.makeText(MainActivity.this, "Image button is being clicked", Toast.LENGTH_SHORT).show();             
         return true;         
         }     
         }


         });
}

}

MainActivity.XML 文件

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/worldfinal"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SELECT A DESIRED COUNTRY" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="28dp"
    android:layout_marginTop="93dp"
    android:src="@drawable/india1" 
    android:background="@android:color/transparent"  />

日志文件是

    08-06 10:52:04.471: E/AndroidRuntime(329): FATAL EXCEPTION: main
08-06 10:52:04.471: E/AndroidRuntime(329): java.lang.NullPointerException
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.graphics.Bitmap.createBitmap(Bitmap.java:367)
08-06 10:52:04.471: E/AndroidRuntime(329):  at com.sunojsworkshop.MainActivity$1.onTouch(MainActivity.java:34)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.view.View.dispatchTouchEvent(View.java:3881)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 10:52:04.471: E/AndroidRuntime(329):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
08-06 10:52:04.471: E/AndroidRuntime(329):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
08-06 10:52:04.471: E/AndroidRuntime(329):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.os.Looper.loop(Looper.java:123)
08-06 10:52:04.471: E/AndroidRuntime(329):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-06 10:52:04.471: E/AndroidRuntime(329):  at java.lang.reflect.Method.invokeNative(Native Method)
08-06 10:52:04.471: E/AndroidRuntime(329):  at java.lang.reflect.Method.invoke(Method.java:507)
08-06 10:52:04.471: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-06 10:52:04.471: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-06 10:52:04.471: E/AndroidRuntime(329):  at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

0

如果存在 india1 图像,请尝试以下操作:

    public static Bitmap loadBitmapFromView(View v) {
     Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
     Canvas c = new Canvas(b);
     v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
     v.draw(c);
     return b;
}
于 2013-08-06T06:42:34.110 回答
0

可绘制文件夹中缺少您的一个资源可能是@drawable/india1

于 2013-08-06T06:42:03.880 回答
0

尝试这个:

实现这个方法:

public static Bitmap loadBitmapFromView(View v) {
     Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
     Canvas c = new Canvas(b);
     v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
     v.draw(c);
     return b;
}

在你的 OnTouchListners OnTouch 中:

@Override     
         public boolean onTouch(View v, MotionEvent event) {        
          Bitmap bmp = loadBitmapFromView(v);     
          int color = bmp.getPixel((int) event.getX(),(int) event.getY());         
         if (color == Color.TRANSPARENT)            
          return false;        
          else {             
         //code to execute
         Toast.makeText(MainActivity.this, "Image button is being clicked", Toast.LENGTH_SHORT).show();             
         return true;         
              }     
            }
         });
于 2013-08-06T06:44:58.287 回答