gpt4 book ai didi

java.lang.IllegalStateException : Could not find method

转载 作者:行者123 更新时间:2023-12-02 22:19:13 25 4
gpt4 key购买 nike

我认为这是一个愚蠢的错误,而且我是编程新手。我正在制作一个应用程序来学习:有 2 个单选按钮用于选择隐藏 TextView 中显示的文本,2 个复选框用于选择 TextView 的背景和文本颜色,还有一个按钮,用于检查复选框和单选按钮并打印 TextView 中的文本。但是当我在模拟器中尝试单击某些内容时,应用程序崩溃了,并且 logcat 说它找不到 id 和 onClick。为什么我会得到这个?

程序:

package com.bauapp.lory.bauapp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
import android.graphics.Color;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
public void cambiaTesto (View v) {



TextView baubau = (TextView) findViewById(R.id.bauText);
RadioButton bau1 = (RadioButton) findViewById(R.id.bauRadio1);
RadioButton bau2 = (RadioButton) findViewById(R.id.bauRadio2);
CheckBox testoRosso = (CheckBox)findViewById(R.id.c1);
CheckBox sfondoGiallo = (CheckBox)findViewById(R.id.c2);

if(bau1.isChecked()) {
baubau.setText("BAU");
}
else
if (bau2.isChecked()) {
baubau.setText("BAUUUUUUU");
}

if(testoRosso.isChecked())
baubau.setTextColor(Color.RED);

else
baubau.setTextColor(Color.BLACK);
if (sfondoGiallo.isChecked())
baubau.setBackgroundColor(Color.YELLOW);

else
baubau.setBackgroundColor(Color.WHITE);


}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bauapp.lory.bauapp.MainActivity"
tools:showIn="@layout/activity_main">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="43dp">

<TextView
android:layout_width="342dp"
android:layout_height="191dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/bauText"
android:layout_gravity="center_horizontal|bottom"
android:onClick="baubau" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testo rosso"
android:id="@+id/c1"
android:layout_gravity="left|center_vertical"
android:onClick="testoRosso"
android:checked="false"
android:clickable="true" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sfondo giallo"
android:id="@+id/c2"
android:layout_gravity="right|center_vertical"
android:onClick="sfondoGiallo"
android:clickable="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAU BAU?"
android:id="@+id/bauButton"
android:layout_gravity="right|top"
android:onClick="cambiaTesto" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAU"
android:id="@+id/bauRadio1"
android:layout_gravity="left|top"
android:onClick="bau1"
android:checked="false"
android:clickable="true" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAUUUUUUU"
android:id="@+id/bauRadio2"
android:layout_gravity="left|top"
android:onClick="bau2"
android:checked="false"
android:clickable="true" />

</RadioGroup>

</FrameLayout>

和日志猫:

http://textuploader.com/5blrn

(我留下了一个外部链接,因为它没有正确粘贴以解决 stackoverflow 中的错误)。

所有其他按钮的崩溃 logcat 都是相同的,但显然,更改 ID 和 OnClick。抱歉我的英语不好。

最佳答案

我的建议是从 RadioButton 中删除 android:onClick=... ,并在 RadioGroup 上使用 onCheckedChangeListener ,然后确定检查了哪个 RadioButton ,然后执行您需要执行的操作。您的代码更改如下:

<RadioGroup
android:id="@+id/bauRadioGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAU"
android:id="@+id/bauRadio1"
android:layout_gravity="left|top"
android:checked="false"
android:clickable="true" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BAUUUUUUU"
android:id="@+id/bauRadio2"
android:layout_gravity="left|top"
android:checked="false"
android:clickable="true" />
</RadioGroup>

然后在您的 MainActivity onCreate 中添加以下代码:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.bauRadioGroup);        
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.bauRadio1){
//radio button bauRadio1 was clicked
baubau.setText("BAU");
}
else if(checkedId == R.id.bauRadio2 ){
//radio button bauRadio2 was clicked
baubau.setText("BAUUUUUUU");
}
}
});
//The rest of your code ...

请尝试一下,并告诉我们这是否有帮助。正如已经建议的,How to set On click listener on the Radio Button in Android就是一个很好的例子。

关于java.lang.IllegalStateException : Could not find method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397261/

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