Pranesh Sahu Pranesh Sahu - 4 months ago 20
Android Question

Fragment gives me an error while inflating

I'm at the learning stage of fragment so if my question sounds stupid then please let it go and give your advices.

So i have developed one very simple application from Click here and got some error that i have posted below with my other class files.

Interface

package com.example.fragmentdemo;

public interface Communicator {
public void respond(String data);
}


Activity

package com.example.fragmentdemo;

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity implements Communicator{

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void respond(String data) {
// TODO Auto-generated method stub
FragmentManager manager=getFragmentManager();
FragmentB f2=(FragmentB) manager.findFragmentById(R.id.fragment_b);
f2.changeText(data);
}
}


FragmentA

package com.example.fragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class FragmentA extends Fragment{
Button btn;
int counter=0;
Communicator comm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(savedInstanceState==null)
{
counter=0;
}
else
{
counter=savedInstanceState.getInt("counter");
}
return inflater.inflate(R.id.fragment_a,container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
btn=(Button)getActivity().findViewById(R.id.button1);

comm=(Communicator)getActivity();

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
comm.respond(counter+""+" times");
}
});
}

@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putInt("counter",counter);
}

}


FragmentB

package com.example.fragmentdemo;

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

public class FragmentB extends Fragment {
TextView txt;
String data;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(savedInstanceState==null)
{

}
else
{
data=savedInstanceState.getString("data");
}
return inflater.inflate(R.id.fragment_b, container, false);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
txt = (TextView) getActivity().findViewById(R.id.textView2);
}
public void changeText(String dta)
{
txt.setText(dta);
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("data", data);
}
}


activity.xml

<RelativeLayout 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: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.fragmentdemo.MainActivity"
android:id="@+id/my_lay">

<fragment
android:id="@+id/fragment_b"
android:name="com.example.fragmentdemo.FragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fragment_a"
android:layout_marginTop="36dp"
android:layout_toLeftOf="@+id/fragment1" />

<fragment
android:id="@+id/fragment_a"
android:name="com.example.fragmentdemo.FragmentA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />

</RelativeLayout>


fragment_a

<?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"
android:background="#FF0000" >

<Button
android:layout_margin="20dp"
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />

</LinearLayout>


fragment_b

<?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"
android:background="#00FF00" >


<TextView
android:layout_margin="20dp"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


Log error

07-22 02:43:13.076: E/AndroidRuntime(1784): FATAL EXCEPTION: main
07-22 02:43:13.076: E/AndroidRuntime(1784): Process: com.example.fragmentdemo, PID: 1784
07-22 02:43:13.076: E/AndroidRuntime(1784): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentdemo/com.example.fragmentdemo.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.os.Handler.dispatchMessage(Handler.java:102)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.os.Looper.loop(Looper.java:136)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-22 02:43:13.076: E/AndroidRuntime(1784): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 02:43:13.076: E/AndroidRuntime(1784): at java.lang.reflect.Method.invoke(Method.java:515)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-22 02:43:13.076: E/AndroidRuntime(1784): at dalvik.system.NativeStart.main(Native Method)
07-22 02:43:13.076: E/AndroidRuntime(1784): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Activity.setContentView(Activity.java:1929)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.example.fragmentdemo.MainActivity.onCreate(MainActivity.java:17)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Activity.performCreate(Activity.java:5231)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-22 02:43:13.076: E/AndroidRuntime(1784): ... 11 more
07-22 02:43:13.076: E/AndroidRuntime(1784): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f090051 type #0x12 is not valid
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2314)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.content.res.Resources.getLayout(Resources.java:939)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.example.fragmentdemo.FragmentB.onCreateView(FragmentB.java:25)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Fragment.performCreateView(Fragment.java:1700)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.app.Activity.onCreateView(Activity.java:4786)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
07-22 02:43:13.076: E/AndroidRuntime(1784): at com.example.fragmentdemo.MainActivity.onCreateView(MainActivity.java:1)
07-22 02:43:13.076: E/AndroidRuntime(1784): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
07-22 02:43:13.076: E/AndroidRuntime(1784): ... 21 more

Answer

In both fragments, you are inflating an 'id':

return inflater.inflate(R.id.fragment_a,container, false);
return inflater.inflate(R.id.fragment_b,container, false);

but you need tu inflate the layout:

return inflater.inflate(R.layout.fragment_a,container, false);
return inflater.inflate(R.layout.fragment_b,container, false);