Android应用程序在发布后立即停止

时间:2013-07-08 11:43:49

标签: java android crash

我正在构建一个简单的Android计算器应用程序。事情进展顺利。后来我认为我的代码非常混乱,并决定重新安排和清理代码。我创建了一个新项目并将重新排列的代码复制到其中,但应用程序在启动后立即停止。这是代码:

package com.example.calculator_v001;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;


public class Calculator_v001_Activity extends Activity
{
    boolean afterAns = false;
    private EditText edtShow;
    private Button btn0, btn1, btn2, btn3, btn4 ,btn5, btn6, btn7, btn8, btn9, btnPlus, btnMinus, btnTimes, btnDivide, btnEquals, btnDot, btnClear, btnBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator_v001);
        edtShow = (EditText)findViewById(R.id.edtShow);
        btn0 = (Button)findViewById(R.id.btn0);
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);
        btn4 = (Button)findViewById(R.id.btn4);
        btn5 = (Button)findViewById(R.id.btn5);
        btn6 = (Button)findViewById(R.id.btn6);
        btn7 = (Button)findViewById(R.id.btn7);
        btn8 = (Button)findViewById(R.id.btn8);
        btn9 = (Button)findViewById(R.id.btn9);
        btnPlus = (Button)findViewById(R.id.btnPlus);
        btnMinus = (Button)findViewById(R.id.btnMinus);
        btnTimes = (Button)findViewById(R.id.btnTimes);
        btnDivide = (Button)findViewById(R.id.btnDivide);
        btnEquals = (Button)findViewById(R.id.btnEquals);
        btnDot = (Button)findViewById(R.id.btnDot);
        btnClear = (Button)findViewById(R.id.btnClear);
        btnBack = (Button)findViewById(R.id.btnBack);

    btn0.setOnClickListener(numListener);
    btn1.setOnClickListener(numListener);
    btn2.setOnClickListener(numListener);
    btn3.setOnClickListener(numListener);
    btn4.setOnClickListener(numListener);
    btn5.setOnClickListener(numListener);
    btn6.setOnClickListener(numListener);
    btn7.setOnClickListener(numListener);
    btn8.setOnClickListener(numListener);
    btn9.setOnClickListener(numListener);

    btnPlus.setOnClickListener(operListener);
    btnMinus.setOnClickListener(operListener);
    btnTimes.setOnClickListener(operListener);
    btnDivide.setOnClickListener(operListener);

    btnEquals.setOnClickListener(equalsListener);
    btnDot.setOnClickListener(dotListener);
    btnClear.setOnClickListener(clearListener);
    btnBack.setOnClickListener(backListener);
}
private String addcomma(String str)
{
    boolean dotdeleted = false;
    if(str.length() != 0)
    {
        if(str.substring(str.length() - 1).equals("."))
        {
            str = str.substring(0, str.length() - 1);
            dotdeleted = true;
        }
    }
    List<String> numlistcomma = new ArrayList<String>();
    List<String> tempsignlist = new ArrayList<String>();
    String num = "";
    for(int i = 0; i < str.length(); i++)
    {
        if(! checkPM(str.substring(i, i + 1)) && ! checkTD(str.substring(i, i + 1)))
        {
            num = num + str.substring(i, i + 1);
        } else
        {
            numlistcomma.add(num);
            tempsignlist.add(str.substring(i, i + 1));
            num = "";
        }
    }
    if(! num.equals(""))
    {
        numlistcomma.add(num);
    }
    for(int j = 0; j < numlistcomma.size(); j++)
    {
        String tempstr = numlistcomma.get(j);
        if(! checkDot(numlistcomma.get(j)))
        {
            for(int k = numlistcomma.get(j).length() - 3; k > 0; k = k - 3)
            {
                tempstr = tempstr.substring(0, k) + "," + tempstr.substring(k);
            }
        } else
        {
            for(int k = numlistcomma.get(j).indexOf(".") - 3; k > 0; k = k - 3)
            {
                tempstr = tempstr.substring(0, k) + "," + tempstr.substring(k);
            }
        }
        if(tempstr.length() != 0)
        {
            numlistcomma.set(j, tempstr);
        }
    }
    str = "";
    for(int l = 0; l < numlistcomma.size(); l++)
    {
        if(l < tempsignlist.size())
        {
            str = str + numlistcomma.get(l) + tempsignlist.get(l);
        } else
        {
            str = str + numlistcomma.get(l);
        }
    }
    if(dotdeleted)
    {
        str = str + ".";
    }
    return str;
};
private String calculate(String str)
{
    String num = "";
    String numlistele = "";
    String tdAns = "";
    List<String> list = new ArrayList<String>();
    List<String> numlist = new ArrayList<String>();
    List<String> TDlist = new ArrayList<String>();
    for(int i = 0; i < str.length(); i++)
    {
        if(! checkPM(str.substring(i, i + 1)))
        {
            num = num + str.substring(i, i + 1);
        } else
        {
            list.add(num);
            list.add(str.substring(i, i + 1));
            num = "";
        }
    }
    list.add(num);
    if(checkPM(list.get(list.size() - 1)))
    {
        list.remove(list.get(list.size() - 1));
    }
    for(int j = 0; j < list.size(); j++)
    {
        if(checkTD(list.get(j)))
        {
            for(int k = 0; k < list.get(j).length(); k++)
            {
                if(! checkTD(list.get(j).substring(k, k + 1)))
                {
                    numlistele = numlistele + list.get(j).substring(k, k + 1);
                } else
                {
                    numlist.add(numlistele);
                    TDlist.add(list.get(j).substring(k, k + 1));
                    numlistele = "";
                }
            }
            numlist.add(numlistele);
            numlistele = "";
            tdAns = numlist.get(0);
            for(int l = 0; l < TDlist.size(); l++)
            {
                if(TDlist.get(l).equals("×"))
                {
                    double tempdou = Double.parseDouble(tdAns) * Double.parseDouble(numlist.get(l + 1));
                    tdAns = String.valueOf(tempdou);
                } else
                {
                    double tempdou = Double.parseDouble(tdAns) / Double.parseDouble(numlist.get(l = 1));
                    tdAns = String.valueOf(tempdou);
                }
            }
            list.set(j, tdAns);
        }
        numlist.clear();
        TDlist.clear();
    }
    String ans = list.get(0);
    for(int m = 0; m < list.size(); m++)
    {
        if(list.get(m).equals("+"))
        {
            double tempdou = Double.parseDouble(ans) + Double.parseDouble(list.get(m + 1));
            ans = String.valueOf(tempdou);
        } else if(list.get(m).equals("-"))
        {
            double tempdou = Double.parseDouble(ans) - Double.parseDouble(list.get(m = 1));
            ans = String.valueOf(tempdou);
        }
    }
    if(ans.length() > 2)
    {
        if(ans.substring(ans.length() - 2).equals(".0"))
        {
            ans = ans.substring(0, ans.length() - 2);
        }
    }
    return ans;
};
private boolean checkDot(String str)
{
    int count = 0;
    if(checkPM(str) || checkTD(str))
    {
        for(int i = str.length(); ! checkPM(str.substring(i - 1, i)) && ! checkTD(str.substring(i - 1, i)); i--)
        {
            if(str.substring(i - 1, i).equals("."))
            {
                count++;
            }
        }
    } else
    {
        for(int i = str.length(); i != 0; i--)
        {
            if(str.substring(i - 1, i).equals("."))
            {
                count++;
            }
        }
    }
    if(count != 0)
    {
        return true;
    } else
    {
        return false;
    }
};
private boolean checkPM(String str)
{
    int count = 0;
    for(int i = 0; i < str.length(); i++)
    {
        if(str.substring(i, i + 1).equals("+") || str.substring(i, i + 1).equals("-"))
        {
            count++;
        }
    }
    if(count != 0)
    {
        return true;
    } else
    {
        return false;
    }
};
private boolean checkTD(String str)
{
    int count = 0;
    for(int i = 0; i < str.length(); i++)
    {
        if(str.substring(i, i + 1).equals("×") || str.substring(i, i + 1).equals("÷"))
        {
            count++;
        }
    }
    if(count != 0)
    {
        return true;
    } else
    {
        return false;
    }
};
private void pressNum(String str, String n)
{
    String tempstr = "";
    if(str.length() == 0 || str.equals("0") || afterAns)
    {
        tempstr = n;
    } else if(str.length() == 1)
    {
        tempstr = str + n;
    } else
    {
        if(str.substring(str.length() - 1).equals("0") && (checkPM(str.substring(str.length() - 2, str.length() - 1)) || checkTD(str.substring(str.length() - 2, str.length() - 1))))
        {
            tempstr = str.substring(0, str.length() - 1) + n;
        } else
        {
            tempstr = str + n;
        }
    }
    edtShow.setText(addcomma(tempstr));
    edtShow.setSelection(edtShow.getText().length());
    afterAns = false;
};
private void pressOper(String str, String n)
{
    String tempstr = "";
    if(str.length() != 0)
    {
        if(! checkPM(str.substring(s.length() - 1)) && ! checkTD(str.substring(str.length() - 1)) && ! str.substring(s.length() - 1).equals("."))
        {
            tempstr = str + n;
        } else
        {
            tempstr = str.substring(0, s.length() - 1) + n;
        }
    }
    edtShow.setText(addcomma(tempstr));
    edtShow.setSelection(edtShow.getText().length());
    afterAns = false;
};
private String removecomma(String str)
{
    for(int i = 0; i < str.length(); i++)
    {
        if(str.substring(i, i + 1).equals(","))
        {
            str = str.substring(0, i) + str.substring(i + 1);
        }
    }
    return str;
};

String s = removecomma(edtShow.getText().toString());
private Button.OnClickListener numListener = new Button.OnClickListener()
{
    public void onClick (View v)
    {
        switch(v.getId())
        {
            case R.id.btn0:
            {
                pressNum(s, "0");
                break;
            }
            case R.id.btn1:
            {
                pressNum(s, "1");
                break;
            }
            case R.id.btn2:
            {
                pressNum(s, "2");
                break;
            }
            case R.id.btn3:
            {
                pressNum(s, "3");
                break;
            }
            case R.id.btn4:
            {
                pressNum(s, "4");
                break;
            }
            case R.id.btn5:
            {
                pressNum(s, "5");
                break;
            }
            case R.id.btn6:
            {
                pressNum(s, "6");
                break;
            }
            case R.id.btn7:
            {
                pressNum(s, "7");
                break;
            }
            case R.id.btn8:
            {
                pressNum(s, "8");
                break;
            }
            case R.id.btn9:
            {
                pressNum(s, "9");
                break;
            }
        }
    }
};
private Button.OnClickListener operListener = new Button.OnClickListener()
{
    public void onClick (View v)
    {
        switch (v.getId())
        {
            case R.id.btnPlus:
            {
                pressOper(s, "+");
                break;
            }
            case R.id.btnMinus:
            {
                pressOper(s, "-");
                break;
            }
            case R.id.btnTimes:
            {
                pressOper(s, "×");
                break;
            }
            case R.id.btnDivide:
            {
                pressOper(s, "÷");
                break;
            }
        }
    }
};
private Button.OnClickListener equalsListener = new Button.OnClickListener()
{
    public void onClick (View v)
    {
        if(s.length() != 0)
        {
            if(! s.substring(s.length() - 1).equals(".") && (! checkPM(s.substring(s.length() - 1)) || ! checkTD(s.substring(s.length() - 1))))
            {
                edtShow.setText(addcomma(calculate(s)));
                afterAns = true;
            }
        }
    }
};
private Button.OnClickListener dotListener = new Button.OnClickListener()
{
    public void onClick (View v)
    {
        String tempstr = "";
        if(s.length() != 0 && ! checkDot(s) && ! checkPM(s.substring(s.length() - 1))&& ! checkTD(s.substring(s.length() - 1)))
        {
            tempstr = s + ".";
        } else
        {
            tempstr = s;
        }
        edtShow.setText(addcomma(tempstr));
    }
};
private Button.OnClickListener clearListener = new Button.OnClickListener()
{
    public void onClick (View v)
    {
        edtShow.setText("");
    }
};
private Button.OnClickListener backListener = new Button.OnClickListener()
{
    public void onClick (View v)
    {
        if(s.length() == 0 || s.equals("NaN") || s.equals("Infinity"))
        {
            edtShow.setText("");
        } else
        {
            edtShow.setText(addcomma(removecomma(s).substring(0, removecomma(s).length() - 1)));
            edtShow.setSelection(edtShow.getText().length());
        }
    }
};
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.calculator_v001_, menu);
    return true;
}
}

堆栈跟踪: enter image description here

07-08 12:00:40.917: E/Trace(913): error opening trace file: No such file or directory (2)
07-08 12:00:41.077: D/AndroidRuntime(913): Shutting down VM
07-08 12:00:41.097: W/dalvikvm(913): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-08 12:00:41.127: E/AndroidRuntime(913): FATAL EXCEPTION: main
07-08 12:00:41.127: E/AndroidRuntime(913): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hychentsa.calculator_v001/com.hychentsa.calculator_v001.Calculator_v001_Activity}: java.lang.NullPointerException
07-08 12:00:41.127: E/AndroidRuntime(913):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-08 12:00:41.127: E/AndroidRuntime(913):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-08 12:00:41.127: E/AndroidRuntime(913):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-08 12:00:41.127: E/AndroidRuntime(913):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-08 12:00:41.127: E/AndroidRuntime(913):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 12:00:41.127: E/AndroidRuntime(913):  at android.os.Looper.loop(Looper.java:137)
07-08 12:00:41.127: E/AndroidRuntime(913):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-08 12:00:41.127: E/AndroidRuntime(913):  at java.lang.reflect.Method.invokeNative(Native Method)
07-08 12:00:41.127: E/AndroidRuntime(913):  at java.lang.reflect.Method.invoke(Method.java:511)
07-08 12:00:41.127: E/AndroidRuntime(913):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-08 12:00:41.127: E/AndroidRuntime(913):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-08 12:00:41.127: E/AndroidRuntime(913):  at dalvik.system.NativeStart.main(Native Method)
07-08 12:00:41.127: E/AndroidRuntime(913): Caused by: java.lang.NullPointerException
07-08 12:00:41.127: E/AndroidRuntime(913):  at com.hychentsa.calculator_v001.Calculator_v001_Activity.<init>(Calculator_v001_Activity.java:332)
07-08 12:00:41.127: E/AndroidRuntime(913):  at java.lang.Class.newInstanceImpl(Native Method)
07-08 12:00:41.127: E/AndroidRuntime(913):  at java.lang.Class.newInstance(Class.java:1319)
07-08 12:00:41.127: E/AndroidRuntime(913):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-08 12:00:41.127: E/AndroidRuntime(913):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-08 12:00:41.127: E/AndroidRuntime(913):  ... 11 more

2 个答案:

答案 0 :(得分:3)

请检查代码的第332行。

String s = removecomma(edtShow.getText().toString());

这不应该在方法之外。它将被视为类变量并在创建活动时进行实例化。此时edtShow.getText()是零。

答案 1 :(得分:1)

我在OnCreate()的每一行都设置一个断点来检查崩溃的位置。 (似乎是一个NullPointerException)