Android计算器app:遗憾的是应用程序停止了

时间:2013-05-26 19:37:45

标签: android error-handling

我是adnroid的新手并且正在开发计算器应用程序。

我的应用程序没有错误但它不起作用。当我运行应用程序时,它说HesapMakinası已停止。

这是我的代码。变量是土耳其语。

 package com.example.hesapmakinesi;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    double sayi1=0;
    double sayi2=0;
    String islem;
    double sonuc=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button no0 = (Button)findViewById(R.id.button1);
        Button no1 = (Button)findViewById(R.id.button2);
        Button no2 = (Button)findViewById(R.id.button3);
        Button no3 = (Button)findViewById(R.id.button4);
        Button no4 = (Button)findViewById(R.id.button5);
        Button no5 = (Button)findViewById(R.id.button6);
        Button no6 = (Button)findViewById(R.id.button7);
        Button no7 = (Button)findViewById(R.id.button8);
        Button no8 = (Button)findViewById(R.id.button9);
        Button no9 = (Button)findViewById(R.id.button10);
        Button arti = (Button)findViewById(R.id.button11);
        Button eksi = (Button)findViewById(R.id.button12);
        Button carpi = (Button)findViewById(R.id.button14);
        Button bolu = (Button)findViewById(R.id.button13);
        Button esittir = (Button)findViewById(R.id.button15);
        Button sifirla = (Button)findViewById(R.id.button16);
        Button teksil =  (Button)findViewById(R.id.button17);



        final EditText islemekran = (EditText)findViewById(R.id.islemekran);

        // Her Button ve Edittext için bir nesne tanımlandı ve 
        // findViewById fonksiyonuyla nesnelere bileşenler atandı.     
        //0-9 arası sayılar için tıklama olayları ve gerekli kodlar.

        no0.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                islemekran.setText(islemekran.getText()+"0");               
            }
        });

        no1.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View v) {

                islemekran.setText(islemekran.getText()+"1");
            }
        });

        no2.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {

            islemekran.setText(islemekran.getText()+"2");   
        }
        });

        no3.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View v) {

            islemekran.setText(islemekran.getText()+"3");
        }
        });

        no4.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {

            islemekran.setText(islemekran.getText()+"4");
        }
        });

        no5.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {

            islemekran.setText(islemekran.getText()+"5");
        }
        });

        no6.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View v) {

            islemekran.setText(islemekran.getText()+"6");
        }
        });

        no7.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {

            islemekran.setText(islemekran.getText()+"7");
        }
        });

        no8.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {

            islemekran.setText(islemekran.getText()+"8");
        }
        });

        no9.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View v) {

            islemekran.setText(islemekran.getText()+"9");
        }
        });
        // İşlemler için tıklama olayları ve gerekli kodlar

        arti.setOnClickListener(new View.OnClickListener() {        
        @Override
        public void onClick(View v) {

            try {           
                sayi1=Double.parseDouble(islemekran.getText().toString());
                islemekran.setText(""); 
                islem = "+";
            } catch (Exception e) {

            }       
        }
        });

        eksi.setOnClickListener(new View.OnClickListener() {    
        @Override
        public void onClick(View v) {

            if(!(islemekran.getText().toString().equals("")))
            {
                sayi1=Double.parseDouble(islemekran.getText().toString());
                islemekran.setText(""); 
                islem = "-";
            }       
        }
        });

        carpi.setOnClickListener(new View.OnClickListener() {       
        @Override
        public void onClick(View v) {

            if(!(islemekran.getText().toString().equals("")))
            {
                sayi1=Double.parseDouble(islemekran.getText().toString());
                islemekran.setText(""); 
                islem = "x";
            }       
        }
        });

        bolu.setOnClickListener(new View.OnClickListener() {        
        @Override
        public void onClick(View v) {

            if(!(islemekran.getText().toString().equals("")))
            {
                sayi1=Double.parseDouble(islemekran.getText().toString());
                islemekran.setText(""); 
                islem = "/";
            }
        }
        });

        esittir.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {

            try {
                sayi2=Double.parseDouble((islemekran.getText().toString()));

                if(islem.equals("+"))
                {               
                    sonuc = sayi1+sayi2;
                    sayi1=0;
                    sayi2=0;
                }else if(islem.equals("-"))
                {
                    sonuc = sayi1-sayi2;
                    sayi1=0;
                    sayi2=0;
                }else if(islem.equals("x"))
                {
                    sonuc = sayi1*sayi2;
                    sayi1=0;
                    sayi2=0;
                }
                else if(islem.equals("/"))
                {
                    sonuc = sayi1/sayi2;
                    sayi1=0;
                    sayi2=0;
                }
            islemekran.setText(Double.toString(sonuc)); 
            } catch (Exception e) {         

            }   
        }
        });

        sifirla.setOnClickListener(new View.OnClickListener() {     
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    islemekran.setText("");
                    islem="";
                    sayi1=0;
                    sayi2=0;
                } catch (Exception e) {

                }               
            }
        });

       teksil.setOnClickListener(new View.OnClickListener() {       
            @Override
            public void onClick(View v) {

                try {
                    if(!(islemekran.getText().toString().equals("")))
                    {
                        String kopyaekran = islemekran.getText().toString();
                        kopyaekran=kopyaekran.substring(0, kopyaekran.length()-1);
                        islemekran.setText(kopyaekran);
                    }else
                    {
                        islem="";
                    }                   
                } catch (Exception e) {

                }                   
            }
        });
    }



    @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;
    }
}

布局

    </EditText>

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="@string/no2" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="18dp"
        android:text="@string/no3" />

    <Button
        android:id="@+id/button6"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignTop="@+id/button5"
        android:text="@string/no6" />

    <Button
        android:id="@+id/button8"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button7"
        android:layout_centerHorizontal="true"
        android:text="@string/no8" />

    <Button
        android:id="@+id/button9"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button6"
        android:layout_alignTop="@+id/button8"
        android:text="@string/no9" />

    <Button
        android:id="@+id/button12"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button9"
        android:layout_alignTop="@+id/button11"
        android:text="@string/eksi" />

    <Button
        android:id="@+id/button14"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button13"
        android:layout_alignBottom="@+id/button13"
        android:layout_alignLeft="@+id/button11"
        android:text="@string/arti" />

    <Button
        android:id="@+id/button15"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button12"
        android:layout_alignTop="@+id/button14"
        android:text="@string/carpi" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="14dp"
        android:text="@string/no1" />

    <Button
        android:id="@+id/button17"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button16"
        android:layout_alignBottom="@+id/button16"
        android:layout_alignLeft="@+id/button15"
        android:text="@string/esittir" />

    <Button
        android:id="@+id/button11"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button10"
        android:layout_alignBottom="@+id/button10"
        android:layout_alignLeft="@+id/button8"
        android:text="@string/arti" />

    <Button
        android:id="@+id/button7"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button4"
        android:layout_below="@+id/button4"
        android:text="@string/no7" />

    <Button
        android:id="@+id/button10"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button7"
        android:layout_below="@+id/button7"
        android:text="@string/no0" />

    <Button
        android:id="@+id/button13"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button10"
        android:layout_below="@+id/button10"
        android:text="@string/bolu" />

    <Button
        android:id="@+id/button16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button13"
        android:layout_alignRight="@+id/button14"
        android:layout_below="@+id/button14"
        android:layout_marginTop="32dp"
        android:textColor="#00FF00"
        android:text="@string/sil" />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:text="@string/no4"/>

    <Button
        android:id="@+id/button5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button8"
        android:layout_alignLeft="@+id/button8"
        android:text="@string/no5" />

</RelativeLayout>

Logcat更新

05-27 16:46:18.641: E/Trace(802): error opening trace file: No such file or directory (2)
05-27 16:46:19.451: D/AndroidRuntime(802): Shutting down VM
05-27 16:46:19.471: W/dalvikvm(802): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-27 16:46:19.511: E/AndroidRuntime(802): FATAL EXCEPTION: main
05-27 16:46:19.511: E/AndroidRuntime(802): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hesapmakinesi/com.example.hesapmakinesi.MainActivity}: java.lang.NullPointerException
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.os.Looper.loop(Looper.java:137)
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-27 16:46:19.511: E/AndroidRuntime(802):  at java.lang.reflect.Method.invokeNative(Native Method)
05-27 16:46:19.511: E/AndroidRuntime(802):  at java.lang.reflect.Method.invoke(Method.java:511)
05-27 16:46:19.511: E/AndroidRuntime(802):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-27 16:46:19.511: E/AndroidRuntime(802):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-27 16:46:19.511: E/AndroidRuntime(802):  at dalvik.system.NativeStart.main(Native Method)
05-27 16:46:19.511: E/AndroidRuntime(802): Caused by: java.lang.NullPointerException
05-27 16:46:19.511: E/AndroidRuntime(802):  at com.example.hesapmakinesi.MainActivity.onCreate(MainActivity.java:46)
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.app.Activity.performCreate(Activity.java:5104)
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-27 16:46:19.511: E/AndroidRuntime(802):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-27 16:46:19.511: E/AndroidRuntime(802):  ... 11 more
05-27 16:47:00.441: I/Process(802): Sending signal. PID: 802 SIG: 9

1 个答案:

答案 0 :(得分:1)

您的活动无法找到ID为 no0 no1 的任何内容,依此类推,因为在您的XML文件中,他们有ID&# 39; s button2 button3 等。因此,当您尝试将NullPointerException设置为按钮时OnClickListener

所以不要这样:

Button no2 = (Button)findViewById(R.id.no2);

你需要这个:

Button no2 = (Button)findViewById(R.id.button2);

等等。