错误:java。lang.IllegalStateException:无法为安卓执行方法:onClick
我是新来的,也是安卓开发的新手,刚刚开始学习, 涵盖了一些概念并开始开发我的第一个应用程序,但遗憾的是,出现了一些错误 到处都是错误。我在谷歌上搜索,但没有找到我能理解的解决方案, 可能是因为我是新来的,而我看到的其他堆栈溢出页面 压倒性的代码和解决方案
在我尝试构建的第一个应用程序中,错误立即开始
这是我的日志:
06/26 09:43:01: Launching app
Cold swapped changes.
$ adb shell am start -n "com.example.安卓.cricketcounter/com.example.安卓.cricketcounter.MainActivity" -a 安卓.intent.action.MAIN -c 安卓.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.安卓.cricketcounter.test | com.example.安卓.cricketcounter
Waiting for application to come online: com.example.安卓.cricketcounter.test | com.example.安卓.cricketcounter
Waiting for application to come online: com.example.安卓.cricketcounter.test | com.example.安卓.cricketcounter
Connecting to com.example.安卓.cricketcounter
W/ActivityThread: Application com.example.安卓.cricketcounter is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/art: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8611', transport: 'socket'
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1499)
W/System: ClassLoader referenced unknown path: /data/app/com.example.安卓.cricketcounter-1/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.example.安卓.cricketcounter, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.example.安卓.cricketcounter-1/lib/x86
W/art: Before Android 4.1, method 安卓.graphics.PorterDuffColorFilter 安卓.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(安卓.graphics.PorterDuffColorFilter, 安卓.content.res.ColorStateList, 安卓.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in 安卓.graphics.drawable.Drawable
I/art: Do partial code cache collection, code=18KB, data=21KB
I/art: After code cache collection, code=18KB, data=21KB
I/art: Increasing code cache capacity to 128KB
I/art: Do partial code cache collection, code=18KB, data=40KB
I/art: After code cache collection, code=18KB, data=40KB
I/art: Increasing code cache capacity to 256KB
I/art: JIT allocated 61KB for compiled code of void 安卓.widget.TextView.<init>(安卓.content.Context, 安卓.util.AttributeSet, int, int)
I/art: Compiler allocated 4MB to compile void 安卓.widget.TextView.<init>(安卓.content.Context, 安卓.util.AttributeSet, int, int)
[ 06-26 09:43:05.790 5001: 5001 D/ ]
HostConnection::get() New Host Connection established 0xa6c3a180, tid 5001
[ 06-26 09:43:05.865 5001: 5020 D/ ]
HostConnection::get() New Host Connection established 0xa6ccb9c0, tid 5020
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.安卓.cricketcounter, PID: 5001
java.lang.IllegalStateException: Could not execute method for 安卓:onClick
at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at 安卓.view.View.performClick(View.java:5610)
at 安卓.view.View$PerformClick.run(View.java:22265)
at 安卓.os.Handler.handleCallback(Handler.java:751)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:154)
at 安卓.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at 安卓.view.View.performClick(View.java:5610)
at 安卓.view.View$PerformClick.run(View.java:22265)
at 安卓.os.Handler.handleCallback(Handler.java:751)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:154)
at 安卓.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.安卓.cricketcounter.MainActivity.runsScored(MainActivity.java:20)
at com.example.安卓.cricketcounter.MainActivity.addSix(MainActivity.java:25)
at java.lang.reflect.Method.invoke(Native Method)
at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at 安卓.view.View.performClick(View.java:5610)
at 安卓.view.View$PerformClick.run(View.java:22265)
at 安卓.os.Handler.handleCallback(Handler.java:751)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:154)
at 安卓.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Disconnected from the target VM, address: 'localhost:8611', transport: 'socket'
以下是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/activity_main"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.安卓.cricketcounter.MainActivity">
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical">
<TextView
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:gravity="center_horizontal"
安卓:text="Cricket scores"
安卓:textSize="20dp" />
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:gravity="center_horizontal">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Score: "
安卓:textSize="40dp" />
<TextView
安卓:id="@+id/runs"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="0"
安卓:textSize="40dp" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="/"
安卓:textSize="40dp" />
<TextView
安卓:id="@+id/wickets"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="0"
安卓:textSize="40dp" />
</LinearLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:gravity="center_horizontal">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Balls: "
安卓:textSize="40dp" />
<TextView
安卓:id="@+id/balls"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="0"
安卓:textSize="40dp" />
</LinearLayout>
</LinearLayout>
<Button
安卓:id="@+id/reset_button"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentBottom="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="40dp"
安卓:text="Reset" />
<LinearLayout
安卓:id="@+id/other_buttons"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_above="@id/reset_button">
<Button
安卓:id="@+id/six_runs"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:onClick="addSix"
安卓:text="+6" />
<Button
安卓:id="@+id/no_ball"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="No ball"
安卓:textSize="13sp" />
<Button
安卓:id="@+id/wide_ball"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="Wide" />
<Button
安卓:id="@+id/out"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="out" />
<Button
安卓:id="@+id/dot_ball"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="Dot" />
</LinearLayout>
<LinearLayout
安卓:id="@+id/runs_buttons"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_above="@id/other_buttons">
<Button
安卓:id="@+id/one_run"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="+1" />
<Button
安卓:id="@+id/two_runs"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="+2" />
<Button
安卓:id="@+id/three_runs"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="+3" />
<Button
安卓:id="@+id/four_runs"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="+4" />
<Button
安卓:id="@+id/five_runs"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:text="+5" />
</LinearLayout>
</RelativeLayout>
和java文件:
package com.example.安卓.cricketcounter;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.view.View;
import 安卓.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int runs = 0;
public void runsScored(int runsa) {
TextView scoreView = (TextView) findViewById(runs);
scoreView.setText(String.valueOf(runsa));
}
public void addSix(View v) {
runs = runs + 6;
runsScored(runs);
}
}
注意:java文件仍然不完整,但足以使其中一个按钮工作
# 1 楼答案
此处发生NullPointerException:
在findViewById中,参数应该是Textview的id,并且应该按如下方式使用