找不到方法()

时间:2017-01-08 02:37:50

标签: java android methods

我有一个XML文件,其中包含一个定义了onClick =“BeginRecall”的按钮。我已经在我的java类文件中实现了该方法,但不知何故它显示为未使用它并且当我按下找不到方法错误的按钮时它会崩溃我的应用程序。

我发现这很奇怪,因为这不是我在我的包中使用onClick的第一个实例。感谢有人可以指出我错过了什么。

这是XML:

block

这是我的Fragment_treasures课程

<RelativeLayout 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"
tools:context="fantasy.myapplication.Fragment_treasures">

<!-- TODO: Update blank fragment layout -->

... I removed the other stuff

<Button
    android:text="Begin Ancestral Recall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn_research"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="BeginRecall"/>

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/iv_research"
    android:layout_below="@+id/btn_research"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp" />

</RelativeLayout>

这是错误:

package fantasy.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
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;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Random;

import cn.pedant.SweetAlert.SweetAlertDialog;
import fantasy.myapplication.Player.Player;

import static android.app.Activity.RESULT_OK;

public class Fragment_treasures extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PLAYER = "thisplayer";
private static final int RESEARCH_COST = 501;
private Player currentplayer;

private Button btn_research;
private ImageView iv_research;
private int GENERATE_SUMMON = 1;

String[] card_common;
String[] card_novel;
String[] card_epic;
String[] card_legendary;
String[] card_event;
TypedArray card_imgs;


// Chance table
private final int chance_common = 100;
private final int chance_novel = 85;
private final int chance_epic = 90;
private final int chance_legendary = 95;
//chance_event by default would be (100 - chance_legendary)

private OnFragTreasureInteractionListener mListener;

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

public static Fragment_treasures newInstance(Player player) {
    Fragment_treasures fragment = new Fragment_treasures();
    Bundle args = new Bundle();
    args.putParcelable(ARG_PLAYER, player);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = this.getArguments();
    if (bundle != null){
        currentplayer = bundle.getParcelable(ARG_PLAYER);
    }
}

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

    Activity activity = getActivity();
    if(activity != null && isAdded()) {
        card_common = getResources().getStringArray(R.array.card_common);
        card_novel = getResources().getStringArray(R.array.card_novel);
        card_epic = getResources().getStringArray(R.array.card_epic);
        card_legendary =     getResources().getStringArray(R.array.card_legendary);
        card_event = getResources().getStringArray(R.array.card_event);


        btn_research = (Button) v.findViewById(R.id.btn_research);
        iv_research = (ImageView) v.findViewById(R.id.iv_research);
    }
    return v;
}

public void BeginRecall(View v) {

    if (currentplayer.crystals > 501) {
        final Random rand = new Random();
        int rint = rand.nextInt(100) + 1;

        if (rint <= chance_common) {
            TypedArray card_imgs = getResources().obtainTypedArray(R.array.img_card_common);
            rint = rand.nextInt(card_imgs.length());
            iv_research.setImageResource(card_imgs.getResourceId(rint, 0));
        } else if (rint <= chance_novel) {
            rint = rand.nextInt(card_novel.length);
        } else if (rint <= chance_epic) {
            rint = rand.nextInt(card_epic.length);
        } else if (rint <= chance_legendary) {
            rint = rand.nextInt(card_legendary.length);
        } else {
            rint = rand.nextInt(card_event.length);
        }

        Intent i = new Intent(getActivity(),Activity_Recall.class);
        startActivityForResult(i,GENERATE_SUMMON);

        //if (mListener != null){
        //mListener.onFragTreasureInteraction(RESEARCH_COST);
        //}
    } else {
        new SweetAlertDialog(getActivity(), SweetAlertDialog.ERROR_TYPE)
                .setTitleText("Oops...")
                .setContentText("Not enough crystals!")
                .show();
    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GENERATE_SUMMON && resultCode == RESULT_OK){
        String s = data.getStringExtra("data");
        Toast.makeText(getContext(),s, Toast.LENGTH_SHORT).show();
    }

}

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



}

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


public interface OnFragTreasureInteractionListener {
    // TODO: Update argument type and name
    void onFragTreasureInteraction(int crystals);
}
}

1 个答案:

答案 0 :(得分:0)

你的错误说你已经将xml包含在另一个[在你的基础活动中加载的片段中]让我们的父xml,并且xml不在同一个类中[当然你有它在您的片段类中而不在您的活动中]。因此,您需要在父活动中定义您的方法!

解决方案:您正在使用BeginRecall内部片段,该方法应位于加载片段的活动内!

如果您想处理片段类中的点击,请执行以下操作,而不使用您自己的方法

  public class Fragment_treasures extends Fragment implements OnClickListener{

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View v = inflater.inflate(R.layout.fragment_treasures, container, false);

            Button b = (Button) v.findViewById(R.id.StartButton);
            b.setOnClickListener(this);
            return v;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.StartButton:

                ...

                break;
            }
        }
    }

更多信息:阅读How to handle button clicks using the XML onClick within Fragments