App crashes due to TextView in Android

541 Views Asked by At

My app crashes due to a TextView. It works as long as the device doesn't change its orientation. If I remove the TextView and rotate the device everything works fine but with the TextView it doesn't.
I have 2 layouts for portrait and layout-land.
They look like that:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextureView
        android:id="@+id/texture"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:layout_width="220dp"
        android:layout_height="50dp"
        android:id="@+id/text_time"
        android:textSize="20dp"
        android:textColor="@color/red"
        android:layout_marginTop="14dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

And some code:

public void onViewCreated(final View view, Bundle savedInstanceState) {
        mTextView = (TextView) view.findViewById(R.id.text_time);
        mTextView.setVisibility(View.INVISIBLE);
}

then in another method I set the text:

mTextView.setVisibility(View.VISIBLE);
mTextView.setText("Text");

and that's it. So do you have any suggestions why that doesn't work?

I get the following error:

01-07 17:14:28.117 12890-12890/k.com.kamera E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: k.com.kamera, PID: 12890
                                                                   java.lang.IllegalStateException: Fragment BasicFragmentVideo{e87fd0c} not attached to Activity
                                                                       at android.app.Fragment.getResources(Fragment.java:805)
                                                                       at android.app.Fragment.getString(Fragment.java:840)
                                                                       at k.com.kamera.BasicFragmentVideo$5$1$1.run(BasicFragmentVideo.java:483)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:168)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5845)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
2

There are 2 best solutions below

7
rafsanahmad007 On

Each time orientation changes, you're creating a new instance of Fragment. And only one of them is actually attached to the Activity. Try this

 public class TestActivity extends Activity {

    TestFragment testFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);
        attachFragment(savedInstanceState);
    }

    private void attachFragment(Bundle savedInstanceState) {
        if (savedInstanceState == null){
           testFragment = new TestFragment();
           getFragmentManager().beginTransaction()
           .replace(R.id.camera_frame, testFragment,     "testFragment").commitAllowingStateLoss();
    } else {
      testFragment =  (TestFragment)getFragmentManager().findFragmentByTag("testFragment");
    }
}

call setRetainInstance(true) in your fragment's onCreateView() or onActivityCreated()

1
sohan shetty On

Use setRetainInstance as true in fragment this will retain the fragment instance when orientation happens and now in Acitivty inside onCreate check for your fragment instance and add