简体   繁体   English

应用程序因Android中的TextView而崩溃

[英]App crashes due to TextView in Android

My app crashes due to a TextView . 我的应用程序因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. 如果我删除TextView并旋转设备一切正常,但使用TextView它没有。
I have 2 layouts for portrait and layout-land. 我有2个纵向和布局的布局。
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)

Each time orientation changes, you're creating a new instance of Fragment. 每次方向改变时,您都在创建一个新的Fragment实例。 And only one of them is actually attached to the Activity. 并且其中只有一个实际上附加到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 () 在片段的onCreateView ()或onActivityCreated ()中调用setRetainInstance(true)

在片段中使用setRetainInstance为true,这将在方向发生时保留片段实例,现在在Acitivty里面onCreate检查片段实例并添加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM