Q:

Android application crashes every time on startup

I started coding my first app in Android Studio, what it should do at this stage is that you click a button with the digit and it outputs it into the textfield. When i run this code the application crashes right on startup and I have no idea what's wrong.
MyActivity.java
package com.example.david.calculator;

import android.app.Activity;
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 org.w3c.dom.Text;

public class MyActivity extends Activity {

EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Button one = (Button) findViewById(R.id.button);
    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BtnPressed(1);
        }
    });

}

private void BtnPressed(int i) {
    result.setText(Integer.toString(i));
}

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

activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<EditText
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/number"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="0"
    android:background="#ffe7e7e7"
    android:textSize="30sp"
    android:gravity="center_vertical|right"
    android:paddingRight="20dp"
    android:editable="true"
    android:enabled="true"
    android:numeric="integer|signed|decimal" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="1"
    android:id="@+id/button"
    android:layout_below="@+id/number"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="2"
    android:id="@+id/button2"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button"
    android:layout_toRightOf="@+id/button"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="3"
    android:id="@+id/button3"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="4"
    android:id="@+id/button4"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="5"
    android:id="@+id/button5"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button4"
    android:layout_toRightOf="@+id/button4"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="6"
    android:id="@+id/button6"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button5"
    android:layout_toRightOf="@+id/button5"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="7"
    android:id="@+id/button7"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_below="@+id/button4"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="8"
    android:id="@+id/button8"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button7"
    android:layout_toRightOf="@+id/button7"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="9"
    android:id="@+id/button9"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button8"
    android:layout_toRightOf="@+id/button8"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="0"
    android:id="@+id/button11"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_below="@+id/button7"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="160dp"
    android:layout_height="75dp"
    android:text="="
    android:id="@+id/button12"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button11"
    android:layout_toRightOf="@+id/button11"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="+"
    android:id="@+id/button13"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button3"
    android:layout_toRightOf="@+id/button3"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="-"
    android:id="@+id/button14"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button6"
    android:layout_toRightOf="@+id/button6"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="*"
    android:id="@+id/button15"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button9"
    android:layout_toRightOf="@+id/button9"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="/"
    android:id="@+id/button16"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button12"
    android:layout_toRightOf="@+id/button12"
    android:layout_marginLeft="10dp" />

Logcat:
07-24 16:30:07.894  26564-26564/com.example.david.calculator D/AndroidRuntime﹕ Shutting down VM
07-24 16:30:07.894  26564-26564/com.example.david.calculator W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4190aba8)
07-24 16:30:07.899  26564-26564/com.example.david.calculator E/AndroidRuntime﹕ FATAL      EXCEPTION: main
Process: com.example.david.calculator, PID: 26564
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.david.calculator/com.example.david.calculator.MyActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.app.Activity.findViewById(Activity.java:1884)
        at com.example.david.calculator.MyActivity.<init>(MyActivity.java:18)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1208)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)  
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

Thank you very much for all the replies.
            

A:

You are setting your EditText in your main class and not in your classes onCreate method. You can't find a view if the activity hasn't been created yet.
public class MyActivity extends Activity {

EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Button one = (Button) findViewById(R.id.button);
    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BtnPressed(1);
        }
    });

}

Should become
public class MyActivity extends Activity {

EditText results;
private int number;
private EditText result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    result = (EditText) findViewById(R.id.number);
    Button one = (Button) findViewById(R.id.button);
    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BtnPressed(1);
        }
    });

}

You were getting a NullPointerException in your log, this should fix it.

