Why do I get the android.view.InflateException when using FragmentContainerView - java

I am still pretty new to fragments and I am sure I do not understand things correctly. But I hope I can learn from this now.
I follow this guide for connecting my xml with the fragments: https://developer.android.com/guide/fragments/create. But when starting the app I get an error that says that some parts are not compatible.
Here are my files:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
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"
tools:context=".MainActivity"
android:id="#+id/main"
android:orientation="horizontal"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/brand"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/item"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:srcCompat="#drawable/gibson_1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/price"
android:gravity="right"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_marginRight="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to cart"
android:drawableLeft="#drawable/drawablestart"
android:id="#+id/addCart"
/>
</LinearLayout>
</FrameLayout>
</androidx.fragment.app.FragmentContainerView>
MainActive.java
package at.hlpinkafeld.android.musicstore;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public MainActivity() {
super(R.layout.activity_main);
}
#Nullable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.add(R.id.main, ProductDetailFragment.class, null)
.commit();
}
}
}
fragment.java
package at.hlpinkafeld.android.musicstore;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.material.textview.MaterialTextView;
public class ProductDetailFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.activity_main,container,false);
System.out.println(view);
return view;
}
}
the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: at.hlpinkafeld.android.musicstore, PID: 13020
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.hlpinkafeld.android.musicstore/at.hlpinkafeld.android.musicstore.MainActivity}: android.view.InflateException: Binary XML file line #11 in at.hlpinkafeld.android.musicstore:layout/activity_main: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.FrameLayout{58ede95 V.E...... ......I. 0,0-0,0} is not associated with a Fragment.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: android.view.InflateException: Binary XML file line #11 in at.hlpinkafeld.android.musicstore:layout/activity_main: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.FrameLayout{58ede95 V.E...... ......I. 0,0-0,0} is not associated with a Fragment.
Caused by: java.lang.IllegalStateException: Views added to a FragmentContainerView must be associated with a Fragment. View android.widget.FrameLayout{58ede95 V.E...... ......I. 0,0-0,0} is not associated with a Fragment.
at androidx.fragment.app.FragmentContainerView.addView(FragmentContainerView.kt:266)
at android.view.ViewGroup.addView(ViewGroup.java:5048)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1131)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at android.view.LayoutInflater.inflate(LayoutInflater.java:485)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:710)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:356)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217)
at at.hlpinkafeld.android.musicstore.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
For help and an explanation why it didn't work like that, would be nice

I think you might want to try loading a simple fragment into an activity.
Try this code. It's working for you.
For more info: click
Note: Always create a new object like this to load fragments new ProductDetailFragment() , and use setContentView(); inside of onCreate().
1. First of all, create the perfect activity to load a fragment.
MainActivity.class
public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.add(R.id.main, new ProductDetailFragment(), null)
.commit();
}
}
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/main"
android:orientation="horizontal"
>
</RelativeLayout>
2. create fragment ProductDetailFragment.class
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class ProductDetailFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_product_details,container,false);
System.out.println(view);
return view;
}
}
3. make diffrent layout for fragment fragment_product_details.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/brand" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/item"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:srcCompat="#drawable/gibson_1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="#string/price" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:inputType="phone" />
<Button
android:id="#+id/addCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/drawablestart"
android:text="Add to cart" />
</LinearLayout>
</FrameLayout>

You misunderstood the actual code in the link :- https://developer.android.com/guide/fragments/create
You put the fragment xml code inside your fragment container view which is inside your main activity.
You just need to declare the fragment container view defined below.
The fragment container view is main container which contains your fragment like below in your main activity.
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="<provide your fragment class here from your code it is ProductDetailFragment>" />
Then create the fragment xml file in layout directory named fragment_product_detail:-
Then put your fragment xml code in it. like below.
fragment_product_detail.xml :-
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/brand"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/item"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:srcCompat="#drawable/gibson_1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/price"
android:gravity="right"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_marginRight="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to cart"
android:drawableLeft="#drawable/drawablestart"
android:id="#+id/addCart"
/>
</LinearLayout>
</FrameLayout>
Then in your fragment onCreateView method make sure to inflate the fragment layout like below :-
View view =inflater.inflate(R.layout.fragment_product_detail,container,false);
This will resolve your problem.
Let me know if you still face any issue.

Related

How to open fragments containing webViews in another activity on button click from fragments of another activity?

I have image-buttons in one of the fragments of an activity and want to open corresponding fragments containing webViews on button clicks in another activity.
I am a beginner so please do give appropriate codes for this.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.administrator.hiha.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar">
</include>
<fragment
android:id="#+id/upperFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.example.administrator.hiha.Upper_main_Fragment"
android:layout_below="#id/toolbar"
>
</fragment>
<fragment
android:layout_below="#id/upperFragment"
android:id="#+id/lowerFragment"
android:layout_width="match_parent"
android:layout_height="140dp"
android:name="com.example.administrator.hiha.Lower_main_Fragment"
>
</fragment>
<fragment
android:layout_below="#id/lowerFragment"
android:id="#+id/Bottom_Most_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.administrator.hiha.BottomMostFragment"
tools:layout="#layout/fragment_bottom_most">
</fragment>
<View
android:id="#+id/hLastRow"
android:layout_centerHorizontal="true"
android:layout_below="#id/Bottom_Most_Fragment"
android:layout_width="300dp"
android:layout_height="2dp"
android:background="#EEEEEE"
/>
<TextView
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/hLastRow"
android:layout_centerHorizontal="true"
android:text="Designed \u0026 Developed by me © 2016."/>
</RelativeLayout>
</ScrollView>
MainActivity.java
package com.example.administrator.hiha;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
//Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar=(Toolbar)findViewById(R.id.toolbar);
}
}
fragment_lower_main.xml
<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"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/about_board"
android:id="#+id/about_img_btn"
android:onClick="onAboutBoardClick"
/>
<TextView
android:id="#+id/text_about_img_btn"
android:text="About Board"
android:layout_below="#id/about_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/sarasvati"
android:id="#+id/sarasvati_img_btn"
android:layout_toRightOf="#id/about_img_btn"
android:onClick="onSarasvatiClick"
/>
<TextView
android:layout_toRightOf="#id/text_about_img_btn"
android:id="#+id/text_sarasvati_img_btn"
android:text="Sarasvati"
android:layout_below="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/scientific_evidences"
android:id="#+id/scientific_evidences_img_btn"
android:onClick="onScientificEvidencesClick"
/>
<TextView
android:layout_toRightOf="#id/text_sarasvati_img_btn"
android:id="#+id/text_scientific_evidences_img_btn"
android:text="Scientific EVidences"
android:layout_below="#id/scientific_evidences_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/digital_library_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/affltd_organization"
android:id="#+id/affltd_oragnization_img_btn"
android:onClick="onAffltdOrganizationClick"
/>
<TextView
android:layout_toRightOf="#id/text_digital_library_img_btn"
android:id="#+id/text_affltd_oragnization_img_btn"
android:text="Affiliated Organizations"
android:layout_below="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="#id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="#drawable/tender"
android:id="#+id/tender_img_btn"
android:onClick="onTenderClick"
/>
<TextView
android:layout_toRightOf="#id/text_affltd_oragnization_img_btn"
android:id="#+id/text_tender_img_btn"
android:text="Tenders"
android:layout_below="#id/tender_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
Lower_Main_Fragment.java
package com.example.administrator.hiha;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class Lower_main_Fragment extends Fragment {
ImageButton about_img_btn;
ImageButton sarasvati_img_btn;
ImageButton scientific_evidences_img_btn;
ImageButton digital_library_img_btn;
ImageButton affltd_oragnization_img_btn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView=inflater.inflate(R.layout.fragment_lower_main_,container,false);
//cast image buttons
about_img_btn=(ImageButton)getView().findViewById(R.id.about_img_btn);
sarasvati_img_btn=(ImageButton)getView().findViewById(R.id.sarasvati_img_btn);
scientific_evidences_img_btn=(ImageButton)getView().findViewById(R.id.scientific_evidences_img_btn);
digital_library_img_btn=(ImageButton)getView().findViewById(R.id.digital_library_img_btn);
affltd_oragnization_img_btn=(ImageButton)getView().findViewById(R.id.affltd_oragnization_img_btn);
return rootView;
}
//onclick methods of image buttons
public void onAboutBoardClick(View view){
}
}
These are the files in which i want that if i click on about_img_btn then it should open a fragment containing a webView in another activity. The same should be repeated for all the buttons.
Please define the further code that i have to use and mention the files where i have to use it. Thanks!
try this:
in your fragment_lower_main xml:
Give an id to the Framelayout:
<?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"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<FrameLayout
android:id="+#id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<--Rest of the code -->
/>
</LinearLayout>
Now in code open new Fragment during button click using:
about_img_btn=(ImageButton)rootView.findViewById(R.id.about_img_btn);
about_img_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AboutUS fragment = new AboutUS();
Replace_fragment(fragment); //pass the fragment u want to replace
}
});
public void ReplaceFragment(Fragment r_fragment) {
FragmentManager fragmentManager = getActivity.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container, r_fragment).commit();
}

Android Studio fragments

I'm trying to create a quiz app with a sidebar that consists of flags (image buttons), and when you click on each flag, you can type in the country it belongs to in a fragment on the right hand side of the screen. However I'm getting an error inside the if statement in my Play.java file where it says:
fragment = new FragmentOne();
and
fragment = new FragmentTwo();
Play.java
import android.os.Bundle;
import android.app.FragmentManager;
import android.view.View;
import android.view.Menu;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import layout.FragmentOne;
import layout.FragmentTwo;
public class Play extends MainActivity {
Fragment fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
}
public void ChangeFragment(View view) {
if(view == findViewById(R.id.imageButton10)) {
fragment = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fragment);
ft.commit();
}
if(view == findViewById(R.id.imageButton9)) {
fragment = new FragmentTwo();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fragment);
ft.commit();
}
}
}
activity_play.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/activity_play"
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.karolinawullum.quizapp.Play">
<LinearLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/LinearLayout1">
<TextView
android:text="Which country does this flag belong to? Press flag to answer."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="10sp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/germany"
android:id="#+id/imageButton10"
android:layout_marginBottom="20dp"
android:layout_marginTop="50dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:onClick="ChangeFragment" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/greece"
android:id="#+id/imageButton9"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:onClick="ChangeFragment"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/france"
android:id="#+id/imageButton8"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/finland"
android:id="#+id/imageButton7"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/denmark"
android:id="#+id/imageButton6"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/cyprus"
android:id="#+id/imageButton5"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/belgium"
android:id="#+id/imageButton4"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/austria"
android:id="#+id/imageButton3"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</LinearLayout>
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:name="layout.FragmentOne"
android:id="#+id/fragment_place"
android:layout_weight="0.72" />
</LinearLayout>
FragmentOne.java
package layout;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
}
Does anyone know what I'm doing wrong?
In your Play.java class, your Fragment variable is of type android.app.Fragment. Though your FragmentOne.java (and I assume your FragmentTwo.java as well) class is of type android.support.v4.app.Fragment.
So in your Play.java class you're trying to initialize the Fragment variable with the wrong type.
Either :
Make your FragmentOne.java and FragmentTwo.java extends android.app.Fragment instead of the support version one
Make your Fragment variable in Play.java of type android.support.v4.Fragment by changing your import.

Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment

I am trying to add fragments to the activity through code to swap between them but I keep getting 'Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment'
Here is the code for the main activity:
package com.example.user.timetable_test;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class Set_Up extends FragmentActivity{
//MiscData data = MiscData.getInstance();
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set__up);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment wel = new frag_welcome();
wel.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
ft.add(R.id.fragment_container, wel);
}
}
XML file:
<?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_set_up"
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.user.timetable_test.Set_Up">
<fragment
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
For the fragment
package com.example.user.timetable_test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class frag_welcome extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.frag_set_up_welcome, container, false);
return view;
}
}
XML file:
<?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:text="#string/welcome_to_the_timetable_app"
android:layout_width="150dp"
android:layout_height="wrap_content" android:id="#+id/textView2"
android:layout_marginTop="100dp"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:text="#string/start_set_up_butt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="69dp"
android:id="#+id/startSetUp" style="#style/Widget.AppCompat.Button.Borderless"
android:layout_below="#+id/textView3" android:layout_centerHorizontal="true"/>
<TextView
android:text="#string/press_start_set_up_to_start"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textAlignment="center"
android:layout_marginTop="24dp"
android:layout_width="170dp" android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Reading your code...
// Add the fragment to the 'fragment_container' FrameLayout
You probably need to change
<fragment
android:id="#+id/fragment_container"
To
<FrameLayout
android:id="#+id/fragment_container"
Or, not use the Java code to create the Fragment and simply add the class attribute to the XML fragment block. From the documentation...
The android:name attribute in the <fragment> specifies the Fragment class to instantiate in the layout.
Otherwise, you can show the Fragment like so
Fragment wel = new WelcomeFragment(); // correct naming schema
wel.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_container, wel)
.commit();
Declare your fragment_container like this,
<?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_set_up"
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.user.timetable_test.Set_Up">
<!-- Can be any child of ViewGroup: RelativeLayout/LinearLayout/... -->
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>

Eclipse-The following classes could not be instantiated: - android.support.v7.widget.RecyclerView

I am trying to use recycler view but eclipse giving error:...my support libraries are up-to-date :
This is Error:
here is my layout that adding recycler-view :.
//part of xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:widget="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:id="#+id/playeramainlayout"
android:background="#color/black">
<LinearLayout
android:id="#+id/hf "
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="#color/black"
android:gravity="center" >
<!-- Player Buttons -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#layout/rounded_corner"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<!-- Previous Button -->
<ImageButton
android:id="#+id/btnPrevious"
android:src="#drawable/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"/>
<!-- Play Button -->
<ImageButton
android:id="#+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/btn_play" />
android:background="#null"/>
<!-- Next Button -->
<ImageButton
android:id="#+id/btnNext"
android:src="#drawable/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"/>
</LinearLayout>
</LinearLayout>
<!-- Progress Bar/Seek bar -->
<!-- Timer Display -->
<FrameLayout
android:id="#+id/imageviewkalayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/player_header_bg"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<android.support.v7.widget.RecyclerView
android:id="#+id/HSVfoalbumarinplayeractivity"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v7.widget.RecyclerView>
<ImageButton
android:id="#+id/btnRepeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#null"
android:src="#drawable/btn_repeat" />
<ImageButton
android:id="#+id/btnShuffle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:background="#null"
android:src="#drawable/btn_shuffle" />
<ImageButton
android:id="#+id/equaliser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:background="#null"
android:src="#drawable/btn_repeat" />
</FrameLayout>
<LinearLayout
android:id="#+id/timerDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/songProgressBar"
android:layout_centerHorizontal="true"
android:background="#color/black" >
<TextView
android:id="#+id/songCurrentDurationLabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left"
android:background="#color/black"
android:textColor="#eeeeee"
android:textStyle="bold" />
<TextView
android:id="#+id/songTotalDurationLabel"
android:layout_width="155dp"
android:layout_height="match_parent"
android:background="#color/black"
android:gravity="right"
android:textColor="#eeeeee"
android:textStyle="bold" />
</LinearLayout>
<SeekBar
android:id="#+id/songProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ hf "
android:layout_alignParentLeft="true"
android:background="#color/black"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:progressDrawable="#drawable/seekbar_progress"
android:thumb="#drawable/seek_handler" />
<RelativeLayout
android:id="#+id/player_header_bg"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:layout_above="#+id/timerDisplay"
android:layout_alignParentLeft="true"
android:background="#color/black"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TextView
android:id="#+id/songTitle"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center"
android:singleLine="true" />
<TextView
android:id="#+id/songartist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:singleLine="true" />
</RelativeLayout>
</RelativeLayout>
Here is the code oof initialising view that is adding recycler view to activity :
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
HSVforimageGallery=(RecyclerView) findViewById(R.id.HSVfoalbumarinplayeractivity);
HSVforimageGallery.setLayoutManager(new LinearLayoutManager(this));
HSVforimageGallery.setHasFixedSize(true);
}
Here is logcat :
12-28 21:40:32.055: E/AndroidRuntime(25525): FATAL EXCEPTION: main
12-28 21:40:32.055: E/AndroidRuntime(25525): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.musicplayer/com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity}: android.view.InflateException: Binary XML file line #113: Error inflating class android.support.v7.widget.RecyclerView
12-28 21:40:32.055: E/AndroidRuntime(25525): Caused by: android.view.InflateException: Binary XML file line #113: Error inflating class android.support.v7.widget.RecyclerView
12-28 21:40:32.055: E/AndroidRuntime(25525): at android.app.Activity.setContentView(Activity.java:1881)
12-28 21:40:32.055: E/AndroidRuntime(25525): at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:114)
12-28 21:40:32.055: E/AndroidRuntime(25525): Caused by: java.lang.NoClassDefFoundError: android.support.v7.recyclerview.R$styleable
12-28 21:40:32.055: E/AndroidRuntime(25525): at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:481)
12-28 21:40:32.055: E/AndroidRuntime(25525): at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:450)
12-28 21:40:32.055: E/AndroidRuntime(25525): ... 26 more
I tried lots of things like adding support project of recycler-view and referencing to main project etc .
But none of the thing worked .
Any help will be appreciated :).
In recent versions of the support library (22.x), the CardView and RecyclerView libraries were provided with only Android Studio (Gradle) integration, so you needed to manually create Eclipse library projects in order to use them in Eclipse.
As of v23.01 of the Support v7 library, however, there are now library projects included in the sdk.
First, make sure that you have at least v23.01 of the Support Library:
Then, navigate to the location of the Support Library in the sdk:
Copy the recyclerview folder to your project, I created a folder called "dependency" and placed all library projects in there.
Then, import your library project into Eclipse, along with your app project:
For each library project, ensure that Is Library is checked:
Then ensure that all library projects are added to your app project:
And, ensure that all library projects are included in the build path of your app project:
Now, your Eclipse project should be configured correctly.
In this simple example, I used a RecyclerView and CardViews.
Here is the Fragment code:
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(true);
MyAdapter adapter = new MyAdapter(new String[]{"testone", "testtwo", "testthree", "testfour"});
rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
return rootView;
}
}
The Adapter for the RecyclerView:
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
public CardView mCardView;
public TextView mTextView;
public MyViewHolder(View v) {
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_item, parent, false);
// set the view's size, margins, paddings and layout parameters
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
fragment_blank.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">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
card_item.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="48dp" >
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
</TextView>
</android.support.v7.widget.CardView>
</RelativeLayout>
Result:

Error inflating class fragment

I'm implementing fragments in my activity ,sometimes it shows the following error.when i trying to load the appActivity fragment. please help me get rid out of this error
Java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.rff/com.example.rff.Parent}:
android.view.InflateException: Binary XML file line #68: Error inflating class fragment
here my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/frag_parent"
>
<TableLayout
android:id="#+id/tablenew"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FFF"
android:paddingTop="1dp"
android:stretchColumns="0"
>
<TableRow
android:background="#000000"
android:paddingBottom="1dp"
android:paddingRight="1dp"
android:tag="new">
<TextView
android:id="#+id/newactivity"
android:text="#string/newact"
android:gravity="center"
android:layout_marginLeft="1dp"
android:textColor="#000000"
android:background="#FFF"
android:padding="10dp"
android:textSize="50sp"
/>
</TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_width="1000dp"
android:layout_height="50dp">
<Button
android:id="#+id/app_new"
android:layout_width="420dp"
android:layout_height="match_parent"
android:text="Apps"/>
<Button
android:id="#+id/doc_new"
android:layout_width="400dp"
android:layout_height="match_parent"
android:text="Docs"/>
</LinearLayout>
<fragment
class="com.example.rff.AppActivity"
android:id="#+id/lm_fragment"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<fragment
class="com.example.rff.DocActivity"
android:id="#+id/doc_fragment"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
Here is my parent fragment activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AppActivity appActivity = new AppActivity();
fragmentTransaction.replace(android.R.id.content, appActivity);
fragmentTransaction.commit();
AppActivity
public class AppActivity extends Fragment{
Context context;
ArrayList<ViewGroup> viewsList;
UpdateAppThread UpdateThread;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_new_layout,container, false);
context = view.getContext();
...
....
return view ;
}
This is my best guess, since your layout files and rest of the code seem fine.
Depending on your minimum SDK declared in your Manifest, if you have it lower than 11 make sure you have the following imports:
In your fragment class: import android.support.v4.app.Fragment;
In your parent Activity: import android.support.v4.app.FragmentActivity;
Make your parent Activity extend FragmentActivity instead of Activity

Categories