1

App wont even open. Fixed last time by Implementing Fragment Interaction Listeners. I believe this error has to do with my onAnswerFragmentInteractionListener and my OnFragmentInteractionListener. Before they were both called the same thing but I looked at my class examples and saw that one was changed so I went and changed all the "OnFragmentInteractionListener"'s in my Answer fragment to "onAnswerFragmentINteractionListener".

They problem could also be me trying to pass data between each of the Fragments. I was just going to test some methods and see if they work but I couldnt get it to work reguardless.

Here is code

MainActivity.java

package com.example.nick.assignment4;

import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public abstract class MainActivity extends AppCompatActivity implements AnswerFragment.onAnswerFragmentInteractionListener, FlashcardFragment.OnFragmentInteractionListener {

    static String correctOp;
    static String selectedOp;
    static int correct;
    static TextView textCorrect;

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



    }

    @Override
    public void onAnswerFragmentInteractionListener(int op) {


    }

    public static void getCorrectOp(String op){
        correctOp = op;
    }

}

FlashcardFragment:

package com.example.nick.assignment4;

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.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Random;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link FlashcardFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class FlashcardFragment extends Fragment {

    private OnFragmentInteractionListener mListener;
    Random rand;
    int answer;
    String[] ops = {"+","-","*"};

    public FlashcardFragment() {
        // Required empty public constructor
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View flashfrag = inflater.inflate(R.layout.fragment_flashcard, container, false);

        // Setting up the flash cards
        TextView textNum1 = (TextView) flashfrag.findViewById(R.id.tv_num1);
        TextView textNum2 = (TextView) flashfrag.findViewById(R.id.tv_num2);
        TextView textAnswer = (TextView) flashfrag.findViewById(R.id.tv_answer);
        TextView textOp = (TextView) flashfrag.findViewById(R.id.tv_opp);


        // Getting random numbers for Flashcard.
        rand = new Random();
        int Number1 = rand.nextInt((100 - 1) + 1);
        textNum1.setText(""+Number1);

        int Number2 = rand.nextInt((Number1 - 1) + 1);
        textNum2.setText(""+Number2);

        // Getting random operator.
        String op = ops[rand.nextInt(3)];
        textOp.setText("?");


        if(op == "+")
            answer = Number1 + Number2;
        if(op == "-")
            answer = Number1 - Number2;
        if(op == "*")
            answer = Number1 * Number2;

        textAnswer.setText(""+answer);

        return flashfrag;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

AnswerFragment

package com.example.nick.assignment4;

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


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link AnswerFragment.onAnswerFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class AnswerFragment extends Fragment {

    private onAnswerFragmentInteractionListener mListener;

    public final static int OP_ADD = 0;
    public final static int OP_SUB = 1;
    public final static int OP_MUL = 2;

    public AnswerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ansfrag = inflater.inflate(R.layout.fragment_answer, container, false);

        // Get buttons
        Button addButton = (Button) ansfrag.findViewById(R.id.bt_add);
        Button subButton = (Button) ansfrag.findViewById(R.id.bt_sub);
        Button mulButton = (Button) ansfrag.findViewById(R.id.bt_mul);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //onButtonPressed(OP_ADD);
                //MainActivity.checkAnswer("+");
            }
        });

        subButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //onButtonPressed(OP_SUB);
                //MainActivity.checkAnswer("-");
            }
        });

        mulButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //onButtonPressed(OP_MUL);
                //MainActivity.checkAnswer("*");
            }
        });

        return ansfrag;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(int op) {
        if (mListener != null) {
            mListener.onAnswerFragmentInteractionListener(op);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof onAnswerFragmentInteractionListener) {
            mListener = (onAnswerFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement onAnswerFragmentInteraction");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface onAnswerFragmentInteractionListener {
        // TODO: Update argument type and name
        void onAnswerFragmentInteractionListener(int op);
    }
}

ERROR CODE:

--------- beginning of crash
2018-09-25 19:13:09.405 13585-13585/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.nick.assignment4, PID: 13585
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nick.assignment4/com.example.nick.assignment4.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
     Caused by: java.lang.RuntimeException: com.example.nick.assignment4.MainActivity@ff6cb87 must implement OnFragmentInteractionListener
        at com.example.nick.assignment4.AnswerFragment.onAttach(AnswerFragment.java:83)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1404)
        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:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.nick.assignment4.MainActivity.onCreate(MainActivity.java:13)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Main XML

<fragment
    android:id="@+id/flash_fragment"
    android:name="com.example.nick.assignment4.FlashcardFragment"
    android:layout_width="369dp"
    android:layout_height="276dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="4dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<fragment
    android:id="@+id/answer_fragment"
    android:name="com.example.nick.assignment4.AnswerFragment"
    android:layout_width="361dp"
    android:layout_height="209dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="4dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
JOhn
  • 91
  • 1
  • 10

4 Answers4

0
 Caused by: java.lang.RuntimeException: com.example.nick.assignment4.MainActivity@ff6cb87 must implement OnFragmentInteractionListener

are you sure you're implementing all the methods for OnFragmentInteractionListener ?

Brandon
  • 1,158
  • 3
  • 12
  • 22
  • I gave you my whole code. I created the fragments and checked the "include interface callbacks" box. Should I make another Override method for the "OnFragmentInteractionListener?" because I have one for the "onAnswerFragmentINteractionListener"? – JOhn Sep 26 '18 at 03:10
  • you need to also add in an override for public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } into your main activity – Brandon Sep 26 '18 at 03:18
  • Added it even implemented the interface while doing it still wont work. Deleted the fragment on the main page and tried to run it without the flash fragment. Im about to just turn this in and call it a night – JOhn Sep 26 '18 at 03:42
0

If you insert fragment programmatically it will solve error. As your adding it from xml when it's added on ui fragment's onAttach it dosen't find context which have implemented interface. that's why it show error

0

Add this method in your MainActivity:

@Override
public void onFragmentInteraction(Uri uri){
}
nupadhyaya
  • 1,909
  • 1
  • 14
  • 14
  • Still crashes my friend. :( – JOhn Sep 26 '18 at 03:19
  • new error `Caused by: java.lang.RuntimeException: com.example.nick.assignment4.MainActivity@ff6cb87 must implement OnFragmentInteractionListener` – JOhn Sep 26 '18 at 03:20
  • did you remove FlashcardFragment.OnFragmentInteractionListener from MainActivity implements? If you did, put it back – nupadhyaya Sep 26 '18 at 03:21
  • Yes I did. Added it back and tried your Override method above. Error: Method does not override method from its super class. – JOhn Sep 26 '18 at 03:28
0

In your layout xml file, replace android:name with name like:

name="com.example.nick.assignment4.FlashcardFragment"

...

name="com.example.nick.assignment4.AnswerFragment"

Because android:name is unknown attribute.

giang nguyen
  • 393
  • 4
  • 12