2017-03-05 6 views
2

アプリケーションが最初に起動されたときにメインアクティビティのフラグメントが表示されないアプリケーションのonCreateメソッド)が、私はアプリを一時停止し、その後onResumeメソッド中に(それを再開したときに表示さん)アプリケーションの開始時にフラグメントが表示されない(onCreate)が再開するときに(onResume)

ここに私の主な活動の重要な部分に activity_main.xmlです:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" />    

    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </FrameLayout> 

MainActivity:

public class MainActivity extends Activity { 

    protected FrameLayout fragment_container; 

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

     if (findViewById(R.id.fragment_container) != null) { 
      if (savedInstanceState != null) { 
       TextViewFragment fragment = new TextViewFragment(); 
       fragment.setArguments(getIntent().getExtras()); 

       getFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit(); 
      } 
     }} 

TextViewFragment:

public class TextViewFragment extends Fragment { 

    @Override 
    public void onCreate(Bundle savedInstance) { 
     super.onCreate(savedInstance); 
    } 

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

} 

textview_fragment:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:text="This is not showing up" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:typeface="sans" 
     android:textSize="35sp"/> 
</LinearLayout> 

すべてのヘルプは歓迎です!

答えて

2

最初に起動すると、(savedInstanceState!= null){条件がnullになります。

条件を削除すると、フラグメントが常にロードされます。

+0

あなたはルーキーミスをしてしまった – user2929779

関連する問題