【问题标题】:Android Fragment is not added未添加 Android Fragment
【发布时间】:2015-09-04 08:17:56
【问题描述】:

我正在尝试将片段添加到活动中,并且应用程序拒绝膨胀或添加片段布局。该应用程序没有崩溃或任何事情,只是属于活动的空白屏幕

这是活动代码:

public class MovieDetail extends ActionBarActivity {
  String Tag = "TAG";

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

    BlankFragment fragment = new BlankFragment();
    getSupportFragmentManager().beginTransaction().add(fragment, Tag).commit();

    this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  }

这里是片段类

public class BlankFragment extends Fragment {


public BlankFragment() {
    setHasOptionsMenu(true);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_blank, container, false);
    return root;
 }

对于布局

活动 xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/weather_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ahmad.popularmovies01.Activities.MovieDetail"
tools:ignore="MergeRootFrame"/>

和片段代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

... etc

【问题讨论】:

  • 你可以尝试使用 add(R.id.weather_detail_container, fragment,tag);
  • 对。片段没有添加到容器中,仅此而已...

标签: android android-fragments android-activity fragment


【解决方案1】:
getSupportFragmentManager().beginTransaction().
          add(0,fragment, Tag).commit();

getSupportFragmentManager().beginTransaction().
          add( fragment,Tag).commit();

这里你的 Id 是 0,所以它只是添加到 Activity 中,但没有与任何视图绑定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多