本文介绍了在碎片中使用观察者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在片段中使用观察者时出现问题,我已经实现了自己的TextViewObserver,并且我不想将其插入到片段中:



<$公共类TextViewObserver扩展TextView实现Observer {

public TextViewObserver(Context context,AttributeSet attrs,int defStyle){
super(context,attrs,defStyle );


public TextViewObserver(Context context,AttributeSet attrs){
super(context,attrs);
}

public TextViewObserver(Context context){
super(context);
}

@Override
public void update(java.util.Observable o,Object arg){
// TODO自动生成的方法存根
this.setText(将String.valueOf(ARG));

}

我的片段代码:

  public class MyFragment extends Fragment {
private TextViewObserver mTextView; // = new TextView(this.getActivity());
private ApplicationContext mContext;
私有DataObservable mDataObservable;

@Override
public view onCreateView(LayoutInflater inflater,ViewGroup container,
Bundled savedInstanceState){

View view = inflater.inflate(R.layout .fragment_6,容器,false);
mTextView =(TextViewObserver)view.findViewById(R.id.sicthtxtview);
返回视图;
}

@Override
public void onAttach(Activity activity){
super.onAttach(activity);
mContext =(ApplicationContext)activity.getApplicationContext();
mDataObservable = mContext.getObservable();



@Override
public void onStart(){
super.onStart();
mDataObservable.addObserver(mTextView);
}

我的DataObservable在我的类ApplicationContext(extends Application)中:

  public class ApplicationContext extends Application {

private DataObservable _mData;

@Override
public void onCreate(){
super.onCreate();
_mData = new DataObservable();
}

public DataObservable getObservable(){
return _mData;
}

当DataObservable发生更改时,MyTextViewObserver必须被通知,并且在结果setText中分段。
这是LogCat给出的错误:

  android.view.ViewRootImpl $ CalledFromWrongThreadException:只有原始线程创建一个视图层次结构可以触及其视图。 

Thx

解决方案在Android中,您只能在UI线程中操作 View s。你可以使用 View.post()在UI线程上运行一些代码,如下所示:

  @Override 
public void update(java.util.Observable o,final Object arg){
this.post(new Runnable(){
public void run ){
this.setText(String.valueOf(arg));
}
}
}


I've a problem when using an observer in a fragment, I've implemented my own TextViewObserver, and I wan't to insert it in a Fragment:

public class TextViewObserver extends TextView implements Observer {

public TextViewObserver(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public TextViewObserver(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TextViewObserver(Context context) {
    super(context);
}

@Override
public void update(java.util.Observable o, Object arg) {
    // TODO Auto-generated method stub
    this.setText(String.valueOf(arg));

}

My fragment code:

public class MyFragment extends Fragment {
private TextViewObserver mTextView;// = new TextView(this.getActivity());
private ApplicationContext mContext;
private DataObservable mDataObservable;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_6, container, false);
    mTextView = (TextViewObserver) view.findViewById(R.id.sicthtxtview);
    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = (ApplicationContext) activity.getApplicationContext();
    mDataObservable = mContext.getObservable();

}

@Override
public void onStart() {
    super.onStart();
    mDataObservable.addObserver(mTextView);
}

My DataObservable is in my class ApplicationContext (extending Application):

public class ApplicationContext extends Application {

private DataObservable _mData;

@Override
public void onCreate() {
    super.onCreate();
    _mData = new DataObservable();
}

public DataObservable getObservable() {
    return _mData;
}

MyTextViewObserver have to be notified when the DataObservable change, and in result setText in the Fragment.This is the error given by the LogCat:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Thx

解决方案

In Android, you can only manipulate Views from the UI thread. You can run some code on the UI thread by using View.post(), like this:

@Override
public void update(java.util.Observable o, final Object arg) {
    this.post(new Runnable() {
        public void run() {
            this.setText(String.valueOf(arg));
        }
    }
}

http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable)

这篇关于在碎片中使用观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 19:48