2013-07-07 96 views
0

我有一個編輯文本,我需要勾TextChanged事件,以做task.I已定義的overrdiimg的EditText上第一,但在文本chnaged事件添加文本提取代碼後,後所述IDE標誌的錯誤和經銷商通過在線 添加final改性劑像這樣應用程序崩潰添加final修飾符來編輯文本

final EditText et1=(EditText)findViewById(R.id.editText1); 

chnaging該應用crahses文本後校正它,所以我試圖以檢索所述的EditText第一2次用於鉤住到事件然後爲獲取文本,然後該應用程序崩潰。 IDE將促使我前添加在類似案件中final修飾符,但它已墜毀的應用程序,所以我不得不添加fetchbyID在這裏需要的對象點

EditText et1=(EditText)findViewById(R.id.editText1); 
     et1.addTextChangedListener(new TextWatcher() { 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 

       new Thread(new Runnable() { 
         public void run() { 
         ImageView img=(ImageView)findViewById(R.id.imageView1); 
         EditText et=(EditText)findViewById(R.id.editText1); 
         img.setImageBitmap(thumbnail); 
          Bitmap b = workwithtext(thumbnail,et.getText().toString(),10); 
          img.setImageBitmap(b); 
         } 
         }).start(); 
       } 

       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

       } 

       @Override 
       public void afterTextChanged(Editable s) { 

       } 
      }); 

請幫我解決這個問題

回答

1

由於您試圖從非UI線程修改UI組件,因此崩潰。

new Thread(new Runnable() { 
      public void run() { 
       ImageView img=(ImageView)findViewById(R.id.imageView1); 
       //...      
       img.setImageBitmap(thumbnail); 
       //... 
       } 
}).start(); 
+0

好吧,我怎麼可以設置ImageView的,因爲過程可能會凍結用戶界面? – techno

+1

你可以使用一個'AsyncTask',但我不明白爲什麼這應該在'onTextChanged進行()'。這意味着每次輸入一個字符時,都會設置圖像。這真的是你想要的嗎? –

+0

怎麼樣?你可以給一個exmaple – techno

0

將EditText et1作爲類中的字段而不是本地變量。

+0

你能解釋一下,即時通訊新到Android剛開始 – techno

+0

有ET1有一個字段,即私人的EditText ET1;然後在你的方法(可能onCreate)使用:et1 = findViewByID(); – james

0

您正在從背景therad這是不可能的,更新界面。還要將您的視圖初始化爲onCreate

img.setImageBitmap(b); // updating ui from thread not possible 

使用runOnUiThread更新UI

 runOnUiThread(new Runnable() { 
    public void run() { 
      // update ui here 
    } 
    }); 

注意你開始一個新的線程每次你onTextChanged被稱爲這是不好的。此外,您還可以在不需要的線程運行方法中每次初始化imageview和editext。

+0

謝謝,但會凍結用戶界面?說如果我把img.setImageBitmap(b);在線程代碼之後,在線程完成後將圖像置位還是將其設置爲正常代碼流? – techno

+0

@techno閱讀此內容。 http://developer.android.com/training/articles/perf-anr.html。如果你阻止用戶線程是的。但正如Andy Res所評論的,你是否真的需要這個 – Raghunandan

+0

@techno你真的需要一個線索來回答你的問題嗎?你是否在'workwithtext'中做了任何網絡實現的東西。 – Raghunandan

相關問題