S. A.
S. A.

Reputation: 76

Android Studio | Button onClick in a NavigationDrawer Fragment

I'm struggling with these errors for a few days already, I tried googling it, but unfortunately I can't find any fixes for this. I'm still a beginner in Java for Android.

So I got a NavigationDrawer with a Fragment, in that fragment i want to put a button which is clickable, but somehow it just doesn't recognize the button and some things while I've got it in my layout.

This is my code:

package nl.c99.c99nlapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Souf on 21-9-2015.
 */
public class First_Fragment extends Fragment {
    View MyView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MyView = inflater.inflate(R.layout.first_layout, container, false);
        return MyView;

        View rootView = inflater.inflate(R.layout.first_layout, container, false);
        Button c = (Button) rootView.findViewById(R.id.button2);
        c.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onClick(v);//THIS IS THE METHOD YOU WROTE ON THE ATTACHED CODE!!
            }
        });

    }
}

Screenshot of my project:

First_Fragment Class https://i.sstatic.net/5W2uG.png

Layout of first_fragment: https://i.sstatic.net/qXYPj.png

I'm getting the following errors:

Upvotes: 1

Views: 1202

Answers (2)

Hussein El Feky
Hussein El Feky

Reputation: 6707

You must return your rootView at the end of your code, because return means "end that method", so it won't continue the code below it in the same method.

Replace your code with this one:

package nl.c99.c99nlapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class First_Fragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.first_layout, container, false);

        Button c = (Button) rootView.findViewById(R.id.button2);
        c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Write your code here.
            }
        });

        return rootView;
    }
}

Upvotes: 0

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20626

Cannot resolve symbol 'OnClickListener' (new OnClickListener)

Add this in your imports : import android.view.View.OnClickListener;

You just have to choose one of your views, since you have 2 I don't know why rootView and MyView then your onClickListener() should be :

Button c = (Button) MyView.findViewById(R.id.button2);
    c.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //your stuff here
        }
    });

Parameter 'v' is never used. (View v)

Just remove this onClick(v)

Your coude should work if you do this :

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    MyView = inflater.inflate(R.layout.first_layout, container, false);

    Button c = (Button) MyView.findViewById(R.id.button2);
    c.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Stuff there
        }
    });
  return MyView;
}

All fo your code

    package nl.c99.c99nlapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

/**
 * Created by Souf on 21-9-2015.
 */
public class First_Fragment extends Fragment {
    View MyView;

    @Nullable
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    MyView = inflater.inflate(R.layout.first_layout, container, false);

    Button c = (Button) MyView.findViewById(R.id.button2);
    c.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Stuff there
        }
    });
  return MyView;
}
}

Upvotes: 1

Related Questions