我的应用程序在MainActivity中崩溃

时间:2015-03-08 20:01:15

标签: android

有人请帮助我。我的应用程序崩溃,我不知道为什么。我的代码如下:

package com.thenexttech.formula;

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

public class MainActivity extends Activity {




Button ourButton;
    TextView theTextView;
    EditText why2;
    EditText why1;
    EditText ex2;
    EditText ex1;
    int iny2;
    int iny1;
    int inx2;
    int inx1;




@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ourButton = (Button) findViewById(R.id.button1);
        ourButton.setOnClickListener(new OurOnClickListener(this));



theTextView = (TextView) findViewById(R.id.textView3);

        why2 = (EditText) findViewById(R.id.y2);
        why1 = (EditText) findViewById(R.id.y1);
        ex2 = (EditText) findViewById(R.id.x2);
        ex1 = (EditText) findViewById(R.id.x1);


int iny2 = Integer.valueOf(why2.getText().toString());
        int iny1 = Integer.valueOf(why1.getText().toString());


int inx2 = Integer.valueOf(ex2.getText().toString());



int inx1 = Integer.valueOf(ex1.getText().toString());




}

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {



// Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.



int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

如果您需要查看更多代码,请询问。顺便说一句,我是Android开发人员的开始。我需要在帖子中添加更多细节,所以我的名字是克里斯托弗,我喜欢食物,生活很美好,是的。

1 个答案:

答案 0 :(得分:0)

我认为问题出在以下几个方面:

    int iny2 = Integer.valueOf(why2.getText().toString());
    int iny1 = Integer.valueOf(why1.getText().toString());
    int inx2 = Integer.valueOf(ex2.getText().toString());
    int inx1 = Integer.valueOf(ex1.getText().toString());

我认为这些TextView在开头是空的尝试将上面的行更改为下面的行:

    iny2 = why2.length() == 0 ? 0 : Integer.valueOf(why2.getText().toString());
    iny1 = why1.length() == 0 ? 0 : Integer.valueOf(why1.getText().toString());
    inx2 = ex2.length() == 0 ? 0 : Integer.valueOf(ex2.getText().toString());
    inx1 = ex1.length() == 0 ? 0 : Integer.valueOf(ex1.getText().toString());

如果您想在单击按钮时使用此值,则必须使用OnClickListener上的onClick方法内部的行,如下面的代码所示:

    ourButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            iny2 = why2.length() == 0 ? 0 : Integer.valueOf(why2.getText().toString());
            iny1 = why1.length() == 0 ? 0 : Integer.valueOf(why1.getText().toString());
            inx2 = ex2.length() == 0 ? 0 : Integer.valueOf(ex2.getText().toString());
            inx1 = ex1.length() == 0 ? 0 : Integer.valueOf(ex1.getText().toString());
          //Do something with these

        }
    });