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

时间:2017-06-26 04:58:17

标签: java android

我是新来的,也是Android开发的新手,刚刚开始学习, 涵盖了一些概念并开始研究我的第一个应用程序,但感叹,错误 到处都是错误我用Google搜索但没有找到我能理解的解决方案, 也许是因为我是新的以及我看到的其他堆栈溢出页面 压倒性的代码和解决方案

在我尝试构建的第一个应用程序上,错误立即开始。

这是我的日志:

    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'

这是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>

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

}

注意:java文件仍然不完整,但足以使其中一个按钮起作用

1 个答案:

答案 0 :(得分:4)

此处发生NullPointerException:

TextView scoreView = (TextView) findViewById(runs);
在findViewById中,参数应该是Textview的id,它应该按如下方式使用

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