更改 ImageView 的资源时强制关闭

发布于 2024-12-01 11:00:25 字数 4487 浏览 1 评论 0原文

当我单击某个项目时,更改图像视图的资源时遇到一些问题。 我知道任何图形更新都应该在 UI 线程中完成,我尝试了两种方法但没有成功:

...我的代码没有处理程序:

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

...我的代码有处理程序和可运行:

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

}

我已经设置了 布局 XML 中的 onClick 为“clicked”,并为所有可点击图像提供唯一的 id。当我将 Log.d() 几乎放在代码中的任何位置时,我注意到它在 iv.setImageResource(R.drawable.avatar_0); 命令处停止。

有人可以帮我吗?我缺少什么?谢谢

这里有一些额外的 LOGCAT 信息:

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/WifiService(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

橙色/红色的第一行是这三行:

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

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/WifiService(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2024-12-08 11:00:25

一般来说,代码没问题,您不需要处理程序,因为您从主活动中更改了资源。

我认为问题是你从 v.getId() 获取 imageView

是 imageView 上的 onclick 吗?可以直接 ((ImageView)v).setImageResource(...) 或者在onCreate方法中获取图片

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文