2016-05-13 109 views
2

我試圖將一個片段加載爲活動的默認視圖,但我只是得到一個空白屏幕和巨大的內存泄漏。活動啓動時的默認片段

活動文件onCreate方法:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_login); 

    if (savedInstanceState == null) { 
     FragmentManager fragmentManager = getFragmentManager(); 
     int fragmentTransaction = fragmentManager.beginTransaction() 
       .add(R.id.login_screen_fragment, new FragmentLogin()) 
       .commit(); 
    } 
} 

的XML爲活動:

<FrameLayout 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=".LoginActivity"> 

    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/login_screen_fragment" 
     class="com.test.project.FragmentLogin" 
    /> 

    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/forgotten_password_fragment" 
     class="com.test.project.FragmentForgottenPassword" 
    /> 
</FrameLayout> 

,將片段類(相關部分):

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

    final View view = inflater.inflate(R.layout.fragment_login, container, false); 
    final Activity activity = getActivity(); 

    ... code ... 

    return view; 
} 

我跟着從指令從一個不同的問題中引導某人提出的建議,但我必須做某些事情可怕的錯誤。有任何想法嗎?

+0

當你定義在佈局''元素,你並不需要在你的代碼動態加載的。 –

+0

@MikeM。我在哪裏做這個? – mwieczorek

+0

「if」塊是動態實例化和處理FragmentLogin實例的。 –

回答

0

只需用FrameLayout替換片段元素,然後你就可以看到你的片段。

0

如果您想以編程方式添加碎片,然後刪除您的Framelayout中的<Fragment>並添加一些ID。

它應該是這樣的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/login_screen_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".LoginActivity" /> 
2

xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/changeFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".LoginActivity"> 
</FrameLayout> 

您的活動

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_login); 

if (savedInstanceState == null) { 
    FragmentLogin f1= new FragmentLogin(); 
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
    fragmentTransaction.add(R.id.changeFragment, f1); 
    fragmentTransaction.commit(); 

    } 
} 
1

我是新來的Android和我跑進同樣的問題。這是我如何修復我的。

因此,在您的onCreate方法中,使用FragmentManager類中的getSupportFragmentManager(),然後分別聲明FragmentTransaction。見下文。我爲我的情況做了這件事,它的工作。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_login); 

if (savedInstanceState == null) { 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.login_screen_fragment, new FragmentLogin()); 
    fragmentTransaction.commit(); 
} 

希望這有助於解決您的問題。

真誠, sylvery