Anonymous class must be declared abstract or implement Onclick method

+1 Rajani Krishna Turlapati · August 26, 2015
button.setOnClickListener(
new View.OnClickListener()
{

public void OnClick(View v){
buttonClicked(v);
}
}

In the code given above, I am getting error on the underlined line saying anonymous class derived from on click listener must be declared abstract or must implement OnClickView(View) in onlicklistener. I am pasting the rest of the code below. Please tell me where I am going wrong.

import android.content.Context;
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.app.Activity;
import android.widget.Button;
import android.widget.EditText;


public class TopSectionFragment extends Fragment {

@Nullable

private static EditText topPanel;
private static EditText BottomPanel;

TopSectionListener ActivityCommander;

public interface TopSectionListener{

public void createMeme(String Top,String Bottom);


}


public void onAttach(Activity activity) {

super.onAttach(activity);
try {
ActivityCommander = (TopSectionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());

}
}

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

topPanel = (EditText) view.findViewById(R.id.topPanel);
BottomPanel = (EditText) view.findViewById(R.id.BottomPanel);
final Button button = (Button) view.findViewById(R.id.MyButton);

button.setOnClickListener(
new View.OnClickListener()
{
public void OnClick(View v){
buttonClicked(v);
}
}
);

return view;
}

public void buttonClicked(View v)
{
ActivityCommander.createMeme(topPanel.getText().toString(),BottomPanel.getText().toString());
}
}


Post a Reply

Replies

Oldest  Newest  Rating
0 Rajani Krishna Turlapati · August 28, 2015
The above error occured because the method public void OnClick(View v) is manually written. I corrected it by pressing Alt+ins, implementing the onclick method and later writing the code. The error is corrected now.
  • 1

Java / Android Development

107,003 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator