Dzyuv001
Dzyuv001

Reputation: 328

No errors in Android studio but application crashes

I am new to android development and tried to make a simple calculator app. The last thing that I changed was try to concatenate two string into one and display the new string in the EditText. There are no errors in my project when I look at it in Android Studio, however when I run the application on my phone it stops working. Is this a phone problem or ... ?

   package com.example.jeff.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    double total = 0;
    double transaction = 0;
    String Back = "";
    String currentString = "";
    String transactionString = "";
    EditText calcInput = (EditText) findViewById(R.id.EtCalc);

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

        buttonSetup();
    }

    private void concatenation() {
        transactionString = transactionString + currentString;
        calcInput.setText(String.valueOf(transactionString));
    }

    private void buttonSetup() {

        Button button0 = (Button) findViewById(R.id.Btn0);
        Button button1 = (Button) findViewById(R.id.Btn1);
        Button button2 = (Button) findViewById(R.id.Btn2);
        Button button3 = (Button) findViewById(R.id.Btn3);
        Button button4 = (Button) findViewById(R.id.Btn4);
        Button button5 = (Button) findViewById(R.id.Btn5);
        Button button6 = (Button) findViewById(R.id.Btn6);
        Button button7 = (Button) findViewById(R.id.Btn7);
        Button button8 = (Button) findViewById(R.id.Btn8);
        Button button9 = (Button) findViewById(R.id.Btn9);

        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "0";
                calcInput.setText(String.valueOf(transactionString));
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "1";
                concatenation();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "2";
                concatenation();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "3";
                concatenation();
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "4";
                concatenation();
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "5";
                concatenation();
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "6";
                concatenation();
            }
        });
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "7";
                concatenation();
            }
        });
        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "8";
                concatenation();
            }
        });
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentString = "9";
                concatenation();
            }
        });
        // (EditText) inputTxt = (EditText) findViewById(R.id.input);

// Store EditText in Variable

        // String str = inputTxt.getText().toString();
    }

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

LOGCAT

   09-07 23:54:12.649  22823-22823/com.example.jeff.calculator I/art﹕ Late-enabling -Xcheck:jni
09-07 23:54:12.718  22823-22823/com.example.jeff.calculator D/AndroidRuntime﹕ Shutting down VM
09-07 23:54:12.719  22823-22823/com.example.jeff.calculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.jeff.calculator, PID: 22823
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jeff.calculator/com.example.jeff.calculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2290)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:211)
            at android.app.ActivityThread.main(ActivityThread.java:5373)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.Activity.findViewById(Activity.java:2072)
            at com.example.jeff.calculator.MainActivity.<init>(MainActivity.java:22)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1606)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:211)
            at android.app.ActivityThread.main(ActivityThread.java:5373)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

Upvotes: 0

Views: 6175

Answers (3)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 365058

You are defining a variable

EditText calcInput = (EditText) findViewById(R.id.EtCalc);

It means that when the class is initialized, your are trying to to find an element in a null windows.

You can't do it.

Just use:

EditText calcInput;

Then init the variable in your onCreate method.

Something like:

public class MainActivity extends AppCompatActivity {
    //...
    EditText calcInput;

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

        buttonSetup();

        calcInput = (EditText) findViewById(R.id.EtCalc);
    }

}

Upvotes: 1

VJ V&#233;lan Solutions
VJ V&#233;lan Solutions

Reputation: 6564

You have to move this line :

EditText calcInput = (EditText) findViewById(R.id.EtCalc);

below the following line:

setContentView(R.layout.activity_main);

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007604

Do not call methods that you are inheriting from Activity until after super.onCreate() has been called, unless specifically told otherwise.

Also, you cannot call findViewById() until you call setContentView(), as there are no widgets to find.

Replace:

EditText calcInput = (EditText) findViewById(R.id.EtCalc);

with:

EditText calcInput;

and add:

calcInput = (EditText) findViewById(R.id.EtCalc);

after your call to setContentView() in onCreate().

Upvotes: 4

Related Questions