gpt4 book ai didi

java - 按钮点击导致应用程序崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:44 26 4
gpt4 key购买 nike

我刚刚设置了一个 onClick 方法,该方法编写在 Activity 类中,只要单击按钮就会调用该方法。甚至在进入方法之前,应用程序就崩溃了。

单击 float 操作按钮时收到的错误消息;

java.lang.IllegalArgumentException: Expected receiver of type com.example.bob.vexteamqueuing.AdminControl,
but got android.support.v7.view.ContextThemeWrapper java.lang.IllegalArgumentException:
Expected receiver of type com.example.bob.vexteamqueuing.AdminControl, but got android.support.v7.view.ContextThemeWrapper

at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Activity 的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/NoActionBar">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_admin_control" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add"
android:onClick="createNewTournament"
android:clickable="true" />

AdminControl Activity Java 代码:

  public class AdminControl extends AppCompatActivity {

Firebase ref;
public static List <Tournament> tournaments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
b.setTitle("Tournaments");
setSupportActionBar(b);
ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
if (tournaments == null){
tournaments = new ArrayList<>();
}
}

public void createNewTournament(View v) {
Intent newIntent = new Intent(this, TournamentCreator.class);
startActivity(newIntent);
}
}

关于创建 - 锦标赛创建者

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tournament_creator);
/*b = (FloatingActionButton) findViewById(R.id.fab);
b.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
beginTournament(v);
}
});*/
}

list 文件 - 可能不是必需的,但只是提供

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
android:name=".VEXQueuing"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Initial">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AdminLogin"
android:label="@string/title_activity_admin_login"
android:parentActivityName=".Initial">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.bob.vexteamqueuing.Initial" />
</activity>
<activity android:name=".TournamentCreator" />
<activity
android:name=".AdminControl"
android:label="@string/title_activity_admin_control"
android:parentActivityName=".Initial"
android:theme="@style/NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.bob.vexteamqueuing.Initial" />
</activity>
</application>

最佳答案

从布局中删除 android:onClick="createNewTournament" 事件应该可以解决您的问题

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add"
android:clickable="true" />

然后在您的 onCreate 中向 R.id.fab 添加一个监听器,就像这样。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
b.setTitle("Tournaments");
setSupportActionBar(b);
ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
if (tournaments == null){
tournaments = new ArrayList<>();
}

FloatingActionButton myFab = (FloatingActionButton)findViewById(R.id.fab);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNewTournament(v);
}
});
}

同样的问题出现在这个问题EditText OnClick Exception并使用监听器修复。

希望这对您有所帮助!

关于java - 按钮点击导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37735852/

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