Why fragment layout cannot be displayed? - android

What have I missed out here? I wanted to open edit_information fragment from edit activity. It can display the Toast but not displaying the edit_information layout.
Edit
listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding listview_item_row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this listview_item_row in the database.
String ID =
cursor.getString(cursor.getColumnIndexOrThrow("_id"));
EditInformation fragment2 = new EditInformation();
Bundle bundle = new Bundle();
bundle.putString("ID", ID);
fragment2.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
EditInformation
public class EditInformation extends Fragment {
String ID;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View edit_info = inflater.inflate(R.layout.edit_information, container, false);
Bundle bundle = this.getArguments();
if(getArguments()!=null)
{
ID=bundle.getString("ID");
}
Toast.makeText(getActivity(),"Edit_Information",Toast.LENGTH_LONG).show();
return edit_info;
}
}
edit
<?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"
android:id="#+id/fragment1">
<RelativeLayout
android:layout_width="209dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/tomato"
android:paddingLeft="10sp"
android:textSize="20sp"
android:paddingTop="10sp"
android:hint="Long Press to delete"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/tomato"
android:paddingTop="40sp"
android:textSize="20sp"
android:paddingLeft="10sp"
android:hint="Hint to edit"/>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listViewEdit"
android:paddingTop="10sp"/>
</LinearLayout>
edit_information
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10sp"
android:orientation="vertical"
android:background="#color/beige">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Name"
android:textColor="#color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Name"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<Spinner
android:layout_width="339dp"
android:layout_height="52dp"
android:id="#+id/SpinnerName" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:id="#+id/Weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Weather"
android:textColor="#color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Weather"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<Spinner
android:layout_width="342dp"
android:layout_height="52dp"
android:id="#+id/SpinnerWeather">
</Spinner>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Date"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Date"
android:textColor="#color/tomato"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Date"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<EditText
android:layout_width="311dp"
android:layout_height="52dp"
android:layout_margin="10sp"
android:background="#drawable/round_corner_square"
android:id="#+id/date"
android:paddingLeft="20dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Status"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Status"
android:textColor="#color/tomato"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Status"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<Spinner
android:layout_width="339dp"
android:layout_height="52dp"
android:id="#+id/SpinnerStatus">
</Spinner>
</LinearLayout>
</RelativeLayout>
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20sp"
android:orientation="horizontal">
<Button
android:layout_width="187dp"
android:layout_weight="0.83"
android:text="Add Work Force"
android:textSize="20sp"
android:drawableRight="#mipmap/add_workforce"
android:layout_height="75dp"
android:id="#+id/AddForce"
android:layout_x="12dp"
android:layout_y="11dp" />
<Button
android:layout_width="123dp"
android:layout_weight="0.83"
android:text="Next"
android:textSize="20sp"
android:drawableRight="#mipmap/next"
android:layout_height="71dp"
android:id="#+id/Next"
android:layout_x="211dp"
android:layout_y="14dp" />
</AbsoluteLayout>
</LinearLayout>

Try using FrameLayout as a container for fragment instead of LinearLayout. I believe because of LinearLayout your fragment is getting created out of screen. where as FrameLayout will overlap your fragment on other Views.

Related

How to click button inside listview

I know this problem already asked many times but i still can't get effective way for solving this. I try for use ListView with clickable/editable widget and Android : How to set onClick event for Button in List item of ListView as reference but the result is i need click button several time for executing my program. Here my program
LeadActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LeadSalesActivity">
<LinearLayout
android:background="#E9ECEB"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/b_menusamping2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="#drawable/menu_button"
android:backgroundTint="#27D01B"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome,"
android:textSize="20sp"
android:textStyle="italic"
android:textColor="#android:color/black"/>
<TextView
android:id="#+id/t_username3"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Rizaldi"
android:textColor="#android:color/black"
android:textSize="25sp"
android:textStyle="bold"
android:fontFamily="#font/action_man_bold"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear_refresh2"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white">
<ImageView
android:backgroundTint="#27D01B"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="#drawable/icon_update"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="("
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:textStyle="bold"
android:id="#+id/count_lead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=")"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/btn_add"
android:layout_marginRight="20dp"
android:backgroundTint="#android:color/holo_blue_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tambah Lead"
android:textAllCaps="false"/>
</LinearLayout>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tanggal"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telp"
android:textColor="#android:color/black"
android:textSize="20sp"/>
</LinearLayout>
<ListView
android:id="#+id/listviewlead"
tools:listitem="#layout/c_lead"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/back6"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
</LinearLayout>
c_lead.xml
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_tgl"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Tgl"
android:textSize="15sp"
android:textStyle="italic"
android:textColor="#000000" />
<TextView
android:id="#+id/t_lead4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Lead"
android:textSize="15sp"
android:textColor="#000000" />
<TextView
android:id="#+id/t_numberphone"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="right"
android:text="No.Telp"
android:textSize="15sp"
android:textColor="#android:color/black"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/pencil_icon"/>
</LinearLayout>
<LinearLayout
android:id="#+id/btn_delete"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/delete_icon"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_alamat"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Alamat"
android:textColor="#android:color/black"
android:textSize="15sp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
MyAdapter
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.c_lead, null);
}
final HashMap<String, Object> hashMap = _data.get(_position);
final TextView t_tanggal = (TextView) _view.findViewById(R.id.t_tgl);
final TextView t_lead = (TextView) _view.findViewById(R.id.t_lead4);
final TextView t_alamat = (TextView) _view.findViewById(R.id.t_alamat);
final TextView t_numberphone = (TextView) _view.findViewById(R.id.t_numberphone);
final LinearLayout btn_edit = (LinearLayout) _view.findViewById(R.id.btn_edit);
final LinearLayout btn_delete = (LinearLayout) _view.findViewById(R.id.btn_delete);
String dateString = _data.get(_position).get("create_date").toString().replace("-","");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmm");
format1.setTimeZone(TimeZone.getTimeZone("GMT+7"));
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = format1.parse(dateString);
String dateFinal = format2.format(date);
t_tanggal.setText(String.valueOf((long)(_position + 1)).concat(".").concat(dateFinal));
} catch (ParseException e) {
e.printStackTrace();
}
t_lead.setText(_data.get((int)_position).get("type").toString());
t_alamat.setText(_data.get((int)_position).get("street").toString());
if (_data.get((int)_position).get("mobile").toString().equals(true)){
t_numberphone.setText("Tanpa Nomor");
} else {
t_numberphone.setText(_data.get((int)_position).get("phone").toString());
}
btn_edit.setFocusable(false);
btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i.setClass(getApplicationContext(), AddLeadSalesActivity.class);
startActivity(i);
}
});
btn_delete.setFocusable(false);
btn_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Data Tidak Bisa Dihapus", Toast.LENGTH_SHORT).show();
}
});
return _view;
}
Is there any mistake inside my program? How to solve this?

Object reference not set to an instance of an object when trying to populate Xamarin Android TextView on fragmenrt from arrary

I'm getting the error System.NullReferenceException: Object reference not set to an instance of an object when I try to reference a TextView from my fragment. It happens at runtime.
Fragment
public class Fragment1 : Fragment
{
private int checkNumber = 0;
private string[] updateTitleArray = new string[10];
private string[] updateBodyArray = new string[10];
private TextView updatesTitle0;
private TextView updatesTitle1;
private TextView updatesTitle2;
private TextView updatesTitle3;
private TextView updatesTitle4;
private TextView updatesTitle5;
private TextView updatesTitle6;
private TextView updatesTitle7;
private TextView updatesTitle8;
private TextView updatesTitle9;
private TextView updateBody0;
private TextView updateBody1;
private TextView updateBody2;
private TextView updateBody3;
private TextView updateBody4;
private TextView updateBody5;
private TextView updateBody6;
private TextView updateBody7;
private TextView updateBody8;
private TextView updateBody9;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
fillUpdates(updateTitleArray, updateBodyArray);
}
public static Fragment1 NewInstance()
{
var frag1 = new Fragment1 { Arguments = new Bundle() };
return frag1;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
return inflater.Inflate(Resource.Layout.fragment1, null);
}
public void fillUpdates(string[] updatetitlearray, string[] updatebodyarray)
{
//increment1 so if block skips here
checkNumber = 1;
//fill updatesArray
updateTitleArray = updatetitlearray;
updateBodyArray = updatebodyarray;
updatesTitle0 = View.FindViewById<TextView>(Resource.Id.update_title0);
updatesTitle1 = View.FindViewById<TextView>(Resource.Id.update_title1);
updatesTitle2 = View.FindViewById<TextView>(Resource.Id.update_title2);
updatesTitle3 = View.FindViewById<TextView>(Resource.Id.update_title3);
updatesTitle4 = View.FindViewById<TextView>(Resource.Id.update_title4);
updatesTitle5 = View.FindViewById<TextView>(Resource.Id.update_title5);
updatesTitle6 = View.FindViewById<TextView>(Resource.Id.update_title6);
updatesTitle7 = View.FindViewById<TextView>(Resource.Id.update_title7);
updatesTitle8 = View.FindViewById<TextView>(Resource.Id.update_title8);
updatesTitle9 = View.FindViewById<TextView>(Resource.Id.update_title9);
updateBody0 = View.FindViewById<TextView>(Resource.Id.update_body0);
updateBody1 = View.FindViewById<TextView>(Resource.Id.update_body1);
updateBody2 = View.FindViewById<TextView>(Resource.Id.update_body2);
updateBody3 = View.FindViewById<TextView>(Resource.Id.update_body3);
updateBody4 = View.FindViewById<TextView>(Resource.Id.update_body4);
updateBody5 = View.FindViewById<TextView>(Resource.Id.update_body5);
updateBody6 = View.FindViewById<TextView>(Resource.Id.update_body6);
updateBody7 = View.FindViewById<TextView>(Resource.Id.update_body7);
updateBody8 = View.FindViewById<TextView>(Resource.Id.update_body8);
updateBody9 = View.FindViewById<TextView>(Resource.Id.update_body9);
updatesTitle0.Text = updateTitleArray[0].ToString();
updatesTitle1.Text = updateTitleArray[1].ToString();
updatesTitle2.Text = updateTitleArray[2].ToString();
updatesTitle3.Text = updateTitleArray[3].ToString();
updatesTitle4.Text = updateTitleArray[4].ToString();
updatesTitle5.Text = updateTitleArray[5].ToString();
updatesTitle6.Text = updateTitleArray[6].ToString();
updatesTitle7.Text = updateTitleArray[7].ToString();
updatesTitle8.Text = updateTitleArray[8].ToString();
updatesTitle9.Text = updateTitleArray[9].ToString();
updateBody0.Text = updateBodyArray[0].ToString();
updateBody1.Text = updateBodyArray[1].ToString();
updateBody2.Text = updateBodyArray[2].ToString();
updateBody3.Text = updateBodyArray[3].ToString();
updateBody4.Text = updateBodyArray[4].ToString();
updateBody5.Text = updateBodyArray[5].ToString();
updateBody6.Text = updateBodyArray[6].ToString();
updateBody7.Text = updateBodyArray[7].ToString();
updateBody8.Text = updateBodyArray[8].ToString();
updateBody9.Text = updateBodyArray[9].ToString();
}
}
View
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:fillViewport="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--Cardview 1-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60dp"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shownbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view2-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--cardview 3-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 4-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 5-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 6-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 7-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 8-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 9-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 10-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
I understand that what I am trying to achieve can be implemented with recycler views but I haven't fully understood that, because I am a beginner programmer.
You are calling you function too early. OnCreate gets called before OnCreateView. That means, that your views haven't been created, when you try to access the layout components. You have to move your call of fillUpdates to OnStart (or OnCreateView or another lifecycle method that gets called after OnCreateView).
Have a look at the Frament Lifecycle
public override void OnStart(Bundle bundle)
{
base.OnStart(bundle);
fillUpdates(updateTitleArray, updateBodyArray);
}

onbuttonclick display a hidden fragment

When I click on login button, it says that app is stopped working. But It should be changed to fragment register. Can anyone tell what I should change in following java file ? because I am new for Android, Please kindly help.
**AdminFragment.java**
public class AdminFragment extends Fragment implementsView.OnClickListener {
EditText etUsername,etPassword;
Button bLogin;
public View mRootView;
public AdminFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView = inflater.inflate(R.layout.fragment_admin, container, false);
etUsername=(EditText)mRootView.findViewById(R.id.etUsername);
etPassword=(EditText)mRootView.findViewById(R.id.etPassword);
bLogin=(Button)mRootView.findViewById(R.id.bLogin);
bLogin.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
Fragment fragment=null;
switch (v.getId()){
case R.id.bLogin:
fragment = new RegisterFragment();
replaceFragment(fragment);
break;
}
}
private void replaceFragment(Fragment AdminFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.tab,AdminFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
enter code here
my Admin Xml file
My xml file in admin fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.AdminFragment">
<TextView
android:id="#+id/tvUsername"
android:text="Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvPassword"
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/etPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bLogin"
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
register xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:tools="http://schppemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.RegisterFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="#string/Hospital_Name"
/>
<EditText
android:id="#+id/etHospital"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="City"
/>
<EditText
android:id="#+id/etCity"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Level"
/>
<EditText
android:id="#+id/etLevel"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Complaint_Address"
/>
<EditText
android:id="#+id/etAddress"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Phone"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Street"
/>
<EditText
android:id="#+id/etStreet"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bRegister"
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
My Register java file
public class RegisterFragment extends Fragment implementsView.OnClickListener {
Button bRegister;
EditText etHospital;
public View mRootView;
public RegisterFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView= inflater.inflate(R.layout.fragment_register, container, false);
bRegister=(Button)mRootView.findViewById(R.id.bRegister);
etHospital=(EditText)mRootView.findViewById(R.id.etHospital);
bRegister.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bRegister:
break;
}
}
}
My register xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schppemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.RegisterFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="#string/Hospital_Name"
/>
<EditText
android:id="#+id/etHospital"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="City"
/>
<EditText
android:id="#+id/etCity"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Level"
/>
<EditText
android:id="#+id/etLevel"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Complaint_Address"
/>
<EditText
android:id="#+id/etAddress"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Phone"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Street"
/>
<EditText
android:id="#+id/etStreet"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bRegister"
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>

ANDROID - How to move to other fragment from fragment

Usually, if we want to go to other page. We use code
Intent i = new Intent(getApplicationContext(), Home.class);
startActivity(i);
finish();
I confuse to go to other page with fragment.
TEACHER_Class.java
public class TEACHER_Class extends Fragment {
Button tambahKelasButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.teacher_class, container, false);
tambahKelasButton = (Button) rootView.findViewById(R.id.tambah_kelas_button);
tambahKelasButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Fragment FRAGMENT = new TEACHER_AddClass();
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//I think I get error in here
fragmentTransaction.replace(R.layout.teacher_class, FRAGMENT);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return rootView;
}
}
TEACHER_AddClass.java
public class TEACHER_AddClass extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.teacher_add_class, container, false);
return rootView;
}
}
I want to move from TEACHER_Class.java to TEACHER_AddClass.java
This is teacher_class.xml
<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:gravity="center_horizontal"
android:orientation="vertical" android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin" android:background="#393f46"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/teacher_class_layout"
>
<!-- Login progress -->
<Button android:id="#+id/tambah_kelas_button"
android:layout_width="150dp" android:layout_height="40dp"
android:layout_marginTop="20dp" android:layout_marginLeft="100dp"
android:text="TAMBAH KELAS" android:background="#f8255f"
android:textSize="18sp" android:textColor="#FFFFFF"
android:textStyle="bold" />
<ScrollView android:id="#+id/insert_class_form" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginTop="30dp">
<TableLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#999999"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="3">
<TextView
android:id="#+id/header_class_name"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text=" NAMA KELAS"
android:layout_marginLeft="0dp"
android:layout_marginTop="7.5dp"
android:layout_marginBottom="7.5dp"
android:textSize="18sp"
android:textColor="#f6f6f6"
android:textStyle="bold"
/>
<TextView
android:id="#+id/header_bank_soal"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="BANK SOAL"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:textColor="#f6f6f6"
android:textStyle="bold"
/>
<TextView
android:id="#+id/header_resources"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="RES"
android:layout_marginLeft="30dp"
android:layout_marginTop="7.5dp"
android:layout_marginBottom="7.5dp"
android:textSize="18sp"
android:textColor="#f6f6f6"
android:textStyle="bold"
/>
</TableRow>
</TableLayout>
</ScrollView>
teacher_add_class.xml
<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="vertical" android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin" android:background="#393f46"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/teacher_add_class_layout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD CLASS"
android:textSize="28sp"
android:textColor="#FFFFFF"
android:id="#+id/loginTittle"
android:layout_marginBottom="30dp"
android:textStyle="bold"
android:layout_marginTop="60dp"
/>
<EditText
android:id="#+id/fullname_edittext"
android:layout_width="330dp" android:layout_height="50dp"
android:textSize="18sp" android:textColor="#282727"
android:background="#FFFFFF" android:hint="Full Name"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="#+id/shortname_edittext"
android:layout_width="330dp" android:layout_height="50dp"
android:textSize="18sp" android:textColor="#282727"
android:background="#FFFFFF" android:hint="Short Name"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="#+id/summary_edittext"
android:layout_width="330dp" android:layout_height="50dp"
android:textSize="18sp" android:textColor="#282727"
android:background="#FFFFFF" android:hint="Summary"
android:layout_marginBottom="30dp"
/>
<Button
android:id="#+id/add_class_button"
android:layout_width="100dp" android:layout_height="60dp"
android:layout_marginTop="0dp" android:layout_marginLeft="150dp"
android:text="ADD" android:background="#f8255f"
android:textSize="22sp" android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
FragmentManager fragmentManager = getActivity().getFragmentManager();
you should use getChildFragmentManager, to perform transaction from a Fragment
fragmentTransaction.replace(R.layout.teacher_class, FRAGMENT);
the first parameter has to be the id (R.id.) of the ViewGroup that hosts the Fragment itself, and not the id of layout.
I suspect R.id.layout.teacher_class is the fragment you want removed? The first argument for the replace function is the container holding the fragment, not the actual fragment to be replaced.
fragmentTransaction.replace([TARGET_CONTAINER], FRAGMENT);

how to inflate a list view to a fragment

i am using fragment in my application.there are 5 tabs.in 1 tab i need to inflate a list view on a button click.given below is my code for that tab.
public class TabStoreLocatorFragment extends Fragment implements OnClickListener {
private Button mapSwitcher;
private LinearLayout lastSelectedStoreButton;
private LinearLayout noSelectedStoreSection;
private TextView storeInfoName;
private TextView storeInfoAddress;
private TextView storeInfoCity;
private RelativeLayout phoneLocationButton;
private EditText searchStoreLocation;
private ListView storeList;
private ViewFlipper storeFlipper;
LinearLayout theLayout;
Store store;
#SuppressWarnings("static-access")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
}
theLayout = (LinearLayout) inflater.inflate(
R.layout.tab_store_locator_layout, container, false);
phoneLocationButton = (RelativeLayout) theLayout.findViewById(R.id.phone_location_button);
mapSwitcher=(Button)theLayout.findViewById(R.id.switch_state_button);
lastSelectedStoreButton=(LinearLayout)theLayout.findViewById(R.id.last_selected_store_button);
noSelectedStoreSection=(LinearLayout)theLayout.findViewById(R.id.no_selected_store);
storeInfoName=(TextView)theLayout.findViewById(R.id.store_name);
storeInfoAddress=(TextView)theLayout.findViewById(R.id.store_address);
storeInfoCity=(TextView)theLayout.findViewById(R.id.store_city);
searchStoreLocation=(EditText)theLayout.findViewById(R.id.search_store);
storeFlipper = (ViewFlipper)theLayout.findViewById(R.id.store_flipper);
store=new Store();
store.setName("store1");
store.setAddress("california");
store.setCity("newyork");
store.setZipCode("23");
store.setState("california");
phoneLocationButton.setOnClickListener(this);
mapSwitcher.setOnClickListener(this);
lastSelectedStoreButton.setOnClickListener(this);
return theLayout;
}
#Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.phone_location_button) {
storeList = (ListView)storeFlipper.inflate(getActivity(), R.layout.store_list, storeFlipper);
storeFlipper.addView(storeList);
StoreAdapter adapter = new StoreAdapter(getActivity(), R.layout.store_list_item);
storeList.setAdapter(adapter);
}
else if(id == R.id.switch_state_button){
}
else{
}
}
}
i want to inflate storelist to storeFlipper.i tried to do that.but it is not working.given below is the corresponding layout for that tab.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/white" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/find_a_store_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:text="#string/tab_find_a_store_title"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<Button android:id="#+id/switch_state_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/switch_selector" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#E8E2D3" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#C5BFAD" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/last_selected_store_button" android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#android:style/Widget.Button"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:background="#drawable/last_selected_store_background" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#8E8672"
android:text="#string/last_selected_store_label"
android:textSize="12sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:orientation="vertical" >
<TextView android:id="#+id/store_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView android:id="#+id/store_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView android:id="#+id/store_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/arrow" />
</RelativeLayout>
</LinearLayout>
<LinearLayout android:id="#+id/no_selected_store"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:background="#drawable/last_selected_store_background" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#8E8672"
android:text="#string/no_selected_store_label"
android:textSize="12sp" />
</LinearLayout>
</FrameLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<EditText android:id="#+id/search_store"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:singleLine="true"
android:textSize="14sp"
android:hint="#string/search_store_placeholder"
android:layout_toLeftOf="#+id/phone_location_button"
android:background="#drawable/search_store_background" />
<RelativeLayout android:id="#id/phone_location_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#drawable/get_location_background" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/get_location" />
</RelativeLayout>
</RelativeLayout>
<ViewFlipper android:id="#+id/store_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
can anybody help me to inflate the list to flipper.
it is simple.just put this code.
storeList = (ListView) inflater.inflate(R.layout.store_list, null);
it is working

Categories

Resources