我正在尝试使用RxAndroid执行与AsyncTask完全相同的工作。我编写了一个简单的例子,我在edittext中输入一个n整数,当我按下时 一个按钮,我使用“SystemClock.sleep”让hread进入睡眠状态3秒,当线程处于休眠状态时,我显示一个进度条,当三秒钟 已经过了进度条,并且在edittext中输入的值将乘以10并显示在文本视图中。
通常,当我使用AsyncTask时,我会在onPreExecute中显示进度条并使其在onPostExecute中消失。但是当我运行以下代码时,我收到了 以下错误:
Only the original thread that created a view hierarchy can touch its views.
如何在RxAndroid中正确处理它?</ p>
码:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private EditText mEditTextValueToProcess = null;
private Button mButtonGoAsynchronous = null;
private TextView mTextViewProcessedValue = null;
private ProgressBar mProgressBar = null;
private rx.Observable<String> mAsyncObservable = null;
Subscriber mAsyncSubscriber = new Subscriber<String>() {
@Override
public void onCompleted() {
Log.w(TAG, "onCompleted");
mProgressBar.setVisibility(View.GONE);
}
@Override
public void onError(Throwable e) {
Log.w(TAG, "onError: " + e.getMessage().toString());
mProgressBar.setVisibility(View.GONE);
}
@Override
public void onNext(String o) {
Log.w(TAG, "onNext:->after processing " + o);
mTextViewProcessedValue.setText(o);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intiViews();
mAsyncObservable = rx.Observable.create(new rx.Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mProgressBar.setVisibility(View.VISIBLE);
SystemClock.sleep(3000);
subscriber.onNext("" + Integer.valueOf(mEditTextValueToProcess.getText().toString()) * 10);
subscriber.onCompleted();
}
});
}
private void intiViews() {
mEditTextValueToProcess = (EditText) findViewById(R.id.edittext);
mButtonGoAsynchronous = (Button) findViewById(R.id.button_go_asynchronous);
mTextViewProcessedValue = (TextView) findViewById(R.id.textview_processed_value);
//mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mButtonGoAsynchronous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAsyncObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mAsyncSubscriber);
}
});
}
}
答案 0 :(得分:0)
你试过吗
private Observable<String> createObservable(final int number) {
return rx.Observable.create(new rx.Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
SystemClock.sleep(3000);
subscriber.onNext("" + number * 10);
subscriber.onCompleted();
}
});
}
private void intiViews() {
mEditTextValueToProcess = (EditText) findViewById(R.id.edittext);
mButtonGoAsynchronous = (Button) findViewById(R.id.button_go_asynchronous);
mTextViewProcessedValue = (TextView) findViewById(R.id.textview_processed_value);
mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mButtonGoAsynchronous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mProgressBar.setVisibility(View.VISIBLE);
createObservable(Integer.valueOf(mEditTextValueToProcess.getText().toString()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext((ignored) -> {
mProgressBar.setVisibility(View.GONE);
})
.subscribe(mAsyncSubscriber);
}
});
}
答案 1 :(得分:0)
你需要改变一些事情才能实现这一目标。查看mAsyncSubscriber
初始化,onStart()方法&amp; progressBar初始化。您收到错误的原因是您正在初始化&amp;使您的进度条在另一个线程中可见,然后是您的UI线程(因为您指定了这个 - &gt; subscribeOn(Schedulers.io()
)。
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private EditText mEditTextValueToProcess = null;
private Button mButtonGoAsynchronous = null;
private TextView mTextViewProcessedValue = null;
private ProgressBar mProgressBar = null;
private rx.Observable<String> mAsyncObservable = null;
Subscriber mAsyncSubscriber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intiViews();
mAsyncSubscriber = new Subscriber<String>() {
// Override onStart method
@Override
public void onStart() {
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onCompleted() {
Log.w(TAG, "onCompleted");
mProgressBar.setVisibility(View.GONE);
}
@Override
public void onError(Throwable e) {
Log.w(TAG, "onError: " + e.getMessage().toString());
mProgressBar.setVisibility(View.GONE);
}
@Override
public void onNext(String o) {
Log.w(TAG, "onNext:->after processing " + o);
mTextViewProcessedValue.setText(o);
}
};
mAsyncObservable = rx.Observable.create(new rx.Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
SystemClock.sleep(3000);
subscriber.onNext("" + Integer.valueOf(mEditTextValueToProcess.getText().toString()) * 10);
subscriber.onCompleted();
}
});
}
private void intiViews() {
// Init your progressbar
mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mEditTextValueToProcess = (EditText) findViewById(R.id.edittext);
mButtonGoAsynchronous = (Button) findViewById(R.id.button_go_asynchronous);
mTextViewProcessedValue = (TextView) findViewById(R.id.textview_processed_value);
//mProgressBar = (ProgressBar) findViewById(R.id.progressDialog);
mButtonGoAsynchronous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAsyncObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mAsyncSubscriber);
}
});
}