0

I am trying to open a new activity by clicking on an imagebutton. I placed the imagebutton and tried to make an onclick method for it. The app already crashes on the place where I try to find my button.It does print the first Log but that's it. It doesn't even get to the onClick method. I am very new to android so I have very little experience working with it.

public class Bestellingen extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ArrayList<String> elementen;
    private ListView lV;
    private MenuAdapter mA;


    ImageView image;
    Spinner spinner1;
    ImageButton imageButtonWinkelmand;

    public Bestellingen() {
        Log.d("tag","test");
        imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand);  // here
        Log.d("testje","erna");
}

My onClick method

   imageButtonWinkelmand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("in de onclick","test");
                startActivity(new Intent(Bestellingen.this, BestellingenInfo.class));
            }
        });

and I did make it in the activity_main.xml

<ImageButton

    android:layout_width="80dp"
    android:layout_height="70dp"
    android:scaleType="fitXY"
    android:id="@+id/imageWinkelmand"
    android:src="@drawable/winkelmand"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="-20dp"
    android:background="#00000000"
    android:contentDescription="winkelmandje"/>

Chat Conversation End

3
  • Post your logcat here. Commented Jan 4, 2016 at 12:27
  • 2
    call this in your oncreate section imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand); Commented Jan 4, 2016 at 12:27
  • 1
    Putting it in onCreate() worked, thanks! Commented Jan 4, 2016 at 12:31

3 Answers 3

2

Override onCreate() and find views inside onCreate().

ImageButton imageButtonWinkelmand;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Find your views in onCreate();
            imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand); 

    }

Use like this.

Sign up to request clarification or add additional context in comments.

1 Comment

I can't yet, need to wait 10 mins
1
ImageButton imageButton;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Find your views in onCreate();
            imageButton = (ImageButton) findViewById(R.id.imageWinkelmand); 

    }

Comments

1

Remove Bestellingen method at first .

Then call ImageButton in your oncreate() section

imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand);

Finally

 @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        setContentView(R.layout.Your_layout_name);
        imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand);

1 Comment

@LievenLuyckx Glad to hear .Move ahead .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.