我想在5秒后交换当前在我的imageview中的图像,但我尝试过的方法不起作用。任何帮助将不胜感激。
这是我的imageview
<ImageView
android:id="@+id/message_sent_indicator"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/ic_action_accept"
android:paddingRight="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="right" />
对我来说,改变这一点最简单的方法是什么。
02-23 16:49:15.109 22171-22171/com.mycompany.messaging D/libc﹕ pt_debug : pthread_create->start_routine=0x5013ee31, tls=0x54a77f00, arg=0x51e3c1c0
02-23 16:49:15.119 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Mapped buffer base:0x54a78000 size:2088960 offset:0 fd:75
02-23 16:49:15.119 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Mapped buffer base:0x4082a000 size:4096 offset:0 fd:77
02-23 16:49:15.119 22171-22231/com.mycompany.messaging D/libc﹕ pt_debug : __thread_entry->func=0x5013ee31
, tls=0x54a77f00, arg=0x51e3c1c0
02-23 16:49:15.180 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Mapped buffer base:0x54ccb000 size:2088960 offset:0 fd:79
02-23 16:49:15.180 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Mapped buffer base:0x40919000 size:4096 offset:0 fd:81
02-23 16:49:15.220 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Mapped buffer base:0x54ec9000 size:2088960 offset:0 fd:83
02-23 16:49:15.220 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Mapped buffer base:0x50026000 size:4096 offset:0 fd:85
02-23 16:49:15.220 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Unmapping buffer base:0x53b76000 size:2088960
02-23 16:49:15.220 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Unmapping buffer base:0x40007000 size:4096
02-23 16:49:15.220 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Unmapping buffer base:0x54317000 size:2088960
02-23 16:49:15.220 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Unmapping buffer base:0x4003e000 size:4096
02-23 16:49:15.220 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Unmapping buffer base:0x54725000 size:2088960
02-23 16:49:15.220 22171-22171/com.mycompany.messaging D/memalloc﹕ ion: Unmapping buffer base:0x40078000 size:4096
02-23 16:49:20.075 22171-22171/com.mycompany.messaging W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40d0c468)
02-23 16:49:20.085 22171-22171/com.mycompany.messaging E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.mycompany.messaging.ConversationView$1.run(ConversationView.java:58)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5454)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
02-23 16:49:22.808 22171-22171/com.mycompany.messaging D/Process﹕ killProcess, pid=22171
02-23 16:49:22.808 22171-22171/com.mycompany.messaging D/Process﹕ dalvik.system.VMStack.getThreadStackTrace(Native Method)
02-23 16:49:22.808 22171-22171/com.mycompany.messaging D/Process﹕ java.lang.Thread.getStackTrace(Thread.java:599)
02-23 16:49:22.808 22171-22171/com.mycompany.messaging D/Process﹕ android.os.Process.killProcess(Process.java:944)
02-23 16:49:22.808 22171-22171/com.mycompany.messaging D/Process﹕ com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
02-23 16:49:22.808 22171-22171/com.mycompany.messaging D/Process﹕ java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
02-23 16:49:22.808 22171-22171/com.mycompany.messaging D/Process﹕ java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
02-23 16:49:22.818 22171-22171/com.mycompany.messaging D/Process﹕ dalvik.system.NativeStart.main(Native Method)
这是我的onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation_view);
Intent intent = getIntent();
String thread = intent.getStringExtra("threadId");
threadId = Integer.parseInt(thread);
write_message = (EditText) findViewById(R.id.write_a_message);
post_message_button = (TextView) findViewById(R.id.send_message_button);
final ImageView iv = (ImageView) findViewById(R.id.message_sent_indicator);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
}
}, 5000); // 5000 milliseconds
final ListView convoListView = (ListView) this.findViewById(R.id.conversationListView);
final ArrayList<ConversationItem> convoItems = this.GetItems(threadId);
final ConversionAdapter convoAdapter = new ConversionAdapter
(this, R.layout.conversation_list_item, convoItems);
post_message_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView username = (TextView) findViewById(R.id.conversation_username2);
convoItems.add(new ConversationItem(1656, 1, 888, "Bill Smith", DateTime.now(), write_message.getText().toString()));
convoAdapter.notifyDataSetChanged();
if (v.getId() == R.id.send_message_button);
write_message.setText("");
}
});
convoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
convoListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
convoListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
final int checkedCount = convoListView.getCheckedItemCount();
mode.setTitle(checkedCount + " Message selected");
convoAdapter.toggleSelection(position);
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
// Calls getSelectedIds method from ListViewAdapter Class
SparseBooleanArray selected = convoAdapter
.getSelectedIds();
// Captures all selected ids with a loop
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
ConversationItem selecteditem = convoAdapter
.getItem(selected.keyAt(i));
// Remove selected items following the ids
convoAdapter.remove(selecteditem);
}
}
// CAB
mode.finish();
return true;
default:
return false;
}
}
答案 0 :(得分:1)
使用处理程序延迟操作:
final ImageView iv = (ImageView) findViewById(R.id.message_sent_indicator);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
iv.setImageDrawable(getDrawable(R.drawable.some_image_id));
}
}, 5000); // 5000 milliseconds
修改强>
getDrawable
是一种活动方法,但它仅适用于API 21 +。
旧方法可在Resource
实例上使用:
myimage.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
答案 1 :(得分:0)
使用postDelayed的Handler方法:
int delay = 3000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//swap image
}
}, delay);
必须在活动的主线程中调用方法。