简体   繁体   English

Android Studio OnClickListener 不起作用

[英]the Android Studio OnClickListener doesn't work

Nothing happens when I click the "start" button, the app suddenly closes.当我单击“开始”按钮时没有任何反应,应用程序突然关闭。 I think the View.OnClickListener doesn't work, but I can't find how to fix it.我认为 View.OnClickListener 不起作用,但我找不到解决方法。

package com.example.myquiz;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView title;
    private Button start;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        title = findViewById(R.id.main_title);
        start =  findViewById(R.id.ma_startB);

        Typeface typeface = ResourcesCompat.getFont(this,R.font.blacklist);
        title.setTypeface(typeface);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,CategoryActivity.class);
                startActivity(intent);
            }
        });
    }
}

I got this error in Logcat: error screenshot我在 Logcat 中遇到了这个错误:错误截图

This is the code for the CategoryActivity class, this is where I get the error from, but I really don't see where the problem is.这是 CategoryActivity class 的代码,这是我得到错误的地方,但我真的不知道问题出在哪里。

package com.example.myquiz;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class CategoryActivity extends AppCompatActivity {
    private  GridView catGrid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Categories");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        catGrid = findViewById(R.id.catGridView);

        List<String> catList = new ArrayList<>();

        catList.add("Cat 1");
        catList.add("Cat 2");
        catList.add("Cat 3");
        catList.add("Cat 4");
        catList.add("Cat 5");
        catList.add("Cat 6");

        CatGridAdapter adapter = new CatGridAdapter(catList);
        catGrid.setAdapter(adapter);

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == android.R.id.home)
        { CategoryActivity.this.finish();}
        return super.onOptionsItemSelected(item);
    }
}

The click listener is working correct, looking at the error log the launching of the CategoryActivity class is causing the app crash:点击监听器工作正常,查看错误日志, CategoryActivity class 的启动导致应用程序崩溃:

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor...

This tells the ActionBar / ToolBar inside the CategoryActivity is setup twice.这告诉CategoryActivity内的ActionBar / ToolBar设置了两次。 Probably (if not, please share the class code) you can either remove setSupportActionBar() or change the theme of the CategoryActivity to something like: Theme.MaterialComponents.Light.NoActionBar to see if you get the Activity running after the button click.可能(如果没有,请分享 class 代码)您可以删除setSupportActionBar()或将CategoryActivity的主题更改为: Theme.MaterialComponents.Light.NoActionBar以查看您是否在单击按钮后运行Activity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM