简体   繁体   English

错误:java.lang.IllegalStateException:无法执行android:onClick的方法

[英]Error: java.lang.IllegalStateException: Could not execute method for android:onClick

I'm new here and also new to android development, just started learning, covered a few concepts and started working on my first app, but sigh, errors errors everywhere. 我是这里的新手,也是android开发的新手,刚开始学习,介绍了一些概念,并开始使用我的第一个应用程序,但是很遗憾,到处都是错误。 I googled but didn't find a solution I could understand, maybe because I'm new and the other stack overflow pages I saw had overwhelming code and solutions 我用谷歌搜索,但是没有找到我能理解的解决方案,也许是因为我是新手,而我看到的其他堆栈溢出页面中却包含了压倒性的代码和解决方案

On my first app I was trying to build, errors started right away. 在我尝试构建的第一个应用程序中,错误立即开始。

Here is my log: 这是我的日志:

    06/26 09:43:01: Launching app
    Cold swapped changes.
    $ adb shell am start -n "com.example.android.cricketcounter/com.example.android.cricketcounter.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
    Waiting for application to come online: com.example.android.cricketcounter.test | com.example.android.cricketcounter
    Waiting for application to come online: com.example.android.cricketcounter.test | com.example.android.cricketcounter
    Waiting for application to come online: com.example.android.cricketcounter.test | com.example.android.cricketcounter
    Connecting to com.example.android.cricketcounter
    W/ActivityThread: Application com.example.android.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.android.cricketcounter-1/lib/x86
    I/InstantRun: Instant Run Runtime started. Android package is com.example.android.cricketcounter, real application class is null.
    W/System: ClassLoader referenced unknown path: /data/app/com.example.android.cricketcounter-1/lib/x86
    W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.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 android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
    I/art: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.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.android.cricketcounter, PID: 5001
                      java.lang.IllegalStateException: Could not execute method for android:onClick
                          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                          at android.view.View.performClick(View.java:5610)
                          at android.view.View$PerformClick.run(View.java:22265)
                          at android.os.Handler.handleCallback(Handler.java:751)
                          at android.os.Handler.dispatchMessage(Handler.java:95)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                       Caused by: java.lang.reflect.InvocationTargetException
                          at java.lang.reflect.Method.invoke(Native Method)
                          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                          at android.view.View.performClick(View.java:5610) 
                          at android.view.View$PerformClick.run(View.java:22265) 
                          at android.os.Handler.handleCallback(Handler.java:751) 
                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                          at android.os.Looper.loop(Looper.java:154) 
                          at android.app.ActivityThread.main(ActivityThread.java:6077) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                          at com.example.android.cricketcounter.MainActivity.runsScored(MainActivity.java:20)
                          at com.example.android.cricketcounter.MainActivity.addSix(MainActivity.java:25)
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                          at android.view.View.performClick(View.java:5610) 
                          at android.view.View$PerformClick.run(View.java:22265) 
                          at android.os.Handler.handleCallback(Handler.java:751) 
                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                          at android.os.Looper.loop(Looper.java:154) 
                          at android.app.ActivityThread.main(ActivityThread.java:6077) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
    Disconnected from the target VM, address: 'localhost:8611', transport: 'socket'

Here is the xml file: 这是xml文件:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.cricketcounter.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Cricket scores"
            android:textSize="20dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Score: "
                android:textSize="40dp" />

            <TextView
                android:id="@+id/runs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="/"
                android:textSize="40dp" />

            <TextView
                android:id="@+id/wickets"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="40dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Balls: "
                android:textSize="40dp" />

            <TextView
                android:id="@+id/balls"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="40dp" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/reset_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Reset" />

    <LinearLayout
        android:id="@+id/other_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/reset_button">

        <Button
            android:id="@+id/six_runs"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="addSix"
            android:text="+6" />


        <Button
            android:id="@+id/no_ball"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="No ball"
            android:textSize="13sp" />


        <Button
            android:id="@+id/wide_ball"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Wide" />


        <Button
            android:id="@+id/out"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="out" />


        <Button
            android:id="@+id/dot_ball"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Dot" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/runs_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/other_buttons">

        <Button
            android:id="@+id/one_run"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+1" />


        <Button
            android:id="@+id/two_runs"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+2" />


        <Button
            android:id="@+id/three_runs"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+3" />


        <Button
            android:id="@+id/four_runs"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+4" />


        <Button
            android:id="@+id/five_runs"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+5" />

    </LinearLayout>


</RelativeLayout>

And the java file: 和java文件:

    package com.example.android.cricketcounter;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.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);
        }

}

Note: The java file is still incomplete, but enough to make one of the buttons work 注意:Java文件仍然不完整,但是足以使其中一个按钮起作用

NullPointerException occurred here: NullPointerException发生在这里:

TextView scoreView = (TextView) findViewById(runs);

in findViewById,the parameter should be the id of Textview and it should be used as follows 在findViewById中,该参数应为Textview的ID,并应按以下方式使用

TextView scoreView = (TextView) findViewById(R.id.runs);

暂无
暂无

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

相关问题 java.lang.IllegalStateException:无法为 android 执行方法:onClick / ZE84E30B9390CDB67ZDDB6 - java.lang.IllegalStateException: Could not execute method for android:onClick / Android java.lang.IllegalStateException:无法执行android:onClick的方法 - java.lang.IllegalStateException: Could not execute method for android:onClick in android Kotlin-java.lang.IllegalStateException:无法执行android:onClick的方法 - Kotlin - java.lang.IllegalStateException: Could not execute method for android:onClick Android Studio 3.1.3 中的错误。 java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - Error in Android Studio 3.1.3. java.lang.IllegalStateException: Could not execute method for android:onClick ANDROID意图致命错误!!! java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - ANDROID INTENTION FATAL ERROR!!! java.lang.IllegalStateException: Could not execute method for android:onClick Tictac toe 游戏不断停止并出现 logcat 错误 java.lang.IllegalStateException:Could not execute method for android:onClick - Tictac toe game keeps stopping with logcat error java.lang.IllegalStateException:Could not execute method for android:onClick 代码错误:java.lang.IllegalStateException:无法执行android:onClick的方法 - Error in code: java.lang.IllegalStateException: Could not execute method for android:onClick 得到运行时错误“ java.lang.IllegalStateException:无法执行android:onClick的方法” - getting runtime error “java.lang.IllegalStateException: Could not execute method for android:onClick” Android-Java:java.lang.IllegalStateException:无法执行 android 的方法:onClick - Android-Java: java.lang.IllegalStateException: Could not execute method for android:onClick Android Studio 问题; java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - Android Studio problem ; java.lang.IllegalStateException: Could not execute method for android:onClick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM