计算机项目应用程序在手机启动时停止

时间:2017-02-09 15:01:20

标签: android

这是我的代码:我的计算器项目的清单文件: 当我尝试在运行android 4.4.2 kitkat版本的手机上启动应用程序时,它表示计算器已停止强行关闭。

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

<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代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight= "30">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_gravity="right"
        android:gravity="right"
        android:padding="10dp"
        android:text=""/>

    <TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="right"
        android:gravity="right"
        android:padding="10dp"
        android:text=""
        android:layout_marginBottom="10dp" />
    <Button
        android:id="@+id/clear"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="260dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Clear"
        android:layout_marginLeft="60dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/one"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="1" />

        <Button
            android:id="@+id/two"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="2"/>

        <Button
            android:id="@+id/three"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="3"/>

        <Button
            android:id="@+id/add"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="+"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

        <Button
            android:id="@+id/four"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="4"/>

        <Button
            android:id="@+id/five"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="5"/>

        <Button
            android:id="@+id/six"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="6"/>

        <Button
            android:id="@+id/minus"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="-"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

        <Button
            android:id="@+id/seven"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="7"/>

        <Button
            android:id="@+id/eight"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="8"/>

        <Button
            android:id="@+id/nine"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="9"/>

        <Button
            android:id="@+id/divide"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="/"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

        <Button
            android:id="@+id/ce"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="CE"/>

        <Button
            android:id="@+id/zero"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="0"/>

        <Button
            android:id="@+id/equal"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="="/>

        <Button
            android:id="@+id/mul"
            android:layout_width="65dp"
            android:layout_height="55dp"
            android:text="*"/>

    </LinearLayout>
    </LinearLayout>
  </ScrollView>
  </LinearLayout>

以下是Java中的MainActivity代码:

package com.example.jeet.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

Button b1, b2, b3, b4, b5, b6, b7, b8, b9, bzero, bAdd, bSub, bMul, bDiv, bClear, bEqual,bCe;
TextView txt1, txt2;
String s = "", s1 = "" , s2 = "" , resultString = "";
int i =0 , i1 = 0, c = -1;
int result = 0;

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

    b1 = (Button)findViewById(R.id.one);
    b2 = (Button) findViewById(R.id.two);
    b3 = (Button)findViewById(R.id.three);
    b4 = (Button)findViewById(R.id.four);
    b5 = (Button)findViewById(R.id.five);
    b6 = (Button)findViewById(R.id.six);
    b7 = (Button)findViewById(R.id.seven);
    b8 = (Button)findViewById(R.id.eight);
    b9 = (Button)findViewById(R.id.nine);
    bzero = (Button)findViewById(R.id.zero);
    bAdd = (Button)findViewById(R.id.add);
    bSub = (Button)findViewById(R.id.minus);
    bMul = (Button)findViewById(R.id.mul);
    bDiv = (Button)findViewById(R.id.divide);
    bClear = (Button)findViewById(R.id.clear);
    bCe = (Button)findViewById(R.id.ce);

    txt1 = (TextView)findViewById(R.id.textview1);
    txt2 = (TextView)findViewById(R.id.textview2);

    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            s = (String) txt1.getText();
            if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
                txt1.setText("");
                s = "";
            }

            txt1.setText(s + "1");
            s = "";
        }
    });

    b2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            s = (String) txt1.getText();
            if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {

                txt1.setText("");
                s = "";
            }
            txt1.setText(s + "2");
            s = "";
        }
    });

    b3.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            s = (String)txt1.getText();
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {

                txt1.setText("");
                s = "";
            }
              txt1.setText(s + "3");
              s = "";
        }

    });

    b4.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){

            s = (String)txt1.getText();
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
                txt1.setText("");
                s = "";
            }
             txt1.setText(s + "4");
                s = "";
        }
    });

    b5.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){
            s = (String)txt1.getText();
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
                txt1.setText("");
                s = "";
            }

            txt1.setText(s + "5");
            s = "";
        }
    });

    b6.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){
            s = (String)txt1.getText();
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
            txt1.setText("");
                s = "";
            }

            txt1.setText(s + "6");
            s = "";
        }
    });

    b7.setOnClickListener(new View.OnClickListener(){

        public void  onClick(View v){

            s = (String)txt1.getText();
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){

                txt1.setText("");
                s = "";
            }

            txt1.setText(s + "7");
            s = "";
        }
    });

    b8.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            s = (String)txt1.getText();
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
                txt1.setText("");
                s = "";
            }
            txt1.setText(s + "8");
            s = "";
        }
    });

    b9.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            s = (String)txt1.getText();
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
                txt1.setText("");
                s = "";
            }

            txt1.setText(s + "9");
            s = "";
        }
    });

    bzero.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            s = (String)txt1.getText();
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){

                txt1.setText("");
                s = "";
            }

            txt1.setText(s + "0");
            s = "";
        }
    });

    bCe.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            txt1.setText("");
            txt2.setText("");
            i = 0;
            i1 = 0;
            s1 = "";
            s2 = "";
            resultString = "";
            c = -1;
            result = 0;
        }
    });

    bClear.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            s = (String)txt1.getText();
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("")){

                i = 0;
            }
            else{

                i = Integer.parseInt(s);
                i = i/10;
            }

            if(i == 0){
                txt1.setText("");
            }
            else{
                txt1.setText(i + "");
            }
            s = null;
        }
    });

    bAdd.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            String tmp = (String)txt1.getText();
            if(tmp.isEmpty()){
                s1 = "0";
            }
            else if(!tmp.equals("+") && !tmp.equals("-") && !tmp.equals("*") && !tmp.equals("/")){

                s1 = tmp;
            }

            c = 0;
            resultString = "";
            txt1.setText("+");
            txt2.setText(s1 + " + ");
        }
    });

    bSub.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            String tmp = (String)txt1.getText();
            if(tmp.isEmpty()){

                s1 = "0";
            }
            else if(!tmp.equals("+") && !tmp.equals("-") && !tmp.equals("*") && !tmp.equals("/")){

                s1 = tmp;
            }
             c= 1;
            resultString = "";
            txt1.setText("-");
            txt2.setText(s1 + " - ");
        }
    });

    bMul.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            String tmp = (String)txt1.getText();
            if(tmp.isEmpty()){

                s1 = "0";
            }
            else if(!tmp.equals("+") && !tmp.equals("-") && !tmp.equals("*") && !tmp.equals("/")){

                s1 = tmp;
            }
            c= 3;
            resultString = "";
            txt1.setText("*");
            txt2.setText(s1 + " * ");
        }
    });

    bDiv.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            String tmp = (String)txt1.getText();
            if(tmp.isEmpty()){

                s1 = "0";
            }
            else if(!tmp.equals("+") && !tmp.equals("-") && !tmp.equals("*") && !tmp.equals("/")){

                s1 = tmp;

            }
            c= 2;
            resultString = "";
            txt1.setText("/");
            txt2.setText(s1 + " / ");
        }
    });

    bEqual.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String operator = "";
            if (s1.equalsIgnoreCase("+") || s1.equalsIgnoreCase("-") || s1.equalsIgnoreCase("/") || s1.equalsIgnoreCase("*")) {
                i = 0;
            } else if (s1 == null || s1.isEmpty()) {
                i = 0;
            } else {
                i = Integer.parseInt(s1);
            }

            if(resultString.isEmpty()) {
                s2 = (String) txt1.getText();
                if (s2.equalsIgnoreCase("+") || s2.equalsIgnoreCase("-") || s2.equalsIgnoreCase("/") || s2.equalsIgnoreCase("*")) {
                    i1 = 0;
                } else if (s2 == null || s2.isEmpty()) {
                    i1 = 0;
                } else {
                    i1 = Integer.parseInt(s2);
                }
            } else {
                i = result;
            }

            if (c == 0) {
                operator = "+";
                result = i + i1;
            } else if (c == 1) {
                operator = "-";
                result = i - i1;
            } else if (c == 2) {
                operator = "/";
                if (i1 == 0) {
                    Toast.makeText(getApplicationContext(),
                            "Invalid Input", Toast.LENGTH_LONG).show();
                    result = 0;
                } else {
                    result = i / i1;
                }
            } else if (c == 3) {
                operator = "*";
                result = i * i1;
            } else {
                operator = "";
                result = 0;
            }

            //History Storage
            if(!operator.isEmpty()) {
                txt2.setText(i + " " + operator + " " + i1);
            } else {
                txt2.setText("");
            }

            resultString = String.valueOf(result);
            txt1.setText(resultString);
        }

    });
}
}

Logcat错误如下:

**02-09 23:39:17.070 766-766/com.example.jeet.calculator E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.jeet.calculator, PID: 766
                                                                         java.lang.RuntimeException: Unable to start activity** ComponentInfo{com.example.jeet.calculator/com.example.jeet.calculator.MainActivity}: java.lang.NullPointerException
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                             at android.os.Looper.loop(Looper.java:193)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: java.lang.NullPointerException
                                                                             at com.example.jeet.calculator.MainActivity.onCreate(MainActivity.java:347)
                                                                             at android.app.Activity.performCreate(Activity.java:5264)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                             at android.os.Looper.loop(Looper.java:193) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5299) 
                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
                                                                             at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

我不确定,但我觉得你忘了在bEqual上做FindViewById