java.lang.runtimeexception无法启动活动组件信息

时间:2015-04-23 06:38:27

标签: java android runtime-error

我在此代码中出现此错误

  

java.lang.runtimeexception无法启动活动组件信息

package estimatewall.example.com.estimatewall;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu; 
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    EditText inputTxt;
    EditText inputTxt1;
    TextView setText;

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

        inputTxt = (EditText) findViewById(R.id.editText);

        // Store EditText in Variable

        int val = Integer.parseInt(inputTxt.getText().toString());

        inputTxt1 = (EditText) findViewById(R.id.editText2);

        // Store EditText in Variable

        int val1 = Integer.parseInt(inputTxt1.getText().toString());

        float block1,block2;
        final float sum;
        block1=(val*12)/7;
        block2=(val1*12)/10;
        sum=block1*block2;

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Perform action on click
                try {
                    TextView textView = (TextView) findViewById(R.id.textView3);
                    textView.setText("" + sum);
                }catch (Exception e)
                {
                    e.printStackTrace();
                }


            }
        });





    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:1)

可能会NumberFormatException,因为这里:

int val = Integer.parseInt(inputTxt.getText().toString());

尝试将null转换为int

在点击监听器上使用Button inputTxt.getText()方法中的onClick获取用户在Edittext中输入的值,或者将两个EditText中的一些默认值设置为android:text="0"