-1

I write code and it look ok. I dont know what is the reson that I get ERROR when I write setOnClickListener(this) all I want is to create button in fragment.

"Error:(25, 78) error: incompatible types: void cannot be converted to Button" "Error:Execution failed for task ':android:compileDebugJava'.

Compilation failed; see the compiler error output for details."

Many Thanks for any Help

the source code:

    import android.os.Bundle;
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.Toast;

import com.popthejam.game.android.R;

public class Wellcome extends Fragment implements View.OnClickListener {

    private Button btnLOGIN ;

    public Wellcome() {
        // Required empty public constructor
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_wellcome,container,false);
        btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);

        return view;
    }

    public void onClick(View view) {
        Toast.makeText(getActivity(), "LOGIN BUTTON PRESS", Toast.LENGTH_SHORT).show();

    }
}

----

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/popthejam"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragment.Wellcome">
<TextView
    android:gravity="center"
    android:textSize="@dimen/profile_entry_text_size"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/welcome_to_app" />
    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@android:color/holo_orange_dark"
        android:text="Login"
         />
</FrameLayout>
River
  • 8,585
  • 14
  • 54
  • 67
  • Code is not ok, this line is faulty: `btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);` – pleft Nov 09 '17 at 07:56

3 Answers3

1

The error is that you're trying to assign the result of setOnClickListener() (void) to your button variable.

So change this

btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);

to this

btnLOGIN = (Button) view.findViewById(R.id.btnLogin);
btnLOGIN.setOnClickListener(this);
Headcracker
  • 522
  • 4
  • 19
  • now is working, I not understand why? – Tom Hanktomhank Nov 09 '17 at 07:57
  • Line `btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);` assigns the result returned by `setOnClickListener` to `btnLOGIN`. The result is...`void` so it tries to assign the `void` to a `Button` which is wrong. – pleft Nov 09 '17 at 07:58
1
(Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);

Above code return a void function. So :

btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this); 

not working because u register a button by the function.

Let do like @Headcracker suggess

kemdo
  • 1,429
  • 3
  • 15
  • 29
0

Use this

btnLOGIN = (Button) view.findViewById(R.id.btnLogin);
btnLOGIN.setOnClickListener(this);

And implement the overridden onClick in your fragment class

 @Override
    public void onClick(View view) {
 switch (v.getId()) {

            case R.id.btnLogin:
 Toast.makeText(getActivity(), "LOGIN BUTTON PRESS", Toast.LENGTH_SHORT).show();
break;
}


        }
Quick learner
  • 10,632
  • 4
  • 45
  • 55