gpt4 book ai didi

java - Button 对象崩溃和挂起过程中调用的方法

转载 作者:行者123 更新时间:2023-12-02 04:03:46 25 4
gpt4 key购买 nike

我正在尝试运行一个小方法,该方法在 Android Studio 中的 Button 实例上运行 setTextsetEnabled 。问题是前者导致崩溃,后者导致进程挂起/卡住。日志指向 NullPointerException,但是我在调​​用它之前创建了该对象。先写代码...

package com.example.richardcurteis.connect3;

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.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import java.util.Random;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

public boolean noughtsTurn;
ArrayList board;
Players player;

public void receiveClick(View view) {
String buttonPressed = (String) view.getTag();
board.remove(buttonPressed);

if (view instanceof Button) {
Button b = (Button) view;
b.setText(noughtsTurn ? player.noughtsPlayer() : player.crossesPlayer()); // Crashes program
b.setEnabled(false); // Hangs program
System.out.println(board);
}
}

public class Players {
public String noughtsPlayer() { return "O"; }
public String crossesPlayer() { return "X"; }
public String blankButton() { return ""; }
}

@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();
}
});
boolean noughtsTurn = true;
board = new ArrayList();
for (int x = 0; x < getBoardSize(); x++) {
String y = String.valueOf(x);
board.add(y);
}
Players player = new Players();

}

public int getBoardSize() {
int buttonCount = 0;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);

for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
View tableLayoutChild = tableLayout.getChildAt(rowIndex);
if (tableLayoutChild instanceof TableRow) {
for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
View view = ((ViewGroup) tableLayoutChild).getChildAt(i);
if (view instanceof Button) {
buttonCount++;
}
}
}
}
return buttonCount;
}

@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);
}
}

如您所见,我无法从第一组日志中看出太多信息。我相当确定按钮是这里的问题,因为 setText() 崩溃日志中的这一行:在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) connectMainActivity$Players.crossesPlayer()' 上的空对象引用 receiveClicMainActivity.java:30)

setEnabled() 方法的挂起/卡住日志

01-04 15:59:19.322 10586-10586/? I/art: Not late-enabling -Xcheck:jni (already on)
01-04 15:59:19.464 10586-10592/? I/art: Debugger is no longer active
01-04 15:59:19.471 10586-10586/? W/System: ClassLoader referenced unknown path: /data/app/com.example.richardcurteis.connect3-2/lib/x86
01-04 15:59:19.952 10586-10604/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-04 15:59:20.015 10586-10604/? I/OpenGLRenderer: Initialized EGL, version 1.4
01-04 15:59:20.067 10586-10604/? W/EGL_emulation: eglSurfaceAttrib not implemented
01-04 15:59:20.068 10586-10604/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabe3fec0, error=EGL_SUCCESS
01-04 15:59:20.813 10586-10586/com.example.richardcurteis.connect3 I/Choreographer: Skipped 37 frames! The application may be doing too much work on its main thread.
01-04 15:59:23.066 10586-10586/com.example.richardcurteis.connect3 I/System.out: [1, 2, 3, 4, 5, 6, 7, 8]

setText() 方法崩溃的日志

01-04 15:47:45.251 10356-10356/com.example.richardcurteis.connect3 I/art: Not late-enabling -Xcheck:jni (already on)
01-04 15:47:45.561 10356-10356/com.example.richardcurteis.connect3 W/System: ClassLoader referenced unknown path: /data/app/com.example.richardcurteis.connect3-1/lib/x86
01-04 15:47:45.949 10356-10367/com.example.richardcurteis.connect3 I/art: Background sticky concurrent mark sweep GC freed 22272(1011KB) AllocSpace objects, 0(0B) LOS objects, 71% free, 1048KB/3MB, paused 2.279ms total 154.536ms
01-04 15:47:45.956 10356-10367/com.example.richardcurteis.connect3 W/art: Suspending all threads took: 7.014ms
01-04 15:47:46.021 10356-10386/com.example.richardcurteis.connect3 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-04 15:47:46.262 10356-10386/com.example.richardcurteis.connect3 I/OpenGLRenderer: Initialized EGL, version 1.4
01-04 15:47:46.316 10356-10386/com.example.richardcurteis.connect3 W/EGL_emulation: eglSurfaceAttrib not implemented
01-04 15:47:46.316 10356-10386/com.example.richardcurteis.connect3 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad75fcc0, error=EGL_SUCCESS
01-04 15:47:50.647 10356-10356/com.example.richardcurteis.connect3 D/AndroidRuntime: Shutting down VM
01-04 15:47:50.648 10356-10356/com.example.richardcurteis.connect3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.richardcurteis.connect3, PID: 10356
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
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)ruZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method) onClicAppCompatViewInflater.java:270)
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) ruZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) connectMainActivity$Players.crossesPlayer()' on a null object reference receiveClicMainActivity.java:30)
at java.lang.reflect.Method.invoke(Native Method) onClicAppCompatViewInflater.java:270) 
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) ruZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
01-04 15:47:53.044 10356-10356/? I/Process: Sending signal. PID: 10356 SIG: 9

最佳答案

Players player = new Players();

这将创建一个名为 player 的局部变量,它与对象的 player 字段具有相同的名称。在 onCreate 方法中,声明此局部变量,然后立即让它超出范围,从而丢失值。同时,您的 player 字段仍未初始化。

要修复错误,请将其更改为:

player = new Players();

这会设置 player 字段,而不是新的局部变量。

关于java - Button 对象崩溃和挂起过程中调用的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34595236/

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