2015-02-23 63 views
0

我想在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

這看起來並不像一個適當的應用程序日誌output.Try重新啓動IDE。 – Simas 2015-02-23 16:21:27

+0

只是更新了它 – Sim 2015-02-23 16:35:23

+0

使用'Timer'和'Animation'來幻燈片 – Xcihnegn 2015-02-23 16:40:16

回答

0

使用HandlerpostDelayed方法是:

int delay = 3000; 
new Handler().postDelayed(new Runnable() { 

        @Override 
        public void run() { 
         //swap image 
        } 
       }, delay); 

方法必須在活動的主線程中調用。

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

而不是創建一個新的處理程序,op可以使用視圖的內部處理程序? – Blackbelt 2015-02-23 15:48:52

+0

@Blackbelt正確,那就是'iv.postDelayed()'。 – Simas 2015-02-23 15:49:46

+0

我會替換iv.setImageDrawable(getDrawable(R.drawable.some_image_id)); 與 iv.setImageDrawable(getResources()。getDrawable(R.drawable.ic_launcher)); – Sim 2015-02-23 16:42:08