App crashes after adding Toolbar to a Fragment - android

I'm trying to switch from an Activity with a fragment without a Toolbar (R.id.main_screen) to one that has a Toolbar (R.id.source_items_list).
However, whenever I have a Toolbar in R.id.source_items_list, it crashes. The error I'm getting is
android.view.InflateException: Binary XML file line #10: Error
inflating class android.support.v7.widget.Toolbar
which comes from the line
View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false); in the following code.
I wonder what I'm doing wrong. Here's my code.
public class MainActivity extends FragmentActivity implements MainFragment.OnFragmentInteractionListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// set uncaught exception handler for thread
// Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
mMainFrame = MainFragment.newInstance();
fragmentManager.beginTransaction().add(R.id.container, mMainFrame).commit();
}
#Override
public void onFragmentInteraction(int id) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (id) {
case R.id.button_sources:
Fragment fragment = new SourceListFragment();
FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
fragmentTrans.replace(R.id.container, fragment);
fragmentTrans.addToBackStack(null);
fragmentTrans.commit();
break;
default:
break;
}
}
}
public class SourceListFragment extends Fragment implements AbsListView.OnClickListener {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
Log.d("SOURCELIST", "SourceListFragment onCreateView\n");
View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);
return view;
}
public static SourceListFragment newInstance(BluetoothAdapter adapter) {
SourceListFragment fragment = new SourceListFragment();
mBTAdapter = adapter;
return fragment;
}
// Container Activity must implement this interface
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(int id);
}
}
Here's the layout:
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="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:background="#FFF"
android:orientation="vertical"
android:id="#+id/container">
</FrameLayout>
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_screen"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="148dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="128dp"
android:background="#01579b"
android:paddingBottom="20dp"
android:paddingLeft="104dp" android:id="#+id/banner">
</RelativeLayout>
</RelativeLayout>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button_sources"
app:layout_widthPercent="30%"
app:layout_heightPercent="30%"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="15%"
app:layout_marginRightPercent="5%"/>
...
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
fragment_sourceitem_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/source_items_list"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimary"/>
<ListView android:id="#android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.gc.materialdesign.views.ProgressBarCircularIndeterminate
android:id="#+id/progressBarCircularIndeterminate"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#1E88E5" />
</FrameLayout>
Any help will be appreciated.
Thanks

Related

Set and call Fragment Tag correct

Can't get rid of this problem. I saw a couple other questions with a similar problem, but nothing fixed it.
Short story: I replace my placeholders with fragments in my MainActivty and would like to call a public method from that new fragment. But calling the fragment from his Tag, leads to a "Null object reference" error.
I tried multiple ways to set and call the Tag but without success
Main.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:id="#+id/mainActivity"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:menu="#menu/quick_settings"
tools:context="....">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/fragment3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
...
Main.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_annual_service);
createList();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void createList() {
Resources r = getResources();
String packageName = getPackageName();
//Array with all Fragments
fragmentList = new Fragment[]{
new FragmentA,
new FragmentB,
new FragmentC,
...
};
numberOfFragments = fragmentList.length;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (int arrayIndex=0, fragmentNumber=1; fragmentNumber <= numberOfFragments; arrayIndex++, fragmentNumber++){
fragmentTransaction.replace(r.getIdentifier("fragment" + fragmentNumber, "id", packageName), fragmentList[arrayIndex], "fragment0"+fragmentNumber); // also try to set the TagName
}
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
((Unfold)fragmentManager.findFragmentByTag("fragment01")).unfold(); // Method is implemented by a interface
FragmentA.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/FragmentA"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingEnd="20dp"
android:paddingStart="45dp"
android:visibility="gone">
...
</LinearLayout>
FragmentA.java
Button backBtn;
TextView header;
LinearLayout llayout;
Drawable defaulBG;
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return setListener(inflater, container);
}
private View setListener(LayoutInflater inflater, ViewGroup container){
view = inflater.inflate(R.layout.fragment_general_information, container, false);
header = (TextView) view.findViewById(R.id.generalInformationHeaderText);
header.setOnClickListener(this);
llayout = (LinearLayout) view.findViewById(R.id.content);
defaulBG = inForkliftBtn.getBackground();
backBtn = (Button) view.findViewById(R.id.backBtn);
setRetainInstance(true);
return view;
}
public void unfold(){
if(llayout.getVisibility() == View.GONE){
llayout.setVisibility(View.VISIBLE);
}else{
llayout.setVisibility(View.GONE);
}
}
Call executePendingTransactions() on fragment manager after committing transaction
...
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
((Unfold)fragmentManager.findFragmentByTag("fragment01")).unfold();
EDIT 1
You can try another option:
Do not add the executePendingTransactions()
In fragment's onViewCreated, if getTag() mathes "fragment01" call unfold method

Fragment is overlapping

I have created an app when i'm testing my app or i clicked on back button my fragment is overlapping other fragment. I don't know why it's happening. Please help me i need help..
My codes
public class MainActivity extends AppCompatActivity {
private SharedPreferences pref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);
initFragment();
}
private void initFragment(){
Fragment fragment;
if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
fragment = new ProfileFragment();
}else {
fragment = new LoginFragment();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,fragment);
ft.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
android:background="#ff51b46d"
tools:context="com.example.hldev.docup.MainActivity">
<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"
tools:context="com.example.hldev.docup.MainActivity">
<FrameLayout
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Uploads.java
public class Uploads extends Fragment implements View.OnClickListener{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_uploads, container, false);
ivAttachment = (ImageView)view.findViewById(R.id.ivAttachment);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_email = (TextView)view.findViewById(R.id.tv_email);
bUpload = (Button)view.findViewById(R.id.b_upload);
tvFileName = (TextView)view.findViewById(R.id.tv_file_name);
ivAttachment.setOnClickListener(this);
bUpload.setOnClickListener(this);
return view;
}
private void goToProfile(){
Fragment profile = new ProfileFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,profile);
ft.commit();
}
}
Just set a background color to your new <fragment /> in XML file or else it will be transparent and show a replaced fragment.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="schemas.android.com/apk/res/android";
xmlns:tools="schemas.android.com/tools";
android:id="#+id/frag24"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green">
<!-- Your Content -->
</RelativeLayout>
</FrameLayout >
set white background color of your parent layout of your activity_uploads. also do same with all layout which you use in Fragment.
replace your code with below.
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
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"
android:background="#android:color/holo_green_dark"
tools:context="com.example.hldev.docup.Uploads">

Simple fragement is not working

I am practicing to multi-pane screen using fragment, but it's not working properly. I want two frame in single activity. One should be static and another will dynamic list.
main_biddingwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="200dp"
android:weightSum="1"
android:id="#+id/ag">
<fragment
android:layout_width="163dp"
android:layout_height="match_parent"
android:name="driver.project_detail"
android:id="#+id/fragment"
android:layout_row="0"
android:layout_column="0" />
<fragment
android:layout_width="184dp"
android:layout_height="match_parent"
android:name="driver.bid_list"
android:id="#+id/fragment2"
android:layout_row="0"
android:layout_column="19" />
</GridLayout>`
main.class
public class biddingWindow extends Activity{
protected void OnCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main_biddingwindow);
}
}
bidlist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
bid_list.java
public class bid_list extends Fragment {
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
return inflater.inflate(R.layout.bidlist,container, false);
}
}
output screen
Just Create the objects of both the fragments in activity and you can access everything in fragment
public class bid_list extends Fragment {
public ListView listView;
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
View view = inflater.inflate(R.layout.bidlist,container, false);
listView = (ListView)view.findViewById(R.id.listView);
return view ;
}
}
public class bid_list extends Fragment {
public ListView listView;
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
View view = inflater.inflate(R.layout.bidlist,container, false);
listView = (ListView)view.findViewById(R.id.listView);
return view ;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ag">
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="driver.project_detail"
android:id="#+id/fragment"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:name="driver.bid_list"
android:id="#+id/fragment2"/>
</LinearLayout>
class Main extends Activity
{
protected void OnCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main_biddingwindow);
FrameLayout frame1 = (FrameLayout)findViewById(R.id.fragment);
FrameLayout frame2 = (FrameLayout)findViewById(R.id.fragment);
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
frame1.removeAllView();
frame1.addView(fragment1)
frame2.removeAllView();
frame2.addView(fragment2)
}
}

Fragment not being replaced

I have a FragmentActivity that is supposed to swap out the front screen fragment (with buttons) to the fragment that the button points to when one is clicked.
Suppose the button that invokes SourceListFragment is clicked. Both onCreate() and onCreateView() of SourceListFragment are called, but somehow the screen stays the same, i.e. the front screen fragment isn't getting replaced.
I wonder what I'm doing wrong. Here's my code.
public class MainActivity extends FragmentActivity implements MainFragment.OnFragmentInteractionListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// set uncaught exception handler for thread
// Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
mMainFrame = MainFragment.newInstance();
fragmentManager.beginTransaction().add(R.id.container, mMainFrame).commit();
}
#Override
public void onFragmentInteraction(int id) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (id) {
case R.id.button_sources:
Fragment fragment = new SourceListFragment();
FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
fragmentTrans.replace(R.id.main_screen, fragment);
fragmentTrans.addToBackStack(null);
fragmentTrans.commit();
break;
default:
break;
}
}
}
public class SourceListFragment extends Fragment implements AbsListView.OnClickListener {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
Log.d("SOURCELIST", "SourceListFragment onCreateView\n");
View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);
return view;
}
public static SourceListFragment newInstance(BluetoothAdapter adapter) {
SourceListFragment fragment = new SourceListFragment();
mBTAdapter = adapter;
return fragment;
}
// Container Activity must implement this interface
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(int id);
}
}
Here's the layout:
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="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:background="#FFF"
android:orientation="vertical"
android:id="#+id/container">
</FrameLayout>
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_screen"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="148dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="128dp"
android:background="#01579b"
android:paddingBottom="20dp"
android:paddingLeft="104dp" android:id="#+id/banner">
</RelativeLayout>
</RelativeLayout>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button_sources"
app:layout_widthPercent="30%"
app:layout_heightPercent="30%"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="15%"
app:layout_marginRightPercent="5%"/>
...
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
fragment_sourceitem_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical">
<ListView android:id="#android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.gc.materialdesign.views.ProgressBarCircularIndeterminate
android:id="#+id/progressBarCircularIndeterminate"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#1E88E5" />
</FrameLayout>
Any help will be appreciated.
Thanks
You add a Fragment into Linear layout with id main_screen, but the fragment should be added into FrameLayout. The transitions do work in case of LinearLayout, but the newly added Fragment is outside of the display.

Error inflating class error - Fragments

im trying to set up a simple app using 2 fragments. For this i created 3 classes:
MainActivity:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_A frag_A = new Fragment_A();
fragmentTransaction.add(R.id.map_container, frag_A);
Fragment_B frag_B = new Fragment_B();
fragmentTransaction.add(R.id.map_container2, frag_B);
fragmentTransaction.commit();
}
}
Fragment_A:
public class Fragment_A extends Fragment{
MapView map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_a_layout, container, false);
map = (MapView) v.findViewById(R.id.map_object);
map.onCreate(savedInstanceState);
return v;
}
}
Fragment_B:
public class Fragment_B extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_b_layout, container, false);
v.setBackgroundColor(Color.BLUE);
return v;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/map_container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/map_container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
fragment_a_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_object"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</FrameLayout>
fragment_b_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
It happens that everytime i try to run this app i have this:Binary XML file line #6 Error inflating class fragment, where #6 is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
...
I dont understand what can be the problem, I simply want to see both fragments on my screen.

Categories

Resources