I am trying fragments for the first time. I have read all the developer documents but still I am unable to figure out why my fragment is not attaching to the main activity. following is my code
I am only getting my main activity's layout. No fragment's layout
the main activity xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.attendancerecord.HomePage"
tools:ignore="MergeRootFrame">
<HorizontalScrollView
android:id="@+id/scroll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="200dp">
</HorizontalScrollView>
</RelativeLayout>
my main.java file
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
HomePage_Fragment myfragment = new HomePage_Fragment();
ft.add(R.id.scroll2, myfragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
my fragment.java file
public class HomePage_Fragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase db = getActivity().openOrCreateDatabase("DATABASE",android.content.Context.MODE_PRIVATE ,null);
db.execSQL("CREATE TABLE IF NOT EXISTS ATTENDANCE_RECORD(NAME VARCHAR, ATTENDED integer, " +
"TOTAL integer, BUNKS integer, PERCENTAGE float)");
for(int i=0;i<num_subjects;i++){ //initializing of num_subjects is done (long code)
db.execSQL("INSERT INTO ATTENDANCE_RECORD VALUES( '"+name_subjects[i]+"', "+0+","+0+", "+0+", "+0.0+" )");
}
}
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstance){
View view= inflater.inflate(R.layout.fragment_home_page,null);
TableLayout tableLayout = (TableLayout)view.findViewById(R.id.record);
TableRow tableRow;
TextView textView;
SQLiteDatabase db = getActivity().openOrCreateDatabase("DATABASE",android.content.Context.MODE_PRIVATE ,null);
Cursor c=db.rawQuery("SELECT * FROM ATTENDANCE_RECORD", null);
c.moveToFirst();
for(int i=0;i<num_subjects;i++){
tableRow = new TableRow(getActivity());
for(int j=0;j<5;j++){
textView = new TextView(getActivity());
switch(j){
case 0: textView.setText(c.getString(c.getColumnIndex("NAME")));
break;
case 1: textView.setText(String.valueOf(c.getString(c.getColumnIndex("ATTENDED"))));
break;
case 2: textView.setText(String.valueOf(c.getInt(c.getColumnIndex("TOTAL"))));
break;
case 3: textView.setText(String.valueOf(c.getInt(c.getColumnIndex("BUNKS"))));
break;
case 4: textView.setText(String.valueOf(c.getFloat(c.getColumnIndex("PERCENTAGE"))));
break;
}
textView.setPadding(15, 25, 15, 25);
tableRow.addView(textView, new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT, 0));
}
c.moveToNext();
tableLayout.addView(tableRow);
}
c.close();
return inflater.inflate(R.layout.fragment_home_page,container,false);
}
}
My fragment_home_page.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.attendancerecord.HomePage$PlaceholderFragment" >
<TableLayout
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</TableLayout>
</RelativeLayout>
In Activity
onCreate()
,
if(saveInstanceState == null)
{
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
HomePage_Fragment myfragment = new HomePage_Fragment();
ft.add(R.id.scroll2, myfragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
In Fragment onCreateView()
,
View view = inflater.inflate(R.layout.fragment_home_page, container, false);
The last line of onCreateView() is currently incorrect, use
return view;
And I'm not sure if the HorizonalScrollView actually qualifies as a Container, I think you need to embed a FrameLayout into it and use THAT as the FragmentContainer.
One issue I have spotted is that you are returning a newly inflated View
from your onCreateView()
method, rather than the View
that you've already created earlier and have been working with.
To fix this, simply return view;
instead of your current return statement.
Also, you are also using wrap_content
for your height and width of your main Activity
layout elements when the Fragment
you are creating might not have a height and width. As such you might never see anything. I would suggest using match_parent
for the two elements in your main activity instead.
I hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.