Android应用在发布时崩溃

时间:2016-10-18 17:22:00

标签: java android

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

清单文件:

<?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) 

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

你应该初始化你的infixInput变量。

tz_adjust = np.timedelta64(int(-int(str(np.datetime64(datetime.datetime.now()))[-5:])/100),'h')  
test['dt']=np.datetime64(dt) + tz_adjust
test
Out[75]: 
   A  B                  dt
0  a  1 2016-10-04 04:00:00
1  b  2 2016-10-04 04:00:00
2  c  3 2016-10-04 04:00:00
test.dt.unique()
Out[76]: array(['2016-10-04T00:00:00.000000000-0400'], dtype='datetime64[ns]')

此外,您似乎想要使用private static String infixInput = ""; 值初始化infixInput。 执行in只检查两个变量是否保持相同的值并返回一个布尔值。

然后你应该这样做:

infixInput.equals(in)

答案 2 :(得分:1)

infixInput.equals(in);为NULL。您必须先初始化infixInput

String.equals(String)检查两个字符串的内容是否相等。如果其中一个字符串为空,则不起作用。