Ich versuche, einen Android-Rechner zu entwickeln, und obwohl Android Studio nicht zeigt, gibt es irgendwelche Fehler in meinem Code, wenn ich die App ausführen und versuchen, auf eine Schaltfläche klicken, funktioniert es nicht mehr. In diesem Fall klickte ich die 8-Taste (mit der ID Button8, das Verfahren onClick1 verknüpft, wenn darauf geklickt)Android App Probleme
Android Studio führt den folgenden Ausnahmecode, wenn ich die 8-Taste drücken:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.st1.u3141294.sparkscientificcalculator, PID: 2125
java.lang.IllegalStateException: Could not find method onClick1 (sparkMain)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button8'
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:5610)
at android.view.View$PerformClick.run(View.java:22260)
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)
Application terminated.
Hier ist mein Hauptklasse Code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.text.Html;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup.*;
import android.app.Activity;
import java.util.ArrayList;
public class sparkMain extends AppCompatActivity {
//TODO: Parentheses; first press enters first parenthesis, second press closes parentheses
//TODO: Long press Sin (gives cos, tan, sin-1, cos-1, tan-1) and log gives log-1
protected void onCreateView() {
((TextView)findViewById(R.id.buttonXPow)).setText(Html.fromHtml("X<sup><Small>y</small><s/up>"));
((TextView)findViewById(R.id.button10Pow)).setText(Html.fromHtml("10<sup><Small>y</small><s/up>"));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spark_activity_main);
onCreateView();
}
ArrayList<String> arrayList = new ArrayList<String>();
String stringInput = "";
String stringOutput = "";
public void onClick1 (View v) {
TextView textViewCalcHistExp1 = (TextView) findViewById(R.id.textViewCalcHistExp1);
Button button = (Button) v;
stringInput = (String) button.getText().toString();
if (!stringInput.contains("+") && !stringInput.contains("-") && !stringInput.contains("×") && !stringInput.contains("÷")) {
stringOutput = stringOutput+stringInput;
if (arrayList.size()>0) {
arrayList.remove((arrayList.size()-1));
}
arrayList.add(stringOutput);
}
else{
arrayList.add(stringInput);
arrayList.add(stringInput);
stringOutput="";
}
//This version truncates array formatting i.e. entering "2+4*6" would display "2+4*6"
textViewCalcHistExp1.setText(textViewCalcHistExp1.getText().toString()+stringInput);
//This version leaves array formatting i.e. entering "2+4*6" would display [2,+,4,*,6] ;good for debugging
//textViewCalcHistExp1.setText(arrayList.toString());
}
public void onClick (View v) {
TextView textViewCalcHistRes1 = (TextView)findViewById(R.id.textViewCalcHistRes1);
int calc = 0;
int c = arrayList.size();
//i.e. array [2,+,3,*,4,-,3] size(c) = 7, so [2,+,3,*,4,-,3]
while (c!=1) {
if (c>3) {
if (arrayList.get(3).contains("×") || arrayList.get(3).contains("÷")) {
if (arrayList.get(3).contains("×")){calc = Integer.parseInt(arrayList.get(2))*Integer.parseInt(arrayList.get(4));}
if (arrayList.get(3).contains("÷")){calc = Integer.parseInt(arrayList.get(2))/Integer.parseInt(arrayList.get(4));}
//calc = 12 ;array = [2,+,3,*,4,-,3]
arrayList.remove(2); //[2,+,*,4,-,3]
arrayList.remove(2); //[2,+,4,-,3]
arrayList.remove(2); //[2,+,-,3]
arrayList.add(2,Integer.toString(calc)); //[2,+,12,-,3]
c = arrayList.size(); // size(c) = 5
}
else {
//[2,+,12,-,3]
if (arrayList.get(3).contains("+")){calc = Integer.parseInt(arrayList.get(0))+Integer.parseInt(arrayList.get(2));}
if (arrayList.get(3).contains("-")){calc = Integer.parseInt(arrayList.get(0))-Integer.parseInt(arrayList.get(2));}
if (arrayList.get(3).contains("×")){calc = Integer.parseInt(arrayList.get(0))*Integer.parseInt(arrayList.get(2));}
if (arrayList.get(3).contains("÷")){calc = Integer.parseInt(arrayList.get(0))/Integer.parseInt(arrayList.get(2));}
//calc = 14
arrayList.remove(0); //[+,12,-,3]
arrayList.remove(0); //[12,-,3]
arrayList.remove(0); //[-,3]
arrayList.add(0,Integer.toString(calc)); //[14,-,3]
c = arrayList.size(); // size(c) = 3
}
}
// size(c) <= 3
else {
if (arrayList.get(3).contains("+")){calc = Integer.parseInt(arrayList.get(0))+Integer.parseInt(arrayList.get(2));}
if (arrayList.get(3).contains("-")){calc = Integer.parseInt(arrayList.get(0))-Integer.parseInt(arrayList.get(2));}
if (arrayList.get(3).contains("×")){calc = Integer.parseInt(arrayList.get(0))*Integer.parseInt(arrayList.get(2));}
if (arrayList.get(3).contains("÷")){calc = Integer.parseInt(arrayList.get(0))/Integer.parseInt(arrayList.get(2));}
//calc = 11
arrayList.remove(0); //[-,3]
arrayList.remove(0); //[3]
arrayList.remove(0); //[null]
arrayList.add(0,Integer.toString(calc)); // [9]
c = arrayList.size(); // size(c) = 1
}
}
textViewCalcHistRes1.setText(Integer.toString(calc));
arrayList.clear();
}
public String backspace (String str) {
if (stringOutput != null && stringOutput.length() > 0) {
stringOutput = stringOutput.substring(0, stringOutput.length()-1);
}
return str;
}
}
Und spark_activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app2="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/spark_activity_main"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.st1.u3141294.sparkscientificcalculator.sparkMain"
android:weightSum="2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<ScrollView
android:layout_width="match_parent"
android:layout_alignParentEnd="true"
android:scrollbarStyle="insideOverlay"
style="@android:style/Widget.DeviceDefault.Light.ScrollView"
android:clipToPadding="false"
android:fillViewport="false"
android:layout_height="260dp">
<LinearLayout
android:id="@+id/linearLayoutScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:id="@+id/textViewCalcHistExp1"
android:layout_height="55dp" />
<TextView
android:layout_width="match_parent"
android:id="@+id/textViewCalcHistRes1"
android:layout_height="55dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_height="40dp"
android:id="@+id/firstLinearHorizontal">
<Button
android:text="log"
android:layout_height="36dp"
android:layout_width="0dp"
android:id="@+id/buttonLog"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1" />
<Button
android:text="√"
android:layout_height="36dp"
android:layout_width="0dp"
android:id="@+id/buttonSqrt"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1" />
<Button
android:text="10^y"
android:layout_height="36dp"
android:layout_width="0dp"
android:id="@+id/button10Pow"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1" />
<Button
android:text="X^y"
android:layout_height="36dp"
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/buttonXPow"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp" />
<ImageButton
android:layout_width="0dp"
android:layout_height="36dp"
android:id="@+id/buttonDel"
android:layout_weight="1"
android:background="@drawable/operator_style"
app2:srcCompat="@mipmap/ic_input_delete_black_trans" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:text="sin"
android:layout_height="36dp"
android:id="@+id/buttonSin"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:text="π"
android:layout_height="36dp"
android:id="@+id/buttonPi"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:text="()"
android:layout_height="36dp"
android:id="@+id/buttonPar"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:text="+/-"
android:layout_height="36dp"
android:id="@+id/buttonSign"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:text="÷"
android:layout_height="36dp"
android:id="@+id/buttonDiv"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick1 (sparkMain)" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:text="7"
android:layout_height="36dp"
android:id="@+id/button7"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="8"
android:layout_height="36dp"
android:id="@+id/button8"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="9"
android:layout_height="36dp"
android:id="@+id/button9"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="×"
android:layout_height="36dp"
android:id="@+id/buttonMult"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_width="0dp"
android:layout_weight="0.735"
android:onClick="onClick1 (sparkMain)" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:text="4"
android:layout_height="36dp"
android:id="@+id/button4"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="5"
android:layout_height="36dp"
android:id="@+id/button5"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="6"
android:layout_height="36dp"
android:id="@+id/button6"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="-"
android:layout_height="36dp"
android:id="@+id/buttonSub"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_width="0dp"
android:layout_weight="0.735"
android:onClick="onClick1 (sparkMain)" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:text="1"
android:layout_height="36dp"
android:id="@+id/button1"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="2"
android:layout_height="36dp"
android:id="@+id/button2"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="3"
android:layout_height="36dp"
android:id="@+id/button3"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="+"
android:layout_height="36dp"
android:id="@+id/buttonAdd"
android:background="@drawable/operator_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/operator_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_width="0dp"
android:layout_weight="0.735"
android:onClick="onClick1 (sparkMain)" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="36dp">
<Button
android:text="0"
android:layout_height="36dp"
android:id="@+id/button0"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="onClick1 (sparkMain)" />
<Button
android:text="."
android:layout_height="36dp"
android:id="@+id/buttonDec"
android:background="@drawable/button_style"
android:fontFamily="sans-serif"
android:textColor="@drawable/button_style"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:layout_width="0dp" />
<Button
android:text="="
android:layout_height="36dp"
android:id="@+id/buttonEqual"
android:background="#f49542"
android:fontFamily="sans-serif-medium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textAllCaps="false"
android:layout_weight="1.78"
android:layout_width="0dp"
android:onClick="onClick (sparkMain)" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Irgendwelche Ideen, warum es bei so einer einfachen Aktion Ausnahmen gibt?
Bitte teilen Sie Ihre spark_activity_main xml –
Meine schlechte, bearbeitet das OP – RThomP
Darf ich fragen, wo Sie gelernt haben, dass 'onClick1 (sparkMain)' ist die richtige Syntax? –