Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的应用程序在MainActivity中崩溃_Android - Fatal编程技术网

Android 我的应用程序在MainActivity中崩溃

Android 我的应用程序在MainActivity中崩溃,android,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 and

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

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开发者。我需要在我的帖子中添加更多细节,所以,嗯,我的名字是克里斯托弗,我喜欢食物,生活很好,是的。

我想问题出在以下几行:

    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());
我假设这些文本视图开头是空的,请尝试将上面的行更改为下面的行:

    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

        }
    });

嗨,克里斯托弗,欢迎来到SO。请发布异常的stacktrace,这将有助于缩小问题的范围。@Egor stacktrace是什么?请搜索它,了解什么是“logcat”和什么是“stack trace”将非常有帮助@StealthDroid从logcatPost发布您的日志发布您的logcat,但这绝对像是您试图将空白字符串转换为整数的情况。因为您的EditExt在开始时可能没有内容。