NullPointerException / Button findViewById

0

I have a problem with this code: every time I put it:

'Button botao = (Button) findViewById(R.id.botao_id);'

It stops working, it does not run on my phone anymore. And if I take that code (and things related to it, it works again). I asked my friend to test his machine and it works perfectly. I tried to read something related to NullPointerException, but I could not solve my problem ...

Java CODE:

package com.example.android.justjava;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    int quantity = 0;
    Button botao = (Button) findViewById(R.id.botao_id);

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

    public void increment(View view) {
        quantity = quantity + 1;
        display(quantity);
    }


      public void minus(View view) {
        if(quantidade>0) {
            quantidade -= 1;
            display();
        }
        if(quantidade==0){
            botao.setClickable(false);
            botao.setTextColor(Color.rgb(153,153,153));
        }
    }


    public void submitOrder(View view) {
        displayPrice(quantity * 5);
    }


    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }

    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);

    priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }
}

XML CODE:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@drawable/coffee"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.android.justjava.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Compre Seu Café"
    android:textColor="#fff"
    android:textSize="50sp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:gravity="center"
    android:text="Quantidade"
    android:textAllCaps="true"
    android:textColor="#fff"
    android:textSize="30sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <Button
        android:id="@+id/botao_id"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:clickable="false"
        android:onClick="decrement"
        android:text="-"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textColor="#fff"
        android:textSize="30sp" />

    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:onClick="increment"
        android:text="+"
        android:textSize="30sp" />
</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16sp"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:text="Preço"
    android:textColor="#fff"
    android:textSize="30sp" />

<TextView
    android:id="@+id/price_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="R$0,00"
    android:textColor="#fff"
    android:textSize="30sp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:onClick="submitOrder"
    android:text="Pedir"
    android:textSize="30sp" />

Errors found on Android Monitor

07-22 15:45:46.260 18338-18338/com.example.android.justjava E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.android.justjava, PID: 18338
                                                                          java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.justjava/com.example.android.justjava.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2561)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                              at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
                                                                              at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:155)
                                                                              at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
                                                                              at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
                                                                              at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
                                                                              at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
                                                                              at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
                                                                              at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                                                                              at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                                                                              at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                                              at com.example.android.justjava.MainActivity.<init>(MainActivity.java:17)
                                                                              at java.lang.Class.newInstance(Native Method)
                                                                              at android.app.Instrumentation.newActivity(Instrumentation.java:1100)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2551)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6123) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
    
asked by anonymous 22.07.2017 / 21:55

1 answer

1

You have to put findViewById(R.id.botao_id) inside the onCreate() method of your activity . Here's how it should look:

Button botao;

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

    botao = (Button) findViewById(R.id.botao_id);
}
    
22.07.2017 / 22:49