我正在创建一个简单的Android计算器应用程序。
没有编译时错误,但是当我点击任何按钮时,应用程序崩溃了。
package com.buckydroid.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView screen;
private String str1,str2,str3,result,str,sign;
private Double a,b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (TextView)findViewById(R.id.textview);
}
private void onClick(View v){
Button button = (Button) v;
str += button.getText().toString();
screen.setText(str);
a = Double.parseDouble(str);
str = "";
}
private void onClickSigns(View v){
Button button = (Button) v;
sign = ((Button) v).getText().toString();
screen.setText(sign);
str="";
}
private void calculate(View v){
Button button = (Button) v;
str2 = screen.getText().toString();
b = Double.parseDouble(str2);
if (sign .equals("+")){
result = a+b+"";
}
else if (sign .equals("-")){
result = a-b+"";
}
else if (sign .equals("X")){
result = a*b+"";
}
else if (sign .equals("÷")){
result = a/b+"";
}
else{
result = "Something went wrong";
}
screen.setText(result);
}
}
错误记录
10-08 19:44:59.361 19449-19449/com.buckydroid.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.buckydroid.myapplication, PID: 19449
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
因此,如果您需要任何其他代码,请发表评论
提前谢谢..
答案 0 :(得分:0)
好的,我在Android工作室建立后得到了它。该错误比您粘贴的错误大得多,我能够通过堆栈跟踪跟踪它。
基本上,您尝试使用onClick
方法中的此行将字符串连接到null:str += button.getText().toString();
由于您无法做到这一点,您可以通过替换:
来解决问题str += button.getText().toString();
有了这个:
str = button.getText().toString();
OR
由于这是一个计算器应用,您可以使用str
在onCreate
方法中初始化str = "";
,然后从str = "";
方法中删除onClick
。
完整代码:
public class MainActivity extends AppCompatActivity {
private TextView screen;
private String str1,str2,str3,result,str,sign;
private Double a,b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (TextView)findViewById(R.id.textview);
str = "";
}
public void onClick(View v){
Button button = (Button) v;
str += button.getText().toString();
screen.setText(str);
a = Double.parseDouble(str);
}
public void onClickSigns(View v){
Button button = (Button) v;
sign = ((Button) v).getText().toString();
screen.setText(sign);
str="";
}
public void calculate(View v){
Button button = (Button) v;
str2 = screen.getText().toString();
b = Double.parseDouble(str2);
if (sign .equals("+")){
result = a+b+"";
}
else if (sign .equals("-")){
result = a-b+"";
}
else if (sign .equals("X")){
result = a*b+"";
}
else if (sign .equals("÷")){
result = a/b+"";
}
else{
result = "Something went wrong";
}
screen.setText(result);
}
}
答案 1 :(得分:0)
onClick应该是公共的而不是私有的。 也将字符串初始化为空字符串。像:
private String str1 = "",str2="",str3="",result="",str="",sign="";
希望这可以解决您的问题。
答案 2 :(得分:0)
方式1: 在你的main_activity xml文件中添加android:onclick =" onClick"并使onClick(View view)方法公开。
例如
http(s)://<username>.github.io
在main_activity.java文件中
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:onClick="onClick"/>
希望它能帮助你:)快乐的编码..