开发者

Force close when changing the resource of an ImageView

开发者 https://www.devze.com 2023-03-30 16:25 出处:网络
I having some problem with changing the resource of an imageview when i click an item. I know that any graphical updates should be done in the UI thread and I have tried two ways without success:

I having some problem with changing the resource of an imageview when i click an item. I know that any graphical updates should be done in the UI thread and I have tried two ways without success:

...my code WITHOUT a handler:

public class AvatarPicker extends Activity {

ImageView iv; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void clicked(View v){
    iv = (ImageView) findViewById(v.getId());
    iv.setImageResource(R.drawable.avatar_0);
}}

...my code WITH a handler and runnable:

public class AvatarPicker extends Activity {

ImageView iv;
private Handler mHandler;

private Runnable updateImg = new Runnable() {
    @Override
    public void run() {
       iv.setImageResource(R.drawable.avatar_0);
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mHandler = new Handler(){};

}

public void clicked(View v){
    iv = (ImageView) findViewById(v.getId());
    mHandler.post(updateImg);
}

}

I have set the onClick in the layout XML to "clicked" and given all the clickable images an unique id. When i put Log.d() almost everywhere in the code i notice that it halts at the iv.setImageResource(R.drawable.avatar_0); command.

Can someone help me out here please? What am i missing? Thanks

Here is some additional LOGCAT info:

06-17 19:23:09.877: INFO/ActivityManager(1184): Process com.google.android.apps.maps:FriendService (pid 4580) has died.
06-17 19:23:09.907: INFO/ActivityManager(1184): Displayed activity weldeborn.avatar/.AvatarPicker: 1008 ms (total 1008 ms)
06-17 19:23:10.077: DEBUG/ddm-heap(4602): Got feature list request
06-17 19:23:10.157: DEBUG/dalvikvm(1701): GC freed 2115 objects / 112440 bytes in 99ms
06-17 19:23:12.607: DEBUG/AndroidRuntime(4602): Shutting down VM
06-17 19:23:12.607: WARN/dalvikvm(4602): threadid=3: thread exiting with uncaught exception (group=0x40026160)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602): Uncaught handler: thread main exiting due to uncaught exception
06-17 19:23:12.617: ERROR/AndroidRuntime(4602): java.lang.NullPointerException
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at weldeborn.avatar.AvatarPicker$1.run(AvatarPicker.java:21)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at android.os.Handler.handleCallback(Handler.java:587)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at android.os.Looper.loop(Looper.java:123)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at android.app.ActivityThread.main(ActivityThread.java:4370)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at java.lang.reflect.Method.invokeNative(Native Method)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at java.lang.reflect.Method.invoke(Method.java:521)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602):     at dalvik.system.NativeStart.main(Native Method)
06-17 19:23:12.627: ERROR/SemcCheckin(4602): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump
06-17 19:23:12.627: WARN/ActivityManager(1184): Unable to start service Intent { act=com.sonyericsson.android.jcrashcatcher.action.BUGREPORT_AUTO cmp=com.sonyericsson.android.jcrashcatcher/.JCrashCatcherService (has extras) }: not found
06-17 19:23:12.627: INFO/Process(1184): Sending signal. PID: 4602 SIG: 3
06-17 19:23:12.627: INFO/dalvikvm(4602): threadid=7: reacting to signal 3
06-17 19:23:12.627: ERROR/dalvikvm(4602): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
06-17 19:23:12.667: ERROR/SemcCheckin(1687): Get Crash Level : java.io.FileNotFoundException: /data/semc-checkin/crashdump
06-17 19:23:15.557: DEBUG/WifiS开发者_JS百科ervice(1184): ACTION_BATTERY_CHANGED pluggedType: 2
06-17 19:23:17.547: DEBUG/WifiService(1184): ACTION_BATTERY_CHANGED pluggedType: 2
06-17 19:23:22.707: DEBUG/dalvikvm(1852): GC freed 5128 objects / 468256 bytes in 150ms

The first lines that are being orange/red is these three:

06-17 19:23:12.607: WARN/dalvikvm(4602): threadid=3: thread exiting with uncaught exception (group=0x40026160)
06-17 19:23:12.617: ERROR/AndroidRuntime(4602): Uncaught handler: thread main exiting due to uncaught exception
06-17 19:23:12.617: ERROR/AndroidRuntime(4602): java.lang.NullPointerException


The code in general is ok, you dont need a handler because you change the resource from the main activity.

I think the problem is that you get the imageView from v.getId()

Is the onclick on the imageView? You can directly ((ImageView)v).setImageResource(...) or get the image in the onCreate method

0

精彩评论

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

关注公众号