【问题标题】:Android Fragment crashing on orientation changeAndroid Fragment 在方向更改时崩溃
【发布时间】:2017-04-03 14:23:58
【问题描述】:

我正在尝试添加一个简单的片段,当我使用它时它工作得很好,直到我切换旋转而不是崩溃。

这是我的代码:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_user);

    //region Fragment
    if(savedInstanceState==null){
    FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment_Titlebar fragment = new Fragment_Titlebar();
        fragmentTransaction.add(R.id.fragment, fragment);
        fragmentTransaction.commit();
        Fragment_Titlebar header = (Fragment_Titlebar) getFragmentManager().findFragmentById(R.id.fragment);
        header.initFragment(R.string.SignUp, 0, true, true);}
    //endregion

    voornaam = (EditText)findViewById(R.id.first_name_text);
    achternaam = (EditText)findViewById(R.id.last_name_text);
    profile = (CircleImageView)findViewById(R.id.profile_image);

    try {
        //Getting the google account
        Intent intent = getIntent();
        if (intent.getExtras().get("User") instanceof GoogleSignInAccount) {

            GoogleSignInAccount acct = (GoogleSignInAccount) intent.getExtras().get("User");
            voornaam.setText(acct.getGivenName());
            achternaam.setText(acct.getFamilyName());
            if (acct.getPhotoUrl() != null) {
                Picasso.with(this).load(acct.getPhotoUrl()).into(profile);

            }
        }
    }
    catch (NullPointerException e)
    {

    }
}}

还有片段:

private TextView headerText;
private ImageButton backButton;
private ImageButton homeButton;
private Toolbar background;

public Fragment_Titlebar(){}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_title_bar, container, false);


    backButton = (ImageButton) view.findViewById(R.id.backButton);
    backButton.setOnClickListener(
            new View.OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    goBack(v);
                }
            });

    homeButton = (ImageButton) view.findViewById(R.id.homeButton);
    homeButton.setOnClickListener(
            new View.OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    goHome(v);
                }
            }
    );

    headerText = (TextView) view.findViewById(R.id.headerText);
    background = (Toolbar) view.findViewById(R.id.background);

    return view;
}

终于报错了:

致命异常:主要 进程:rickvanfessem.carcoolandroid,PID:18652 java.lang.RuntimeException:无法启动活动 ComponentInfo{rickvanfessem.carcoolandroid/rickvanfessem.carcoolandroid.AddUser}:android.view.InflateException:二进制 XML 文件第 10 行:二进制 XML 文件第 10 行:膨胀类片段时出错

我已经尝试过这样做setRetainInstance(true),但没有成功。

请帮忙

请求 XML 后编辑 片段的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    xmlns:android="http://schemas.android.com/apk/res/android">
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/background"
    android:background="@color/HeaderRed"
    app:theme="@style/AppTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageButton
            android:id="@+id/backButton"
            style="@style/image_style"
            android:layout_alignParentLeft="true"
            android:src="@drawable/ic_arrow_back"

            />

        <TextView
            android:id="@+id/headerText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            style="@style/HeaderText"
            tools:text="Test scherm"/>

        <ImageButton
            android:id="@+id/homeButton"
            style="@style/image_style"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_home"
            />


    </RelativeLayout>


</Toolbar>
</LinearLayout>

【问题讨论】:

    标签: android android-fragments screen-rotation


    【解决方案1】:

    试试这个

    编辑您的清单

     <activity
                android:name=".MainActivity"
                android:configChanges="orientation|keyboardHidden|screenSize"
                android:label="@string/app_name"
                />
    

    MainActivity

    添加此覆盖方法

    public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            if (newConfig.orientation== Configuration.ORIENTATION_PORTRAIT)
            {
    
            }
            else  if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
    
            }
        }
    

    【讨论】:

    • 谢谢!你不需要覆盖方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多