0

I'm trying to write a project with an activity where there is a fragment with an edit text, a seekbar and a button and below a fragment with a textview. If you write on the editText and you move the seekbar you change the content and the size of the text in the textView of the fragment below. I used the following already done commented project and then I rename some elements and I corrected some parts:here

However I get an error "

Error inflating class fragment

": the following is part of the error message:

03-21 08:33:11.174 2830-2830/com.example.utente.fragmentconmutamenti E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.utente.fragmentconmutamenti, PID: 2830 java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.utente.fragmentconmutamenti/com.example.utente.fragmentconmutamenti.MainActivity}: android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class fragment

I read similar questions like this and their answers but I still didn't find what is the error in my code.

MainActivity.java

package com.example.utente.fragmentconmutamenti;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}



public void onButtonClick(int fontsize, String text) {

        TextFragment textFragment =
                (TextFragment)
                        getFragmentManager().findFragmentById(R.id.text_fragment);

        textFragment.changeTextProperties(fontsize, text);

}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
tools:context="com.example.utente.fragmentconmutamenti.MainActivity">

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.utente.fragmentconmutamenti.ToolbarFragment"
    android:id="@+id/toolbar_fragment"
    tools:layout="@layout/toolbar_fragment"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.utente.fragmentconmutamenti.TextFragment"
    android:id="@+id/text_fragment"
    android:layout_marginTop="130dp"
    android:layout_below="@+id/toolbar_fragment"
    android:layout_centerHorizontal="true"
    tools:layout="@layout/text_fragment" />
</RelativeLayout>

ToolbarFragment.java

package com.example.utente.fragmentconmutamenti;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;

public class ToolbarFragment

implements SeekBar.OnSeekBarChangeListener
{


private static int seekvalue = 10;
private static EditText edittext;

ToolbarListener activityCallback;

public interface ToolbarListener {
    public void onButtonClick(int position, String text);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activityCallback = (ToolbarListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement ToolbarListener");
    }
}

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

    View view_toolbar_fragment =inflater.inflate(R.layout.toolbar_fragment, container, false);

    edittext = (EditText) view_toolbar_fragment.findViewById(R.id.editText1);

    final SeekBar seekbar =
            (SeekBar) view_toolbar_fragment.findViewById(R.id.seekBar1);

    seekbar.setOnSeekBarChangeListener(this);

    final Button button =
            (Button) view_toolbar_fragment.findViewById(R.id.button_text);


    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            buttonClicked(v);
        }
    });

    return view_toolbar_fragment;

}

public void buttonClicked (View view) {

    activityCallback.onButtonClick(seekvalue,
            edittext.getText().toString());

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
                              boolean fromUser) {
    seekvalue = progress;
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
}

@Override
public void onStopTrackingTouch(SeekBar arg0) {

}


}

toolbar_fragment.xml

package com.example.utente.fragmentconmutamenti;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;

public class ToolbarFragment

implements SeekBar.OnSeekBarChangeListener
{


private static int seekvalue = 10;
private static EditText edittext;

ToolbarListener activityCallback;

public interface ToolbarListener {
    public void onButtonClick(int position, String text);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activityCallback = (ToolbarListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement ToolbarListener");
    }
}

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

    View view_toolbar_fragment =inflater.inflate(R.layout.toolbar_fragment, container, false);

    edittext = (EditText) view_toolbar_fragment.findViewById(R.id.editText1);

    final SeekBar seekbar =
            (SeekBar) view_toolbar_fragment.findViewById(R.id.seekBar1);

    seekbar.setOnSeekBarChangeListener(this);

    final Button button =
            (Button) view_toolbar_fragment.findViewById(R.id.button_text);


    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            buttonClicked(v);
        }
    });

    return view_toolbar_fragment;

}

public void buttonClicked (View view) {

    activityCallback.onButtonClick(seekvalue,
            edittext.getText().toString());

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
                              boolean fromUser) {
    seekvalue = progress;
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
}

@Override
public void onStopTrackingTouch(SeekBar arg0) {

}


}

TextFragment.java

 package com.example.utente.fragmentconmutamenti;

    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.LayoutInflater;
   import android.view.View;
  import android.view.ViewGroup;
import android.widget.TextView;

    public class TextFragment

        extends Fragment {

    private static TextView textview;

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

        View view_text_fragment =inflater.inflate(R.layout.text_fragment, container, false);

        textview = (TextView) view_text_fragment.findViewById(R.id.TextView1);

        return view_text_fragment;

    }

    public void changeTextProperties(int fontsize, String text)
    {
        textview.setTextSize(fontsize);
        textview.setText(text);
    }

    }

text_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/TextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Fragment Two"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Update: i replaced android.app.Fragment with android.support.v4.app.Fragment; and getFragmentManager() with

getSupportFragmentManager()

but it get a very similar error:

Process: com.example.utente.fragmentconmutamenti, PID: 3170

java.lang.RuntimeException:     Unable to start activity ComponentInfo   

{com.example.utente.fragmentconmutamenti/com.example.utente.fragmentconmutamenti.MainActivity}: android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class fragment at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)

Miche85
  • 5
  • 6

1 Answers1

0

Check your imports in both of the Fragments

You should go with

import android.support.v4.app.Fragment;

instead of

import android.app.Fragment;

and on click

TextFragment textFragment =
                (TextFragment)
                        getSupportFragmentManager().findFragmentById(R.id.text_fragment);
9spl
  • 357
  • 1
  • 11
  • Thank you very much, also I make an error in copying and paste the code of "TextFragment.java" on the thread, obiouvsly there are the imports at the beginning: – Miche85 Mar 21 '17 at 10:42
  • As I wrote above, I corrected the code with support.v4.app.Fragment and getSupportFragmentManager() but I still have a very similar error with inflating class. Do you have any idea about the cause of it? – Miche85 Mar 21 '17 at 12:17
  • hi , yes if you have two framgents in Same Activity you should try with FrameLayout Approch..check this link http://stackoverflow.com/questions/17580593/android-two-fragments-in-same-activity it will help you to understand.. – 9spl Mar 21 '17 at 12:36
  • I read the link, and I understood that I must change in activity_main.xml every " – Miche85 Mar 21 '17 at 23:10
  • you also need to add or replace fragment with help of FragmentTransaction which you will find in same link and don't forgot to commit it. and remove findFragmentById it will not help you.. – 9spl Mar 22 '17 at 06:48