我构建了一个对给定数字进行平方的简单项目。在那个应用程序中,一旦我点击“计算”按钮就会说“不幸的是,SquareCalculator已停止”。我该怎么做才能解决这个问题?
以下是我的整个代码: -
MainActivity.java
package thenerdimite.squarecalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void calculate (View v){
EditText input = (EditText) findViewById(R.id.etInput);
TextView output = (TextView) findViewById(R.id.tvOutput);
int base = Integer.valueOf(input.getText().toString());
int result = base * base;
String formattedResult = String.format("%,d", result);
output.setText("Result: " + formattedResult);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="thenerdimite.squarecalculator.MainActivity">
<EditText
android:id="@+id/etInput"
android:layout_width="245dp"
android:layout_height="41dp"
android:layout_marginTop="15dp"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="62dp"
android:layout_below="@+id/tvInput"
android:layout_alignStart="@+id/tvInput" />
<Button
android:id="@+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/etInput"
android:layout_below="@+id/etInput"
android:layout_marginTop="20dp"
android:onClick="Calculate"
android:text="Calculate"
android:textSize="18sp" />
<TextView
android:id="@+id/tvOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result:"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:layout_below="@+id/btnCalculate"
android:layout_alignStart="@+id/btnCalculate"
android:layout_marginTop="18dp" />
<TextView
android:id="@+id/tvInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter an Integer:"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:layout_marginStart="19dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
logcat的
04-25 09:28:05.060 3596-3596/thenerdimite.squarecalculator D/AndroidRuntime: Shutting down VM
04-25 09:28:05.060 3596-3596/thenerdimite.squarecalculator W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa5007678)
04-25 09:28:05.060 3596-3596/thenerdimite.squarecalculator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find method Calculate(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnCalculate'
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:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
04-25 09:28:06.740 3596-3596/? I/Process: Sending signal. PID: 3596 SIG: 9
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="thenerdimite.squarecalculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
从我看到的,问题是你的按钮动作是android:onClick =&#34;计算&#34;但应该是android:onClick =&#34;计算&#34;
答案 1 :(得分:1)
将android:onClick="Calculate"
更改为android:onClick="calculate"
。
calculate
方法也应为public
。
答案 2 :(得分:1)
您认为该方法需要公开并接受View
为了使其正常工作,该方法必须是公共的,并接受View作为其唯一参数。
所以它应该是
public void Calculate (View v){
当然,使用xml表示法,您应该将其称为calculate
并更正XML
答案 3 :(得分:1)
您需要使xml无法访问方法public
,protected
和private
方法。
public void calculate (View v){
EditText input = (EditText) findViewById(R.id.etInput);
TextView output = (TextView) findViewById(R.id.tvOutput);
int base = Integer.valueOf(input.getText().toString());
int result = base * base;
String formattedResult = String.format("%,d", result);
output.setText("Result: " + formattedResult);
}
答案 4 :(得分:0)
在XML中你有&#34;计算&#34;资本&#34; C&#34;方法被称为&#34;计算&#34;。更改XML,它应该可以工作。
https://developer.android.com/reference/android/widget/Button.html