1

I'm new to this and i'm doing an app for a university project and I wanted to put a VideoView in my app, so I watched this video ("https://www.youtube.com/watch?v=SrPHLj_q_OQ&t=421s") and did it and worked. But then I add to copy the code from content_main.xml to a fragment and it stopped working and giving an error on the "android:onClick" on the Button. And when I press CTRL+F1 to inspect it says this:

"Corresponding method handler'public void videoplay(android.view.View)' not found
Inspection info:The onClick attribute value should be the name of a method in this View's context to invoke when the view is clicked.This name must correspond to a public method that takes exactly one parameter of type View.

Must be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character.

Issue id:OnClick "

Heres my xml:

<?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:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:background="#003e6f"
    android:orientation="vertical"
    tools:context=".InicioFragment">


    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_height="197dp" />

    <Button
        android:id="@+id/button2"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="#FF6600"
        android:text="Play"
        android:onClick="videoplay"
        android:textColor="#ffffff" />

    <ImageView
        android:id="@+id/imagem2"
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="false"
        android:background="@drawable/tech3" />

</LinearLayout>

Heres my java:

package intro.android.migueloliveiraapp;

import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {



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

        clk = (Button) findViewById(R.id.button2);
        videov = (VideoView) findViewById(R.id.videoView);



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        //Carregar o layout do fragent incial "InicioFragment"
        InicioFragment inicioFragment = new InicioFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction()
                .replace(R.id.relative_layout_para_o_fragment, inicioFragment, inicioFragment.getTag())
                .commit();


    }



    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_oliveira) {
            Quem_Oliveira quem_oliveiraFragment = new Quem_Oliveira();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction()
                    .replace(R.id.relative_layout_para_o_fragment,
                            quem_oliveiraFragment, quem_oliveiraFragment.getTag())
                    .commit();
            Toast.makeText(this, "Oliveira", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_profissao) {
            Profissao profissaoFragment = new Profissao();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction()
                    .replace(R.id.relative_layout_para_o_fragment,
                            profissaoFragment, profissaoFragment.getTag())
                    .commit();
            Toast.makeText(this, "Profissão", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_feitos) {
            Principais_feitos principais_feitosFragment = new Principais_feitos();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction()
                    .replace(R.id.relative_layout_para_o_fragment,
                            principais_feitosFragment, principais_feitosFragment.getTag())
                    .commit();
            Toast.makeText(this, "Principais Feitos", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_academicas) {
            Habilitacoes_Academicas habilitacoes_academicasFragment = new Habilitacoes_Academicas();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction()
                    .replace(R.id.relative_layout_para_o_fragment,
                            habilitacoes_academicasFragment, habilitacoes_academicasFragment.getTag())
                    .commit();
            Toast.makeText(this, "Habilitações Académicas", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_galeria) {
            Galeria galeriaFragment = new Galeria();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction()
                    .replace(R.id.relative_layout_para_o_fragment,
                            galeriaFragment, galeriaFragment.getTag())
                    .commit();
            Toast.makeText(this, "Galeria", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_contactos) {
            Contactos contactosFragment = new Contactos();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction()
                    .replace(R.id.relative_layout_para_o_fragment,
                            contactosFragment, contactosFragment.getTag())
                    .commit();
            Toast.makeText(this, "Contactos", Toast.LENGTH_SHORT).show();

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    //video view
    Button clk;
    VideoView videov;

    public void videoplay(View v){
        String videopath = "android.resource://intro.android.migueloliveiraapp/" + R.raw.oliveira;
        Uri uri = Uri.parse(videopath);
        videov.setVideoURI(uri);
        videov.start();
    }


}

Heres my InicioFragment java:

package intro.android.migueloliveiraapp;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class InicioFragment extends Fragment {


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_inicio, container, false);
    }

}

heres my updated InicioFragment.java:

package intro.android.migueloliveiraapp;


import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.net.Uri;
import android.widget.Button;
import android.widget.VideoView;



/**
 * A simple {@link Fragment} subclass.
 */
public class InicioFragment extends Fragment {


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



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

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_inicio, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        clk = (Button) view.findViewById(R.id.button);
        videov = (VideoView) view.findViewById(R.id.videoView);

        // bind the views here.
        Button button2 = view.findViewById(R.id.button);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something here.

            }
        });
    }

    Button clk;
    VideoView videov;


    public void videoplay(View v){
        String videopath = "android.resource://intro.android.migueloliveiraapp/" + R.raw.oliveira;
        Uri uri = Uri.parse(videopath);
        videov.setVideoURI(uri);
        videov.start();
    }



}

5 Answers5

0

You can use this for click.

Button mPlayVideo;

//In oncreate

mPlayVideo = findViewById(R.id.button2);
mPlayVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
            }
        });
Mbuodile Obiosio
  • 1,463
  • 1
  • 15
  • 19
0

In your xml your tools:context=".InicioFragment refers to your fragment and not your activity.

Your method is found inside the activity and not the fragment class and this is why are getting this error.

You can check Call an activity method from a fragment

But I can recommend using the onClick attribute inside your activities and inside fragment use normal click listener:

View view =  findViewById(R.id.viewId);
view .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    //do something
  }
});
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
0

Pay attention on your xml file that this file's context is your InicioFragment (tools:context=".InicioFragment" ) Therefore, videoplay(View v) should be inside your InicioFragment class. That's the reason why is not found. It won't search on your MainActivity.

public class InicioFragment extends Fragment {


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.testclassfragment, container, false);
        Button clk = (Button) view.findViewById(R.id.button);
        VideoView videov = (VideoView) view.findViewById(R.id.videoView);
        return view;
    }

    public void videoplay(View v){
        String videopath = "android.resource://intro.android.migueloliveiraapp/" + R.raw.oliveira;
        Uri uri = Uri.parse(videopath);
        videov.setVideoURI(uri);
        videov.start();
    }
}

Since you also have a variable for your button another option is to do:

clk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Log.d("TESTING", "Clicked");
            }
        });
S. Souza
  • 110
  • 7
  • I updated the question with my InicioFragment java. If you could try and put the method there, because its giving me error on "findViewById" when I copy it from MainActivity to InicioFragment. – Diogo Gonçalves Apr 09 '19 at 21:04
  • on my MainActivity I have "clk = (Button) findViewById(R.id.button); videov = (VideoView) findViewById(R.id.videoView);" inside "protected void onCreate(Bundle savedInstanceState)" don't I have to put it in InicioFragment too? and when I try to put it it gives me the error I was talking about on "findViewByid" – Diogo Gonçalves Apr 09 '19 at 21:11
  • Yes, you're right about that, clk and videov must be on your fragment. I have updated my answer, check out how it's done on a fragment. It's a bit trick indeed – S. Souza Apr 10 '19 at 11:16
  • I have put the videoplay method inside InicioFragment, and it dosen't give me any errors, but its says "Method videoplay is never used", do you know why? – Diogo Gonçalves Apr 12 '19 at 08:24
0

This is because you created videoplay() method inside your MainActivity.java . You should put your videoplay() method into the InicioFragment.java as it is the corresponding file for the xml layout you have mentioned above.

Initialize your variables like bellow inside your onCreateView method on InicioFragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_inicio, container, false);

        Button btn1 = view.findViewById(R.id.button_id);
        .....
             .....

        return view;
    }

    public void videoplay(View v){
          .........
          .....
    }

Alif Hasnain
  • 1,176
  • 1
  • 11
  • 24
  • I updated the question with my InicioFragment java. If you could try and put the method there, because its giving me error on "findViewById" when I copy it from MainActivity to InicioFragment. – Diogo Gonçalves Apr 09 '19 at 21:03
0

The following error:

"Corresponding method handler'public void videoplay(android.view.View)' not found
Inspection info:The onClick attribute value should be the name of a method in this View's context to invoke when the view is clicked.This name must correspond to a public method that takes exactly one parameter of type View.

Must be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character.

Issue id:OnClick "

means that you forget to add the corresponding method in your Activity class when using android:onClick="videoplay" attribute. So, you need to add the following method to your activity:

public void videoplay(View v){
  // do something here.
}

The most important part is, android:onClick tag is only working when you're using it inside the content layout of Activity.. So, android:onClick doesn't work when you're using it inside the fragment layout. You need to use the onClickListener instead.

You need to bind the view and add the onClickListener with something like this:

public class InicioFragment extends Fragment {

    public InicioFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_inicio, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // bind the views here.
        Button button2 = view.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // do something here.
            }
        });
    }
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96