gpt4 book ai didi

java - Android 应用程序在启动时崩溃

转载 作者:行者123 更新时间:2023-12-01 21:15:55 24 4
gpt4 key购买 nike

我正在尝试制作一个计算器应用程序。首先,我创建了一个类,它将接受一个字符串(我要求解的方程)并将其从 Infix 更改为 Postfix。该应用程序实际上是熊骨头,但当我启动它时,它立即崩溃,并且在控制台中我没有收到任何错误。有人知道问题可能出在哪里吗?

list 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.projectcalculator">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>

XML 文件 Activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.projectcalculator.MainActivity">

<TextView
android:id="@+id/textCalc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10sp"
android:layout_weight="1"
android:text="Hello"/>
</LinearLayout>

Java 文件MainActivity:

package com.example.android.projectcalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import static android.R.attr.onClick;

import com.example.android.projectcalculator.InfixToPostfix;

public class MainActivity extends AppCompatActivity {

public TextView calculationText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PrintMainScreen("hello you");
String s = InfixToPostfix.StartInfixToPostfix("A*(B+C)");
PrintMainScreen(s);
}

public void PrintMainScreen(String str)
{
TextView txview = (TextView)findViewById(R.id.textCalc);
txview.setText(str);
}
}

Java 文件 InfiToPostfix:

package com.example.android.projectcalculator;

import java.util.Stack;

public class InfixToPostfix{

//Varibili private
private static String postfixOutput;
private static Stack<Character> operatorStack;
private static String infixInput;

//Metodo per controlare se ho a che fare con l'operatore
private static boolean IsOperator (char c)
{
return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '^';
}

private static int OpratorPriority(Character operator1)
{
switch(operator1)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}

//Metodo Supremo
public static String StartInfixToPostfix(String in)
{
//inizializzo variabili
postfixOutput = "";
infixInput.equals(in);
int lunghezza = infixInput.length();
operatorStack = new Stack<Character>();

//inizio il processo
for (int i=0; i < infixInput.length(); i++)
{
//se non è un operatore ma un operando, lo aggiungo alla string di output
if (!IsOperator(infixInput.charAt(i)))
{
postfixOutput += infixInput.charAt(i);
}
//Considero il caso in cui sia l'operatore ')'
else if (infixInput.charAt(i) == ')')
{
//Inserisco nel postfix gli operatori fino a che lo sctack è vuoto o incontro una parentesi chiusa
while (!operatorStack.isEmpty() && operatorStack.peek() != ')')
{
postfixOutput += (operatorStack.pop());
}
//elimino la '(' se c'è
if (!operatorStack.isEmpty())
{
operatorStack.pop();
}
}
//considero il caso in cui ho un operatore che non sia ')'
else
{
//questo while si attiva solo se (1) lo stack non è vuoto (2) l'elemento in cima allo stack non è '(' (3) se l'ultimo operatore ha grado minore
while ( (!operatorStack.isEmpty()) && (operatorStack.peek() != '(') && (OpratorPriority(operatorStack.peek()) >= OpratorPriority(infixInput.charAt(i))))
{
postfixOutput += operatorStack.pop();
}
//aggiungo l'operatore a prescindere di ciò che ho fatto o non fatto con il cilo while
operatorStack.push(infixInput.charAt(i));
}
}

//Alla fine del metodo rilascio il postfix
return postfixOutput;
}
}

编辑:有谁知道如果 InfixToPostfix 类中的所有内容都不是静态的,为什么我会收到错误?

Edit2:现在给我这个错误...

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.projectcalculator, PID: 25981
Theme: themes:{}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.projectcalculator/com.example.android.projectcalculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.android.projectcalculator.InfixToPostfix.StartInfixToPostfix(InfixToPostfix.java:40)
at com.example.android.projectcalculator.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
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) 

最佳答案

第 40 行中的 infixInput 为 null,因为它尚未初始化

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

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