2015-09-04 75 views
1

我正在尝试向活动添加片段,并且该应用会拒绝膨胀或添加片段布局。该应用程序是不是崩溃或什么,只是属于活动未添加Android片段

这里一个空白屏幕是活动代码:

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 
+4

你可以尝试使用add(R.id.weather_detail_container,fragment,tag); – k0sh

+0

对。该片段没有添加到容器中,这就是所有... – FlanschiFox

+0

非常感谢:) –

回答

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

一样

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

这里你的ID为0,所以它只是加入活动,但不与任何视图绑定。