Android片段崩溃

时间:2018-09-26 02:41:22

标签: android android-fragments fragment

应用程序甚至无法打开。上次通过实现片段交互侦听器进行修复。我相信此错误与我的onAnswerFragmentInteractionListener和OnFragmentInteractionListener有关。在它们被称为同一事物之前,但我查看了我的类示例,发现其中的示例已更改,因此我将我的Answer片段中的所有“ OnFragmentInteractionListener”都更改为“ onAnswerFragmentINteractionListener”。

他们的问题还可能是我试图在每个Fragment之间传递数据。我只是要测试一些方法,看看它们是否有效,但我无法毫无保留地使它起作用。

这是代码

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);
    }
}

错误代码:

--------- 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)

主要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" />

4 个答案:

答案 0 :(得分:0)

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

确定要实现OnFragmentInteractionListener的所有方法吗?

答案 1 :(得分:0)

如果以编程方式插入片段,它将解决错误。 当您在ui片段的onAttach上添加xml时从xml中添加它时,它不会找到已实现接口的上下文。这就是为什么它显示错误

答案 2 :(得分:0)

在您的A中添加此方法:

X1

答案 3 :(得分:0)

在布局xml文件中,将android:name替换为name,例如:

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

...

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

因为android:name是未知属性。