Android Studio在启动应用

时间:2018-02-08 18:26:09

标签: java android

所以我有这段代码

    package me.danilkp1234.laerkeholtmilk;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.google.firebase.crash.FirebaseCrash;

import static me.danilkp1234.laerkeholtmilk.R.id.textView;
import static me.danilkp1234.laerkeholtmilk.R.id.textView3;

public class MainActivity extends AppCompatActivity {

private final EditText milkdone = (EditText) findViewById(R.id.milkliter);
private final EditText brixdone = (EditText) findViewById(R.id.brix);
private double milkdone2 = Double.parseDouble(milkdone.getText().toString());
private double brixdone2 = Double.parseDouble(brixdone.getText().toString());
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    milkdone.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                double milkdone2 = Double.parseDouble(milkdone.getText().toString());
                EditText brix = (EditText) findViewById(R.id.brix);
                double brixdone = Double.parseDouble(brix.getText().toString());
                double test2 = brixdone + milkdone2;
                TextView change = (TextView) findViewById(R.id.textView3);
                change.setText(String.valueOf(test2));
                handled = true;
            }
            return handled;
        }

    });
    brixdone.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE){
                double test2 = brixdone2 + milkdone2;
                TextView change = (TextView) findViewById(R.id.textView3);
                change.setText(String.valueOf(test2));
                handled = true;
            }
            return handled;
        }
    });
    };
    public void onStop() {
        super.onStop();
        SavePreferences("savedbrixdone",String.valueOf(brixdone2));

    }
private void SavePreferences(String key, String value){
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

private void LoadPreferences(){
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    String strSavedMem1 = sharedPreferences.getString("MEM1", "");
    String strSavedMem2 = sharedPreferences.getString("MEM2", "");

}
}

私人决赛EditText milkdone =(EditText)findViewById(R.id.milkliter);

私人最终EditText brixdone =(EditText)findViewById(R.id.brix);

private double milkdone2 = Double.parseDouble(milkdone.getText()。toString());

private double brixdone2 = Double.parseDouble(brixdone.getText()。toString());

我添加了这4行,现在我的应用程序在启动错误时崩溃,看起来它还没有加载 这是我的SplashScreen

    package me.danilkp1234.laerkeholtmilk;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    import static java.lang.Thread.sleep;

 public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    try {
        Intent intent = new Intent(this, MainActivity.class);
        sleep(2000);
        startActivity(intent);
        finish();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

}

这是logcat的错误     30562-30562 /? E / UncaughtException:java.lang.RuntimeException:无法实例化活动ComponentInfo {me.danilkp1234.laerkeholtmilk / me.danilkp1234.laerkeholtmilk.MainActivity}:java.lang.NullPointerException:尝试调用虚方法' android.view。 Window $ Callback android.view.Window.getCallback()'在空对象引用上

1 个答案:

答案 0 :(得分:0)

在创建视图之前,无法实例化视图(编辑文本)。您正在调用findviewbyid并且android不知道在哪里查看,因为尚未创建视图/活动。我认为这是问题所在,你正在尝试从尚不存在的视图中获取值。将实例化移动到像onlow一样的oncreate:

// CASE 1 Show Hide on click, no slide effect yet  
class ServicesDropdown extends Component {
    constructor() {
        super();
        this.state = {
            dropdown: false
        };
    }

    handleClick = () => {
        if (!this.state.dropdown) {
            // attach/remove event handler
            document.addEventListener('click', this.handleOutsideClick, false);
        } else {
            document.removeEventListener('click', this.handleOutsideClick, false);
        }

        this.setState(prevState => ({
            dropdown: !prevState.dropdown,
        }));
    }

    handleOutsideClick = (e) => {
        // ignore clicks on the component itself
        if (this.node.contains(e.target)) {
            return;
        }
        this.handleClick();
    }

    render() {
        return (
            <li ref={node => { this.node = node; }}>
                <a href="#!" onClick={this.handleClick}>Services +</a>
                {this.state.dropdown && 
                (
                    <ul className="nav-dropdown" ref={node => { this.node = node; }}>
                    <li><a href="#!">Web Design</a></li>
                    <li><a href="#!">Web Development</a></li>
                    <li><a href="#!">Graphic Design</a></li>
                    </ul>

                )}
            </li>
        )
    }
}