0
public class MainActivity extends AppCompatActivity implements
    TopSectionFragment.TopSectionListener {

When I try to implement the TopSectionFragment the writing goes red and, then it says Cannot resolve symbol when my mouse goes over it.

This is all happening in Android Studio.

My MainActivity.java looks like this:

package com.example.danielhunter.fragments;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;`


public class MainActivity extends AppCompatActivity
    implements TopSectionFragment.TopSectionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

My TopSectionFragment.java looks liks this:

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

import com.example.danielhunter.fragments.R;


public class TopSectionFragment extends Fragment {

private static EditText topTextInput;
private static EditText bottomTextInput;

TopSectionListener activityCommander;

    public interface TopSectionListener{
    public void createMeme(String top,String bottom);


    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try{
        activityCommander = (TopSectionListener) activity;

        }catch (ClassCastException e){
            throw new ClassCastException(activity.toString());
        }



    }

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

        View view = inflater.inflate(R.layout.top_section_fragment, container, false);

        topTextInput = (EditText) view.findViewById(R.id.topTextInput);
        bottomTextInput = (EditText) view.findViewById(R.id.bottomTextInput);

        final Button button = (Button) view.findViewById(R.id.button);


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


                    }
                }
        );



        return view;



    }
    // calls this when the button is clicked
    public void buttonClicked(View view){
    activityCommander.createMeme(topTextInput.getText().toString(), bottomTextInput.getText().toString());

    }

    }
pp_
  • 3,435
  • 4
  • 19
  • 27
Dan Hunter
  • 1
  • 1
  • 1

1 Answers1

0

In order for a class to correctly implement an interface, it must include implementations of ALL the methods defined in the interface. In your case, MainActivity would need to have a method with the signature void createMeme(String top,String bottom) in order for this to compile.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • How would that look on the MainActivity? – Dan Hunter Feb 13 '16 at 02:55
  • If you follow the quickfix suggestion in Android Studio, it will actually create the method for you. It would look like this `public void createMeme(String top,String bottom) {}`, or essentially, just like the interface definition, but with a method body. – Doug Stevenson Feb 13 '16 at 03:06