1

I am testing basic Fragements stuff, so i created 4 classes , 1 MainActivity, 1 FragmentActivity and 2 Fragments, the idea is that we invoke the FragmentActivity via a button from the MainActivity, which should show then Fragment1 and Fragment2 , as Fragment1 having 30 % of the screen with orrange background and Fragment 2 the 70 % with background blue.There is no error , just when i click the button in the MainActivity , it goes to the FragmentActivity , but shows white screen with only the textview , but when i go into the .xml file on the FragmentActivity it shows the Orange-Blue screen ?

here is the MainActivity part

    buttonFrag2.setOnClickListener(new Button.OnClickListener() {


    public void onClick(View v) {


        Intent intent = new Intent(MainActivity.this,StaticFragment.class);
         startActivity(intent); 
                 }

           }); 

then the StaticFragment Class

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

public class StaticFragment extends FragmentActivity {
@Override
protected void onCreate(Bundle bundle) {


    super.onCreate(bundle);
        }

}

and the static_fragment.xml , which as said is shown correctly in the Graphical Layout

<LinearLayout 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"
android:orientation="horizontal" 
android:baselineAligned="false"
>


<fragment

    android:name="com.example.fragment.ListFrag"
    android:id="@+id/listFrag"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight=".3"
    tools:layout="@layout/list_frag" />

<fragment

    android:name="com.example.fragment.DetailFrag"
    android:id="@+id/detailFrag"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight=".7"
    tools:layout="@layout/detail_frag" />

</LinearLayout> 

also as an example one of the Fragment classes (they are identical)

package com.example.fragment;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ListFrag extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.list_frag, container, false);
}

}

1 Answer 1

2

Your not setting the content view of your new activity. You may be pushing to StaticFragment Activity, but you don't load the xml of the view. After the super.onCreate() of your StaticFragmentActivity, add setContentView(R.layout.static_fragment.xml);

Should fix the issue.

1
  • thats it :), i was taking that from online course , where they didnt use setContentView , but i guess they were passing it in a different way Commented Aug 13, 2013 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.