-3
package com.francesco.provadinuovo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
       button =(Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("Welcome!");

            }
        });
    }
}

the problem is: 02/18 14:32:30: Launching MainActivity $ adb push C:\Users\start\AndroidStudioProjects\Provadinuovo\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.francesco.provadinuovo $ adb shell pm install -r "/data/local/tmp/com.francesco.provadinuovo" Success

$ adb shell am start -n "com.francesco.provadinuovo/com.francesco.provadinuovo.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Client not ready yet..Waiting for process to come online Connected to process 2354 on device emulator-5554 W/System: ClassLoader referenced unknown path: /data/app/com.francesco.provadinuovo-1/lib/x86 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable I/OpenGLRenderer: Initialized EGL, version 1.4 D/OpenGLRenderer: Swap behavior 1 E/EGL_emulation: tid 2445: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH) W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x971ec920, error=EGL_BAD_MATCH

frlaka
  • 9
  • 2

3 Answers3

4

You have to first call setContentView, and after that, you can use "findViewById".

Also a minor mistake with the findViewById, you need to call it in the activity, and not on the button, and then cast the returned view to whatever you're trying to use:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("Welcome!");

            }
        });
    }
Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • ^ Works and not to mention. You need to look at your layout > activity_main.xml to have the button implemented with the right ID. (Just tested and it works fine) – user7568042 Feb 18 '17 at 13:31
  • thank you so much, now it work...but i have another probleE/EGL_emulation: tid 2445: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH) W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x971ec920, error=EGL_BAD_MATCHm : – frlaka Feb 18 '17 at 13:33
  • @frlaka Edit your post and add the problems you have, not in the comments please. – JonZarate Feb 18 '17 at 13:34
  • Also, +1 this one, for explaining the reason behind the error. – JonZarate Feb 18 '17 at 13:34
2

You need to swap the 2 lines.

findViewById(R.id.button);
setContentView(R.layout.activity_main);

to

setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button)

And assign the button member variable in onCreate

button = (Button) findViewById(R.id.button);

Alternate solution using XML. You can explicitly call a method in your activity and make it acts as a callback for button click e.g. android:onClick="doAction". In your activity just add that method.

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

public void doAction(View v){
  System.out.println("Welcome!");
}
Enzokie
  • 7,365
  • 6
  • 33
  • 39
0

(edit: added cast)

I think

button.findViewById(R.id.button);
setContentView(R.layout.activity_main);

should be replaced with:

setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);

The first line build the layout, the second stores the reference to the button widget into button, which would be otherwise null.

rrobby86
  • 1,356
  • 9
  • 14