以编程方式更改图像源

时间:2013-03-15 12:05:06

标签: java android

当我尝试使用以下方法更改图像的来源时

img.setBackgroundResource(R.drawable.anfex2011);

img.setImageResource(R.drawable.anfex2011);

我收到错误:

03-15 13:56:56.215: E/AndroidRuntime(30733): FATAL EXCEPTION: main
03-15 13:56:56.215: E/AndroidRuntime(30733): java.lang.IllegalStateException: Could not execute method of the             activity
03-15 13:56:56.215: E/AndroidRuntime(30733):    at android.view.View$1.onClick(View.java:3691)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at android.view.View.performClick(View.java:4211)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at android.widget.CompoundButton.performClick(CompoundButton.java:105)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at android.view.View$PerformClick.run(View.java:17267)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at android.os.Handler.handleCallback(Handler.java:615)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at android.os.Looper.loop(Looper.java:137)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at java.lang.reflect.Method.invoke(Method.java:511)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at dalvik.system.NativeStart.main(Native Method)
03-15 13:56:56.215: E/AndroidRuntime(30733): Caused by: java.lang.reflect.InvocationTargetException
03-15 13:56:56.215: E/AndroidRuntime(30733):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at java.lang.reflect.Method.invoke(Method.java:511)
03-15 13:56:56.215: E/AndroidRuntime(30733):    at android.view.View$1.onClick(View.java:3686)
03-15 13:56:56.215: E/AndroidRuntime(30733):    ... 12 more
03-15 13:56:56.215: E/AndroidRuntime(30733): Caused by: java.lang.NullPointerException
03-15 13:56:56.215: E/AndroidRuntime(30733):    at com.TLC.aelminingsolutions.MainActivity.onClickLists(MainActivity.java:675)
03-15 13:56:56.215: E/AndroidRuntime(30733):    ... 15 more

我做错了什么?

修改 这是一些代码:

        @SuppressWarnings("deprecation")
        public void onClickLists(View v)
    {       
        switch (v.getId())
        {
    case R.id.radCoalMiningShallow :
    { 
        ImageView img =  (ImageView) findViewById(R.id.img);
        img.setBackgroundDrawable(R.drawable.anfex2011);
        //img.setImageDrawable(R.drawable.anfex2011);
        break;
    }   

2 个答案:

答案 0 :(得分:0)

试试这个,图片OnTouchListener()

  img.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {


            switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    imgTicket.setImageResource(R.drawable.hticket);
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    imgTicket.setImageResource(R.drawable.ticket);
                    break;
                }
                case MotionEvent.ACTION_CANCEL:{
                    imgTicket.setImageResource(R.drawable.ticket);
                    break;
                }
                }
                return true;
            // TODO Auto-generated method stub

        }
    });

答案 1 :(得分:0)

您是否初始化了您的图像视图?

ImageView img = (ImageView) findViewById(R.id.ImageView1);
img.setImageResource(R.drawable.anfex2011);

这是你的图像名称 - anfex2011?