0
votes

I am writting a simple example of using fragments. In main activity, there are two fragments. First fragment has some textviews and second fragment has some buttons. When user click on the textview, the text on button will be changed.

The activity_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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" android:background="@drawable/ic_launcher_background">

    <fragment
        android:id="@+id/MenuFrag"
        android:name="www.myapplication.MenuFrag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/ContentFrag"
        android:name="www.myapplication.ContentFrag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />
</LinearLayout>

The MainActivity.java

package www.application;

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

public class MainActivity extends AppCompatActivity implements MenuFrag.IFragmentClickListener{

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

    @Override
    public void onMenuItemClick(int position) {
        ContentFrag content = (ContentFrag) getSupportFragmentManager().findFragmentById(R.id.ContentFrag);
        if(content!=null)
            content.updateContent(position);
    }
}

The menu_frag.xml (first fragment)

<?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:background="@color/colorPrimaryDark"
    tools:context=".MenuFrag">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 1"
            android:textSize="20sp"
            android:typeface="serif" />

        <TextView
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 2"
            android:textSize="20sp"
            android:typeface="serif" />

        <TextView
            android:id="@+id/tv3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 3"
            android:textSize="20sp"
            android:typeface="serif" />

        <TextView
            android:id="@+id/tv4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 4"
            android:textSize="20sp"
            android:typeface="serif" />

        <TextView
            android:id="@+id/tv5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-smallcaps"
            android:text="TextView 5"
            android:textSize="20sp"
            android:typeface="serif" />
    </LinearLayout>

</FrameLayout>

The MenuFrag.java

package www.application;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class MenuFrag extends ListFragment {
    IFragmentClickListener itemfragment;
    TextView tv1;

    private View.OnClickListener onclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                 itemfragment.onMenuItemClick(1);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_frag, container, false);
        tv1= view.findViewById(R.id.tv1);
        tv1.setOnClickListener(onclick);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            itemfragment = (IFragmentClickListener) context;
        }catch(ClassCastException e){e.printStackTrace();}
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        itemfragment.onMenuItemClick(position);
    }

    public interface IFragmentClickListener{
        public void onMenuItemClick(int position);
    }
}

The content_frag.xml (second fragment)

<?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:background="@color/colorPrimary"
    tools:context=".ContentFrag">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btmessage"/>
    </ScrollView>

</FrameLayout>

The ContentFrag.java

package www.application;

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.Button;

public class ContentFrag extends Fragment {

    Button btmessage;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_frag, container, false);
        btmessage = v.findViewById(R.id.btmessage);
        return v;
    }

    public void updateContent(int position){
        btmessage.setText("You clicked !");
    }
}

I didn't write the action to control the button, just generate the onclick event. However, I got error. Please help me fix error, and continue completing my task.

E/AndroidRuntime: FATAL EXCEPTION: main Process: www.myapplication, PID: 21117 java.lang.RuntimeException: Unable to start activity ComponentInfo{www.myapplication/www.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at android.view.LayoutInflater.inflate(LayoutInflater.java:365) at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at www.myapplication.MainActivity.onCreate(MainActivity.java:11) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5221)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)  Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' at android.support.v4.app.ListFragment.ensureList(ListFragment.java:349) at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:150) at android.support.v4.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1695) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1431) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1684) at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1930) at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3745) at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120) at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:387) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)  at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  at www.myapplication.MainActivity.onCreate(MainActivity.java:11)  at android.app.Activity.performCreate(Activity.java:5937)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5221)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1

1 Answers

2
votes

The issue is with MenuFrag Fragment, the way you are initializing your TextView.
ie tv1= getActivity().findViewById(R.id.tv1);

Rather I'd suggest to move it to onCreateView(). Like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.menu_frag, container, false);
    tv1 = view.findViewById(R.id.tv1);
    tv1.setOnClickListener(onclick);
    return view;
} 

When you called getActivity().findViewById, It searches for that TextView id in activity's layout which couldn't be found.