java 无法执行 Activity 的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9947428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not execute method of Activity
提问by Exorcist
Well I have set of buttons and their onClick
property is "click" like this android:onClick="click"
. I am running this in android 2.2
好吧,我有一组按钮,它们的onClick
属性是这样的“单击” android:onClick="click"
。我在 android 2.2 中运行这个
Here's the click function in the main activity. This function is outside the onCreate
method as expected.
这是主活动中的点击功能。onCreate
正如预期的那样,此函数在方法之外。
public void click(View v)
{
String s = et2.getText().toString();
switch(v.getId())
{
case R.id.b0 :
if(s.length() == 1 && s.charAt(0) == '0')
{
et2.setText("0");
et1.setText("0");
}
else
{
s = s + "0";
call(s);
}
break;
//Other cases
}
}
The problem is whenever I click any button, the app stops working (Force Close).
问题是每当我单击任何按钮时,应用程序都会停止工作(强制关闭)。
Here's the Log:
这是日志:
03-30 22:23:20.129: E/AndroidRuntime(1240): FATAL EXCEPTION: main 03-30 22:23:20.129: E/AndroidRuntime(1240): java.lang.IllegalStateException: Could not execute method of the activity 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View$1.onClick(View.java:3044) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View.performClick(View.java:3511) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View$PerformClick.run(View.java:14105) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.os.Handler.handleCallback(Handler.java:605) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.os.Handler.dispatchMessage(Handler.java:92) 03-30 22:23:20.129: E/AndroidRuntime(1240): at
android.os.Looper.loop(Looper.java:137) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.app.ActivityThread.main(ActivityThread.java:4424) 03-30 22:23:20.129: E/AndroidRuntime(1240): at java.lang.reflect.Method.invokeNative(Native Method) 03-30 22:23:20.129: E/AndroidRuntime(1240): at java.lang.reflect.Method.invoke(Method.java:511) 03-30 22:23:20.129: E/AndroidRuntime(1240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 03-30 22:23:20.129: E/AndroidRuntime(1240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 03-30 22:23:20.129: E/AndroidRuntime(1240): at dalvik.system.NativeStart.main(Native Method) 03-30 22:23:20.129: E/AndroidRuntime(1240): Caused by: java.lang.reflect.InvocationTargetException 03-30 22:23:20.129: E/AndroidRuntime(1240): at java.lang.reflect.Method.invokeNative(Native Method) 03-30 22:23:20.129: E/AndroidRuntime(1240): at java.lang.reflect.Method.invoke(Method.java:511) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View$1.onClick(View.java:3039) 03-30 22:23:20.129: E/AndroidRuntime(1240): ... 11 more 03-30 22:23:20.129: E/AndroidRuntime(1240): Caused by: java.lang.NullPointerException 03-30 22:23:20.129: E/AndroidRuntime(1240): at yadav.sanjay.calculator.CalculatorActivity.click(CalculatorActivity.java:63)
03-30 22:23:20.129: E/AndroidRuntime(1240): 致命异常: main 03-30 22:23:20.129: E/AndroidRuntime(1240): java.lang.IllegalStateException: 无法执行活动 03 的方法-30 22:23:20.129: E/AndroidRuntime(1240): 在 android.view.View$1.onClick(View.java:3044) 03-30 22:23:20.129: E/AndroidRuntime(1240): 在 android. view.View.performClick(View.java:3511) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View$PerformClick.run(View.java:14105) 03-30 22: 23:20.129: E/AndroidRuntime(1240): 在 android.os.Handler.handleCallback(Handler.java:605) 03-30 22:23:20.129: E/AndroidRuntime(1240): 在 android.os.Handler.dispatchMessage (Handler.java:92) 03-30 22:23:20.129: E/AndroidRuntime(1240): at
android.os.Looper.loop(Looper.java:137) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.app.ActivityThread.main(ActivityThread.java:4424) 03-30 22: 23:20.129: E/AndroidRuntime(1240): 在 java.lang.reflect.Method.invokeNative(Native Method) 03-30 22:23:20.129: E/AndroidRuntime(1240): 在 java.lang.reflect.Method。 invoke(Method.java:511) 03-30 22:23:20.129: E/AndroidRuntime(1240): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 03-30 22: 23:20.129: E/AndroidRuntime(1240): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 03-30 22:23:20.129: E/AndroidRuntime(1240): 在 dalvik.system .NativeStart.main(Native Method) 03-30 22:23:20.129: E/AndroidRuntime(1240): 引起: java.lang.reflect.InvocationTargetException 03-30 22:23:20.129:E/AndroidRuntime(1240): 在 java.lang.reflect.Method.invokeNative(Native Method) 03-30 22:23:20.129: E/AndroidRuntime(1240): 在 java.lang.reflect.Method.invoke(Method. java:511) 03-30 22:23:20.129: E/AndroidRuntime(1240): at android.view.View$1.onClick(View.java:3039) 03-30 22:23:20.129: E/AndroidRuntime(1240) ): ... 11 more 03-30 22:23:20.129: E/AndroidRuntime(1240): 引起: java.lang.NullPointerException 03-30 22:23:20.129: E/AndroidRuntime(1240): at yadav。 sanjay.calculator.CalculatorActivity.click(CalculatorActivity.java:63)E/AndroidRuntime(1240): ... 11 more 03-30 22:23:20.129: E/AndroidRuntime(1240): 引起:java.lang.NullPointerException 03-30 22:23:20.129: E/AndroidRuntime(1240) ): 在 yadav.sanjay.calculator.CalculatorActivity.click(CalculatorActivity.java:63)E/AndroidRuntime(1240): ... 11 more 03-30 22:23:20.129: E/AndroidRuntime(1240): 引起:java.lang.NullPointerException 03-30 22:23:20.129: E/AndroidRuntime(1240) ): 在 yadav.sanjay.calculator.CalculatorActivity.click(CalculatorActivity.java:63)
回答by antonio081014
Caused by: java.lang.NullPointerException, Usually, this means your staff is not initialized yet. Check the code around Line 63.
引起:java.lang.NullPointerException,通常,这意味着您的员工尚未初始化。检查第 63 行周围的代码。
回答by Tiago Almeida
Check if your et2
is inicialized. If you are using findViewById()
please check if you do that after the setContent()
.
检查您et2
是否已初始化。如果您正在使用,findViewById()
请检查您是否在setContent()
.
回答by JXPheonix
Try changing the code on line 63 into two separate lines, like this.
尝试将第 63 行的代码更改为两个单独的行,如下所示。
String s; //you should probably change s to something less common like calS or the like
s = et2.getText().toString();
I think another thing that could be the problem is that et2 was never initialized. Make sure you have initialized et2.
我认为可能是问题的另一件事是 et2 从未初始化。确保您已初始化 et2。