0

次を使用して画像のソースを変更しようとすると:

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;
    }   
4

2 に答える 2

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

        }
    });
于 2013-03-15T12:06:17.727 に答える
0

イメージビューを初期化しましたか?

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

そして、これはあなたのイメージ名です - anfex2011?

于 2013-03-15T12:12:34.043 に答える