簡體   English   中英

UIThread上的setText不起作用

[英]setText on UIThread not working

在我的AsyncTask中

@Override
protected void onProgressUpdate(Void... values)

方法,該方法在UI線程上運行,我嘗試將MainLayout的Text設置為“”:

LayoutInflater temporaryInflater = (LayoutInflater) myActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View temporaryRootView = temporaryInflater.inflate(R.layout.myLayout, (ViewGroup) gv.getParent(), false);
        EditText temporaryEditText = (EditText) temporaryRootView.findViewById(R.id.myEdtiText);
        temporaryEditText.setText("");

這應該可以正常工作,我嘗試更改UIThread上的視圖,我提供了一個帶有rootViewGroup父級的正確RootView以提供LayoutParams,但是什么也沒有發生。 EditText與舊的Text保持在一起。

有什么建議么? 將不勝感激。

關於Rohan550的評論/提示的更新

我將嘗試您的解決方案,但並非完全相同(僅用於理解):

public void resetEditText() {
    EditText temporaryEditText = (EditText) MyActivity.this.findViewById(R.id.myEditText);
    temporaryEditText.setText("");
}

我在onProgressUpdate中的Activity和UIThread的AsyncTask中調用:

MyActivity wusch = new MyActivity();
wusch.resetEditText();

但是它將在resetEditText方法中的findViewById處引發NPE。

為什么?

嘗試更改:temporalSearchbar.setText(“”); 臨時文件.setText(“”);

如果添加一些庫,則AsyncTask可能會損壞。 例如admob或flurry。 不要假設onProgressUpdate將在uithread中,即使它的功能是那樣。 因此,請嘗試使用Handler。 在onCreate中創建一個處理程序,然后在onProgressUpdate使用它。

Handler h = new Handler(); // in activity on create

然后

@Override
protected void onProgressUpdate(Void... values){
h.post(new Runnable() {
            @Override
            public void run() {
                // do ui operations
            }
        });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM