title: Use Observer in Fragment
body: <p>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:</p>
  
  <pre><code>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));
  
  }
  </code></pre>
  
  <p>My fragment code:</p>
  
  <pre><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);
  }
  </code></pre>
  
  <p>My DataObservable is in my class ApplicationContext (extending Application):</p>
  
  <pre><code>public class ApplicationContext extends Application {
  
  private DataObservable _mData;
  
  @Override
  public void onCreate() {
      super.onCreate();
      _mData = new DataObservable();
  }
  
  public DataObservable getObservable() {
      return _mData;
  }
  </code></pre>
  
  <p>MyTextViewObserver have to be notified when the DataObservable change, and in result setText in the Fragment.
  This is the error given by the LogCat:</p>
  
  <pre><code>android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  </code></pre>
  
  <p>Thx</p>