收到“java.lang.IllegalStateException:无法执行android的方法:onClick”

时间:2017-12-20 20:23:48

标签: android nullpointerexception onclick

错误......

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vaidish.myapplication, PID: 12727
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:6256)
    at android.view.View$PerformClick.run(View.java:24697)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6567)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
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:6256) 
    at android.view.View$PerformClick.run(View.java:24697) 
    at android.os.Handler.handleCallback(Handler.java:789) 
    at android.os.Handler.dispatchMessage(Handler.java:98) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6567) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:224)
    at android.app.AlertDialog$Builder.<init>(AlertDialog.java:454)
    at com.example.vaidish.myapplication.Background.<init>(Background.java:25)
    at com.example.vaidish.myapplication.MainActivity.userLogin(MainActivity.java:56)
    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:6256) 
    at android.view.View$PerformClick.run(View.java:24697) 
    at android.os.Handler.handleCallback(Handler.java:789) 
    at android.os.Handler.dispatchMessage(Handler.java:98) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6567) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

1 个答案:

答案 0 :(得分:0)

崩溃的根本原因是:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:224)
    at android.app.AlertDialog$Builder.<init>(AlertDialog.java:454)
    at com.example.vaidish.myapplication.Background.<init>(Background.java:25)

这表示您已将null传递给AlertDialog.Builder构造函数。您可以明确地这样做,例如:

new AlertDialog.Builder(null)

或者你可能无意中这样做,例如:

new AlertDialog.Builder(mContext)

检查Background类的第25行,并确保将非空Context实例传递给构建器。