gpt4 book ai didi

java - Android程序崩溃

转载 作者:行者123 更新时间:2023-12-01 23:25:58 24 4
gpt4 key购买 nike

我的 Android 应用程序遇到一些困难。

这是我的代码:

package activities;

import java.util.Vector;

import coursework.prototype.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class game_calculator extends Activity {

double currentTotal = 0.0; //Total of current calculation
String currentText = "0"; //Text displayed in the TextView
TextView Calculator_Display;
Vector<String> currentCalculations = new Vector<String>(); //Set up a vector to hold calculations

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_calculator);

Calculator_Display = (TextView) findViewById(R.id.display); //Locate the textview
Calculator_Display.setText( currentText ); //Set the text to default value
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

// MY CODE STARTS HERE

//Go Back button on click event
//Sets the current activity to the main menu
public void GoToMenu(View v)
{
startActivity( new Intent(this, game_selection.class) );
}

//Adds the number to the current display
public void CalculatorButton(View v)
{
//Get the buttons text
String buttons_text = ((Button) v).getText().toString();

//If no number has been inputed (screen will show 0)
if(currentText == "0")
{
currentText = buttons_text;
}
else //Add the number to the current display (after previous entered numbers)
{
currentText = currentText + buttons_text;
}
Calculator_Display.setText(currentText); //Set the display to the inputed number
}

//Performs the calculation with the current total and the display number
public void CalculatorFunction(View button)
{
switch( button.getId() )
{
case R.id.calculator_equals: //display the current total

currentCalculations.add( currentText ); //Add current number into the vector

if(currentCalculations.size() > 0) //Only process calculations if there's elements to process
CalculateTotal();

currentText = "0"; //Reset display
break;
case R.id.calculator_reset:
currentText = "0"; //Reset stored display
currentTotal = 0; //Reset stored total
currentCalculations.clear();
break;
case R.id.calculator_backspace:
//Delete the last entered number
if (currentText.length() > 0 && currentText.charAt(currentText.length()-1)=='0') {
currentText = currentText.substring(0, currentText.length()-1);
}
break;
default:
//Convert the display to an integer to be worked with
currentCalculations.add( currentText ); //Add current number into the vector
currentCalculations.add( ((Button) button).getText().toString() ); //Add the type of calculation to perform

currentText = "0"; //Reset display number
break;
}

Calculator_Display.setText( currentText ); //Set the new display
}

public void CalculateTotal()
{
//Set up variables
String currentElement; //Temporary string storage
String currentSign = " "; //Temporary sign storage
double currentNumber = 0; //Temporary number storage
Log.i("INFORMATION", "<=================== MY LOGS START HERE ===================>" );
Log.i("INFORMATION", "Size: " + Integer.toString(currentCalculations.size()) );

//Start processing
for(int c = 0; c <= currentCalculations.size(); c++) //Loops through elements to process
{
currentElement = currentCalculations.get( c ); //Get element from array
Log.i("INFORMATION", "Element " + Integer.toString(c) + " : " + currentElement );

//Check if the element is a sign
if(currentElement == "+" || currentElement == "-" || currentElement == "*" || currentElement == "/")
{
Log.i("INFORMATION", "Sign found!");
currentSign = currentElement; //Save into temporary storage to be used next loop
}
else // Element is a number
{
currentNumber = Double.parseDouble( currentElement ); //Convert number to integer

if(currentSign == "+")
currentTotal += currentNumber;
else if(currentSign == "-")
currentTotal -= currentNumber;
else if(currentSign == "*")
currentTotal = currentTotal * currentNumber;
else if(currentSign == "/")
currentTotal = currentTotal / currentNumber;
else
Log.i("INFORMATION", "THE PROGRAMS FUCKED UP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}

}
Calculator_Display.setText( Double.toString( currentTotal ) ); //Display the currentTotal

Log.i("INFORMATION", "<=================== MY LOGS END HERE ===================>" );
}
}

当我点击等于按钮时,应用程序崩溃了,所以我知道错误出在CalculateTotal上,但不确定为什么会崩溃。如果我执行 7 + 8,Logcat 将显示:

11-13 19:10:43.073: I/INFORMATION(1408): <=================== MY LOGS START HERE ===================>
11-13 19:10:43.073: I/INFORMATION(1408): Size: 5
11-13 19:10:43.114: I/INFORMATION(1408): Element 0 : 7
11-13 19:10:43.114: I/INFORMATION(1408): Element 1 : +
11-13 19:10:43.244: W/dalvikvm(1408): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
11-13 19:10:43.476: E/AndroidRuntime(1408): FATAL EXCEPTION: main
11-13 19:10:43.476: E/AndroidRuntime(1408): java.lang.IllegalStateException: Could not execute method of the activity
11-13 19:10:43.476: E/AndroidRuntime(1408): at android.view.View$1.onClick(View.java:3633)
11-13 19:10:43.476: E/AndroidRuntime(1408): at android.view.View.performClick(View.java:4240)
11-13 19:10:43.476: E/AndroidRuntime(1408): at android.view.View$PerformClick.run(View.java:17721)
11-13 19:10:43.476: E/AndroidRuntime(1408): at android.os.Handler.handleCallback(Handler.java:730)
11-13 19:10:43.476: E/AndroidRuntime(1408): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 19:10:43.476: E/AndroidRuntime(1408): at android.os.Looper.loop(Looper.java:137)
11-13 19:10:43.476: E/AndroidRuntime(1408): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-13 19:10:43.476: E/AndroidRuntime(1408): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:10:43.476: E/AndroidRuntime(1408): at java.lang.reflect.Method.invoke(Method.java:525)
11-13 19:10:43.476: E/AndroidRuntime(1408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-13 19:10:43.476: E/AndroidRuntime(1408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 19:10:43.476: E/AndroidRuntime(1408): at dalvik.system.NativeStart.main(Native Method)
11-13 19:10:43.476: E/AndroidRuntime(1408): Caused by: java.lang.reflect.InvocationTargetException
11-13 19:10:43.476: E/AndroidRuntime(1408): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:10:43.476: E/AndroidRuntime(1408): at java.lang.reflect.Method.invoke(Method.java:525)
11-13 19:10:43.476: E/AndroidRuntime(1408): at android.view.View$1.onClick(View.java:3628)
11-13 19:10:43.476: E/AndroidRuntime(1408): ... 11 more
11-13 19:10:43.476: E/AndroidRuntime(1408): Caused by: java.lang.NumberFormatException: Invalid double: "+"
11-13 19:10:43.476: E/AndroidRuntime(1408): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-13 19:10:43.476: E/AndroidRuntime(1408): at java.lang.StringToReal.initialParse(StringToReal.java:151)
11-13 19:10:43.476: E/AndroidRuntime(1408): at java.lang.StringToReal.parseDouble(StringToReal.java:263)
11-13 19:10:43.476: E/AndroidRuntime(1408): at java.lang.Double.parseDouble(Double.java:295)
11-13 19:10:43.476: E/AndroidRuntime(1408): at activities.game_calculator.CalculateTotal(game_calculator.java:127)
11-13 19:10:43.476: E/AndroidRuntime(1408): at activities.game_calculator.CalculatorFunction(game_calculator.java:76)
11-13 19:10:43.476: E/AndroidRuntime(1408): ... 14 more
11-13 19:14:16.423: I/Process(1408): Sending signal. PID: 1408 SIG: 9
11-13 19:14:18.223: I/dalvikvm-heap(1446): Grow heap (frag case) to 4.082MB for 1536016-byte allocation
11-13 19:20:14.522: I/dalvikvm-heap(1545): Grow heap (frag case) to 4.082MB for 1536016-byte allocation
11-13 19:20:20.124: I/dalvikvm-heap(1545): Grow heap (frag case) to 5.822MB for 1536016-byte allocation
11-13 19:20:29.415: I/dalvikvm-heap(1545): Grow heap (frag case) to 7.856MB for 1536016-byte allocation
11-13 19:20:32.132: I/Choreographer(1545): Skipped 31 frames! The application may be doing too much work on its main thread.
11-13 19:20:43.032: I/INFORMATION(1545): <=================== MY LOGS START HERE ===================>
11-13 19:20:43.032: I/INFORMATION(1545): Size: 3
11-13 19:20:43.122: I/INFORMATION(1545): Element 0 : 7
11-13 19:20:43.162: I/INFORMATION(1545): THE PROGRAMS FUCKED UP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
11-13 19:20:43.172: I/INFORMATION(1545): Element 1 : +
11-13 19:20:43.224: W/dalvikvm(1545): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
11-13 19:20:43.292: E/AndroidRuntime(1545): FATAL EXCEPTION: main
11-13 19:20:43.292: E/AndroidRuntime(1545): java.lang.IllegalStateException: Could not execute method of the activity
11-13 19:20:43.292: E/AndroidRuntime(1545): at android.view.View$1.onClick(View.java:3633)
11-13 19:20:43.292: E/AndroidRuntime(1545): at android.view.View.performClick(View.java:4240)
11-13 19:20:43.292: E/AndroidRuntime(1545): at android.view.View$PerformClick.run(View.java:17721)
11-13 19:20:43.292: E/AndroidRuntime(1545): at android.os.Handler.handleCallback(Handler.java:730)
11-13 19:20:43.292: E/AndroidRuntime(1545): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 19:20:43.292: E/AndroidRuntime(1545): at android.os.Looper.loop(Looper.java:137)
11-13 19:20:43.292: E/AndroidRuntime(1545): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-13 19:20:43.292: E/AndroidRuntime(1545): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:20:43.292: E/AndroidRuntime(1545): at java.lang.reflect.Method.invoke(Method.java:525)
11-13 19:20:43.292: E/AndroidRuntime(1545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-13 19:20:43.292: E/AndroidRuntime(1545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 19:20:43.292: E/AndroidRuntime(1545): at dalvik.system.NativeStart.main(Native Method)
11-13 19:20:43.292: E/AndroidRuntime(1545): Caused by: java.lang.reflect.InvocationTargetException
11-13 19:20:43.292: E/AndroidRuntime(1545): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:20:43.292: E/AndroidRuntime(1545): at java.lang.reflect.Method.invoke(Method.java:525)
11-13 19:20:43.292: E/AndroidRuntime(1545): at android.view.View$1.onClick(View.java:3628)
11-13 19:20:43.292: E/AndroidRuntime(1545): ... 11 more
11-13 19:20:43.292: E/AndroidRuntime(1545): Caused by: java.lang.NumberFormatException: Invalid double: "+"
11-13 19:20:43.292: E/AndroidRuntime(1545): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-13 19:20:43.292: E/AndroidRuntime(1545): at java.lang.StringToReal.initialParse(StringToReal.java:151)
11-13 19:20:43.292: E/AndroidRuntime(1545): at java.lang.StringToReal.parseDouble(StringToReal.java:263)
11-13 19:20:43.292: E/AndroidRuntime(1545): at java.lang.Double.parseDouble(Double.java:295)
11-13 19:20:43.292: E/AndroidRuntime(1545): at activities.game_calculator.CalculateTotal(game_calculator.java:126)
11-13 19:20:43.292: E/AndroidRuntime(1545): at activities.game_calculator.CalculatorFunction(game_calculator.java:76)
11-13 19:20:43.292: E/AndroidRuntime(1545): ... 14 more
11-13 19:25:43.513: I/Process(1545): Sending signal. PID: 1545 SIG: 9
11-13 19:25:44.963: I/dalvikvm-heap(1577): Grow heap (frag case) to 4.082MB for 1536016-byte allocation

最佳答案

永远不要使用==来检查Java中字符串的相等性。您必须始终使用

string1.equals(string2)

线

if (currentString == "+") 

将始终返回false,因此您必须使用

if (currentString.equals("+")) 

关于java - Android程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com