gpt4 book ai didi

android - java.lang.IllegalStateExeception : could not find method in activity class 错误

转载 作者:行者123 更新时间:2023-11-29 17:42:27 25 4
gpt4 key购买 nike

我不确定为什么,但我得到了这个异常:

java.lang.IllegalStateException: Could not find a method onButtonClick(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'button19'

在我的应用程序中,我不知道如何修复它。出于某种原因,它只会在平板电脑布局中引发此异常。有什么建议吗?

这是layout.xml

<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl"
android:theme="@style/Base.Theme.AppCompat.Light">

<com.example.czhou.myapplication2.SudokuBoardView
android:id="@+id/board"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:mCellSize="40dp"
custom:mOrientationLandscape="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="1"
android:id="@+id/button10"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/board"
android:layout_toEndOf="@+id/board" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="2"
android:id="@+id/button11"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_below="@+id/button10"
android:layout_toRightOf="@+id/board"
android:layout_toEndOf="@+id/board" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="3"
android:id="@+id/button12"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_below="@+id/button11"
android:layout_toRightOf="@+id/board"
android:layout_toEndOf="@+id/board" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="4"
android:id="@+id/button13"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_below="@+id/button12"
android:layout_toRightOf="@+id/board" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="5"
android:id="@+id/button14"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_below="@+id/button13"
android:layout_toRightOf="@+id/board" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="6"
android:id="@+id/button15"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_below="@+id/button14"
android:layout_toRightOf="@+id/board" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="7"
android:id="@+id/button16"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_below="@+id/button15"
android:layout_toRightOf="@+id/board" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="8"
android:id="@+id/button17"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_below="@+id/button16"
android:layout_toRightOf="@+id/board" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="9"
android:id="@+id/button18"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_below="@+id/button17"
android:layout_toRightOf="@+id/board"
android:layout_toEndOf="@+id/board" />

<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Save &amp; Quit"
android:id="@+id/button19"
android:background="@drawable/button_background"
android:onClick="onButtonClick"
android:layout_below="@+id/eraser"
android:layout_toRightOf="@+id/button10"
android:layout_toEndOf="@+id/button10" />

<ImageButton
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/eraser"
android:src="@drawable/ic_action_eraser"
android:background="@drawable/button_background"
android:clickable="true"
android:onClick="onImageButtonClick"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/button19"
android:layout_alignStart="@+id/button19" />
</RelativeLayout>

这是 Activity 课

package com.example.czhou.myapplication2;

import android.app.FragmentManager;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class SudokuGameActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
SudokuBoardView sudokuBoardView = (SudokuBoardView) findViewById(R.id.board);
sudokuBoardView.setmFileName(this.getIntent().getStringExtra("FILE"));

}




public void onButtonClick(View v) {
Log.i("Button", "Button clicked!");

Button b = (Button) findViewById(v.getId());
Log.i("Button", b.getText().toString());

Log.i("Button", "in the loop!");
SudokuBoardView sudokuBoardView = (SudokuBoardView) findViewById(R.id.board);
if(b.getText().equals("Save & Quit")) {
saveGame();
finish();
}else {
sudokuBoardView.setCellValue(Integer.parseInt(b.getText().toString()));
}
}
public void onImageButtonClick(View v){
ImageButton ib = (ImageButton) findViewById(v.getId());
if (ib.getId() == R.id.eraser){
SudokuBoardView sudokuBoardView = (SudokuBoardView) findViewById(R.id.board);
sudokuBoardView.setCellValue(0);

} else {

}
}

public void saveGame(){
FileOutputStream outputStream;

String fn = getIntent().getStringExtra("FILE");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (SudokuBoardView.cells[i][j].isEditable){
sb.append(",");
} else {
sb.append(".");
}
if (SudokuBoardView.cells[i][j].isWrong){
sb.append("!");
} else {
sb.append(".");
}

sb.append(SudokuBoardView.cells[i][j].getValue());
}
}

try {

outputStream = openFileOutput(fn, Context.MODE_PRIVATE);
outputStream.write(sb.toString().getBytes());
outputStream.close();
System.out.println("Saving... ");
} catch (Exception e) {
e.printStackTrace();
}
}


}

最佳答案

我有一个非常相似的问题,它只发生在 Android L 上,而它在旧版本上运行良好。看起来像是 5.0 中的错误或未记录的功能。

对我有用的解决方案:

remove android:theme="@style/Base.Theme.AppCompat.Light"

来自您的 layout.xml。像这样在 AndroidManifest.xml 应用程序元素中定义您的应用程序主题:

<application 
...
android:theme="@android:style/Theme.Holo.Light"
... >

关于android - java.lang.IllegalStateExeception : could not find method in activity class 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28126771/

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