gpt4 book ai didi

java - 扩展 AppCompatButton 的自定义按钮引发强制转换错误

转载 作者:行者123 更新时间:2023-12-02 09:27:20 25 4
gpt4 key购买 nike

我尝试制作一个扩展 AppCompatButton 的自定义按钮,但应用程序崩溃了。我使用的是 ButtonState 的一个版本,它内部有一个 AppCompatButton 对象以及一个 int 来保存状态,但我发现我可以扩展 AppCompatButton,这样我就不必像这样调用我的按钮 buttonstate.button。 method(),而是可以直接使用按钮 button.method() 以及使用额外变量来操作按钮状态的好处。

ButtonState.java

package com.example.detetiveinvestigativo.Interface.CharacterSelection;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;

class ButtonState extends AppCompatButton{
static final int NOT_SELECTED=0, SELECTED=1, CHARACTER_DEAD=2;
int state;

ButtonState(@NonNull Context c) {
super(c);
state = NOT_SELECTED;
}
}

Fragment.java

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.detetiveinvestigativo.DataManager.Entity;
import com.example.detetiveinvestigativo.Events.EventCharacterSelect;
import com.example.detetiveinvestigativo.Events.EventSwapFragment;
import com.example.detetiveinvestigativo.Game.Game;
import com.example.detetiveinvestigativo.Game.Register;
import com.example.detetiveinvestigativo.R;
import com.example.detetiveinvestigativo.Utilities.MyDrawableCompat;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import javax.annotation.Nonnegative;

public class CharacterSelectionFragment extends Fragment {
private static final String TAG = "CharacterSelectionFrag";
private static final int NONE=-1, JAILOR=0, MAGE=1, REPORTER=2, GUMSHOE=3, NURSE=4, BODYGUARD=5, WATCHMAN=6,
PARTY_HARD=7, VETERAN=8;
private Context c;
private View v;
private AppCompatTextView title;
private ButtonState[] buttonArray;
private AppCompatButton buttonReviewEvidences;
private AppCompatButton buttonProceed;
private int last_char_selected;
private int total_char_selected; // How many characters are selectedCharacter
private ArrayList<Register> actionsArray;
private Game currentGame;


public CharacterSelectionFragment() { }

@Override
public void onStart() {
super.onStart();
if(!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
}

@Override
public void onStop() {
super.onStop();
if(EventBus.getDefault().isRegistered(this)) EventBus.getDefault().unregister(this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_character_selection, container, false);
c = v.getContext();
findViewsById();
return v;
}

private void findViewsById(){
if(buttonArray == null) buttonArray = new ButtonState[9];
for(int i=0; i<buttonArray.length; i++) buttonArray[i] = new ButtonState(c);
buttonArray[JAILOR] = v.findViewById(R.id.cs_button_jailor); // <-- Error happens here "cannot cast AppCompatButton to ButtonState"
buttonArray[MAGE] = v.findViewById(R.id.cs_button_mage);
buttonArray[REPORTER] = v.findViewById(R.id.cs_button_reporter);
buttonArray[GUMSHOE] = v.findViewById(R.id.cs_button_gumshoe);
buttonArray[NURSE] = v.findViewById(R.id.cs_button_nurse);
buttonArray[BODYGUARD] = v.findViewById(R.id.cs_button_bodyguard);
buttonArray[WATCHMAN] = v.findViewById(R.id.cs_button_watchman);
buttonArray[PARTY_HARD] = v.findViewById(R.id.cs_button_party_hard);
buttonArray[VETERAN] = v.findViewById(R.id.cs_button_veteran);
buttonReviewEvidences = v.findViewById(R.id.cs_button_review_evidences);
buttonProceed = v.findViewById(R.id.cs_button_proceed);
title = v.findViewById(R.id.cs_textview_title);
}

/*... More methods below ...*/
}

frag_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".Interface.CharacterSelection.CharacterSelectionFragment"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:background="@drawable/wood_texture"
android:clickable="true"
android:focusable="false"
android:id="@+id/cs_parent_layout">

<!-- ... More buttons above ... -->

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/cs_button_jailor"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:text="@string/jailor"
android:textSize="16.5sp"
android:fontFamily="@font/joystix_monospace"/>

<!-- ... More buttons below ... -->
</androidx.appcompat.widget.LinearLayoutCompat>

异常

E/EventBus: Could not dispatch event: class com.example.detetiveinvestigativo.Events.EventSwapFragment to subscribing class class com.example.detetiveinvestigativo.MainActivity
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatButton cannot be cast to com.example.detetiveinvestigativo.Interface.CharacterSelection.ButtonState
at com.example.detetiveinvestigativo.Interface.CharacterSelection.CharacterSelectionFragment.findViewsById(CharacterSelectionFragment.java:98)
at com.example.detetiveinvestigativo.Interface.CharacterSelection.CharacterSelectionFragment.onCreateView(CharacterSelectionFragment.java:89)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl.executePendingTransactions(FragmentManagerImpl.java:183)
at com.example.detetiveinvestigativo.MainActivity.swapFragments(MainActivity.java:247)
at com.example.detetiveinvestigativo.MainActivity.onEvent(MainActivity.java:294)
at java.lang.reflect.Method.invoke(Native Method)
at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:507)
at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:434)
at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:411)
at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:384)
at org.greenrobot.eventbus.EventBus.post(EventBus.java:265)
at com.example.detetiveinvestigativo.Interface.Menu.MenuFragment$1.onClick(MenuFragment.java:53)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

我尝试使用此方法来强制转换按钮,但没有任何改变,引发了相同的异常。

buttonArray[JAILOR] = (ButtonState)v.findViewById(R.id.cs_button_jailor);

如何使 Actor 阵容有效(隐式或非隐式)?

编辑

如果您遇到与我相同的问题,请在下面查看我自己的答案,修复就在那里。

最佳答案

我通过在 ButtonState 类中插入另外两个构造函数,并将 xml 从 AppCompatButton 更改为 ButtonState 来修复它,现在我的自定义按钮类如下所示:

Java

package com.example.detetiveinvestigativo.Interface.CharacterSelection;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;

public class ButtonState extends AppCompatButton {
static final int NOT_SELECTED=0, SELECTED=1, CHARACTER_DEAD=2;
int state;

public ButtonState(@NonNull Context c) {
super(c);
state = NOT_SELECTED;
}

// Added this constructor here
public ButtonState(@NonNull Context context, AttributeSet attrs) {
super(context, attrs);
state = NOT_SELECTED;
}

// And this constructor too
public ButtonState(@NonNull Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
state = NOT_SELECTED;
}
}

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".Interface.CharacterSelection.CharacterSelectionFragment"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:background="@drawable/wood_texture"
android:clickable="true"
android:focusable="false"
android:id="@+id/cs_parent_layout">

<com.example.detetiveinvestigativo.Interface.CharacterSelection.ButtonState
android:id="@+id/cs_button_jailor"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:text="@string/jailor"
android:textSize="16.5sp"
android:fontFamily="@font/joystix_monospace"/>

</androidx.appcompat.widget.LinearLayoutCompat>

关于java - 扩展 AppCompatButton 的自定义按钮引发强制转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58253752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com