单击某个项目时,我在更改图像视图的资源方面遇到了一些问题。 我知道任何图形更新都应该在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);
}}
...我的代码带有处理程序和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);
}
}
我已将布局XML中的onClick
设置为“已点击”,并为所有可点击图片指定了唯一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
答案 0 :(得分:1)
一般来说代码没问题,你不需要处理程序,因为你从主要活动中改变了资源。
我认为问题是您从v.getId()
获取imageView是onView吗?你可以直接((ImageView)v).setImageResource(...)或者在onCreate方法中获取图像