应用未部署

时间:2016-05-14 21:43:04

标签: java android

我已经开始在android中编码了。在运行我的应用程序时,我收到消息"不幸的是,myapp已停止"。 我已附上以下代码。

你能告诉我这段代码有什么问题吗?

代码如下:

     package in.developer.rjsharma.myfirstapp;

     import android.support.v7.app.AppCompatActivity;
     import android.os.Bundle;
     import android.text.Editable;
     import android.view.View;
     import android.widget.Button;
     import android.widget.EditText;
     import android.widget.TextView;
     import android.view.View.OnClickListener;

     public class MainActivity extends AppCompatActivity {

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

             EditText fno = (EditText) findViewById(R.id.fno);

             String no1=fno.getText().toString();
             int first=Integer.parseInt(no1);


             EditText sno = (EditText) findViewById(R.id.sno);

             String no2=fno.getText().toString();
             int second=Integer.parseInt(no2);

             final int r1=first+second;

             final int r2=first-second;

             final int r3=first*second;

             final int r4=first/second;

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

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

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

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

             final TextView solution = (TextView) findViewById(R.id.solution);

             add.setOnClickListener(new Button.OnClickListener(){

                 @Override
                 public void onClick(View v) {
                     // TODO Auto-generated method stub
                     solution.setText(r1);
                 }
         });

             subtract.setOnClickListener(new Button.OnClickListener(){

                 @Override
                 public void onClick(View v) {
                     // TODO Auto-generated method stub
                     solution.setText(r2);
                 }
             });

             multiply.setOnClickListener(new Button.OnClickListener(){

                 @Override
                 public void onClick(View v) {
                     // TODO Auto-generated method stub
                     solution.setText(r3);
                 }
             });

             divide.setOnClickListener(new Button.OnClickListener(){

                 @Override
                 public void onClick(View v) {
                     // TODO Auto-generated method stub
                     solution.setText(r4);
                 }
             });


         }

     }

2 个答案:

答案 0 :(得分:1)

您不能在Integer中设置Textview。您必须将Integer转换为String

package in.developer.rjsharma.myfirstapp;    
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText fno = (EditText) findViewById(R.id.fno);

        String no1=fno.getText().toString();
        int first=Integer.parseInt(no1);

        EditText sno = (EditText) findViewById(R.id.sno);

        String no2=fno.getText().toString();
        int second=Integer.parseInt(no2);

        final int r1=first+second;
        final int r2=first-second;
        final int r3=first*second;
        final int r4=first/second;

        Button add = (Button) findViewById(R.id.add);
        Button subtract = (Button) findViewById(R.id.subtract);
        Button multiply = (Button) findViewById(R.id.multiply);
        Button divide = (Button) findViewById(R.id.divide);

        final TextView solution = (TextView) findViewById(R.id.solution);

        add.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                solution.setText(String.valueOf(r1));
            }
        });

        subtract.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                solution.setText(String.valueOf(r2));
            }
        });

        multiply.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                solution.setText(String.valueOf(r3));
            }
        });

        divide.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                solution.setText(String.valueOf(r4));
            }
        });
    }
}

答案 1 :(得分:0)

你在代码中犯了一些错误。您正在阅读EditText fnoonCreate()的值,它刚刚创建,其值为空String。如果您将其解析为某个值,我不知道它是0还是null

但是,我会以这种方式尝试(未经过测试 - 可能是solution或其他变量的错误):

public class MainActivity extends AppCompatActivity {

    private EditText fno, sno;
    private TextView solution;

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

        EditText fno = (EditText) findViewById(R.id.fno);
        EditText sno = (EditText) findViewById(R.id.sno);

        Button add = (Button) findViewById(R.id.add);
        Button subtract = (Button) findViewById(R.id.subtract);
        Button multiply = (Button) findViewById(R.id.multiply);
        Button divide = (Button) findViewById(R.id.divide);

        TextView solution = (TextView) findViewById(R.id.solution);

        add.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                solution.setText(readIntegerFromEditText(fno)+readIntegerFromEditText(sno));
            }
        });

        subtract.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                solution.setText(readIntegerFromEditText(fno)-readIntegerFromEditText(sno));
            }
        });

        multiply.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                solution.setText(readIntegerFromEditText(fno)*readIntegerFromEditText(sno));
            }
        });

        divide.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                solution.setText(readIntegerFromEditText(fno)/readIntegerFromEditText(sno));
            }
        });
    } 

    private int readIntegerFromEditText (EditText editText){
        int no = Integer.parseInt(editText.getText());
        return no;
    }
}

还有很多事要做,比如检查0师。如果你使用LogCat和Debugging消息,你会发现在路上。