运行Fragment Transaction会在应用程序打开后立即停止

时间:2018-07-10 08:44:45

标签: android android-fragments

如果我在事务中删除了.commit(),则该应用会编译但显示空白屏幕。如果我添加.commit(),则该应用会立即打开和关闭。我是碎片的新手,任何人都可以帮助解决此问题。我只是添加了一个ImageView布局。在Fragment类中插入它并对其进行硬编码。此外,我只是在MainActivity中创建了该Fragment的实例。

我的Android Studio版本为:3.1.3

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="@+id/head_container"
/>


</android.support.constraint.ConstraintLayout>

body_part_fragment.xml

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

</ImageView>

BodyPartFragment.java

 package com.example.musa.my_frag_app;

 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;

public class BodyPartFragment extends Fragment {

public BodyPartFragment(){
    //Do Nothing
}

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

    View rootView = inflater.inflate(R.layout.body_part_fragment, container);
    ImageView body_image = rootView.findViewById(R.id.body_part_frag);
    body_image.setImageResource(R.drawable.ic_launcher_background);

  return rootView;
}

}

MainActivity.java

  package com.example.musa.my_frag_app;

  import android.support.v4.app.FragmentManager;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;

  public class MainActivity extends AppCompatActivity {

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

    BodyPartFragment headfrag = new BodyPartFragment();
    FragmentManager frag_m = getSupportFragmentManager();
    frag_m.beginTransaction().add(R.id.head_container,headfrag).commit();
    //frag_m.executePendingTransactions();
}
}

5 个答案:

答案 0 :(得分:5)

inflater.inflate(R.layout.body_part_fragment, container);肯定会引发异常。 LayoutInflator.inflate()的默认值为attachToRoot=true。在false中从onCreateView调用时,它应该为Fragment,因为这可以为您处理。更改为inflater.inflate(R.layout.body_part_fragment, container, false);

答案 1 :(得分:2)

为attachToRoot false添加参数

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.body_part_fragment, container, false);
    TextView body_text = rootView.findViewById(R.id.body_part_frag);
    body_text.setText("Hello World");
    return rootView;
}

答案 2 :(得分:0)

对此片段主体部分尝试一下:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ImageView 
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            android:id="@+id/body_part_frag"/>
</android.support.constraint.ConstraintLayout>

答案 3 :(得分:0)

尝试这样:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BodyPartFragment headfrag = new BodyPartFragment();
    fragmentTransaction.replace(R.id.head_container, headfrag);
    fragmentTransaction.commit();

答案 4 :(得分:0)

更改

查看rootView = inflater.inflate(R.layout.body_part_fragment,container);

查看rootView = inflater.inflate(R.layout.body_part_fragment,null);

相关问题