-2

So I got this code but I'm having a problem with it crashing when creating a public or private class

    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", "");

}
}

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());

It all started when I added these 4 lines of code

If its need here is the code for my 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();
    }

}

}

And here is the Error Report from Logcat

30562-30562/? E/UncaughtException: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{me.danilkp1234.laerkeholtmilk/me.danilkp1234.laerkeholtmilk.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

1 Answers1

0

You can't instantiate your views (Edit text) before the view is created. You are calling findviewbyid and android doesn't know where to look, since the view/activity hasn't been created. I think that's the problem and you are trying to get values from views that don't exist yet. Move you instantiation into oncreate like bellow:

private final EditText milkdone;
private final EditText brixdone;
private double milkdone2;
private double brixdone2;

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

milkdone = (EditText) findViewById(R.id.milkliter);
brixdone = (EditText) findViewById(R.id.brix);

milkdone.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            milkdone2 = Double.parseDouble(milkdone.getText().toString());
            brixdone2 = Double.parseDouble(brixdone.getText().toString());
Joshua
  • 589
  • 1
  • 5
  • 19