3
votes

I am new to android.I am getting the following error. I have looked into the same questions and have applied the answers to my code. But I am still getting the same error.

05-28 09:30:12.800 2016-2016/com.example.yatisawhney.fragmentdemo E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.yatisawhney.fragmentdemo, PID: 2016 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yatisawhney.fragmentdemo/com.example.yatisawhney.fragmentdemo.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:714) at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) at com.example.yatisawhney.fragmentdemo.MainActivity.onCreate(MainActivity.java:13) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: Fragment com.example.yatisawhney.fragmentdemo.BottomFragment did not create a view. at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2319) at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120) at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357) at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690) at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) at com.example.yatisawhney.fragmentdemo.MainActivity.onCreate(MainActivity.java:13) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)


The following are my java 3 files

MainActivity.java

package com.example.yatisawhney.fragmentdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements TopFragment.ActivityCommunicator {

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

    @Override
    public void changeText(String a, String b) {
        BottomFragment bf = (BottomFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
        bf.setMeme(a,b);
    }
}

Bottom Fragment

package com.example.yatisawhney.fragmentdemo;

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

/**
 * Created by yatisawhney on 28/05/16.
 */
public class BottomFragment extends Fragment {
    private static TextView top ;
    private static TextView bottom;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View bottom = inflater.inflate(R.layout.bottom_section ,container, false);

        top = (TextView)bottom.findViewById(R.id.textView);
        bottom=(TextView)bottom.findViewById(R.id.textView2);

        return bottom;
    }

    public void setMeme(String a , String b){
        top.setText(a);
        bottom.setText(b);
    }
}

Top Fragment

package com.example.yatisawhney.fragmentdemo;

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

/**
 * Created by yatisawhney on 28/05/16.
 */
public class TopFragment extends Fragment{

    private static  EditText top;
    private static EditText bottom;

    ActivityCommunicator communicator;


    public interface ActivityCommunicator{
        public void changeText(String a , String b);
    }

    private void changeText(){
        communicator.changeText(top.getText().toString() , bottom.getText().toString());
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            communicator = (ActivityCommunicator) activity;
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View top_view = inflater.inflate(R.layout.top_section , container , false);

        top=(EditText)top_view.findViewById(R.id.yates_text);
        bottom=(EditText)top_view.findViewById(R.id.yates_another_text);
        Button button = (Button)top_view.findViewById(R.id.yates_button);
        button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        changeText();
                    }
                }
        );

        return top_view;
    }
}

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: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.yatisawhney.fragmentdemo.MainActivity">

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.yatisawhney.fragmentdemo.TopFragment"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/top_section" />

    <fragment
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:name="com.example.yatisawhney.fragmentdemo.BottomFragment"
        android:id="@+id/fragment2"
        tools:layout="@layout/bottom_section"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
1

1 Answers

2
votes

Finally I got the answer to my question.

onCreate of the Fragment life cycle might be called during the onCreate of the Activity life cycle. But this not the case all the times

Therefore, just to be on safer side if you wish to access the views in fragment do it inside onActivityCreated() method. This gives you the confirmation that the activity has been created and View hierarchy has been loaded into the memory. :)