0

I am making a simple memegenerator app where user should enter text for the bottom and top field and that text will be added on the bottom and top section of the picture. Since I want to keep the app simple I have used only one fixed image for Meme. I have used two fragments. One is for the Edittext areas and other is for the viwtext areas (where the etxt will be attached on the picture). In order to glue them together I have created interface which also gets called in my mainActivity's java file. Now the problem is in the following lines of code in my main java class. Here the method memeGen is the main brain for this app. Inside it I am trying to find one of the fragment by it's id. But it seems thst its not finding that resourse.

   public void memeGen(String top, String bottom) {
        BottomSectionJava bjs=(BottomSectionJava)getSupportFragmentManager().findFragmentById(R.id.fragment2);

    }

Everything else is working fine. Only the above line shows this error: Error

My log file outputs this:

C:\Users\Riyana\AndroidStudioProjects\Fragment\app\src\main\java\com\mycompany\fragment\FragmentActivity.java
Error:(20, 94) error: inconvertible types
required: BottomSectionJava
found:    Fragment
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

This is the complete code for that class where the error is appearing:

    public class FragmentActivity extends ActionBarActivity implements TopSectionjava.TopSectionListener{

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

    @Override
    public void memeGen(String top, String bottom) {
        BottomSectionJava bjs=(BottomSectionJava)getSupportFragmentManager().findFragmentById(R.id.fragment2);

    }


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_, menu);
        return true;
    }
}

BottomSectionJava:

    public class BottomSectionJava extends Fragment{
    private static TextView memetext_top;
    private static TextView memetext_bottom;
 //
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view=inflater.inflate(R.layout.bottom_section,container,false);
        memetext_top=(TextView)view.findViewById(R.id.memetext_top);
        memetext_bottom=(TextView)view.findViewById(R.id.memetext_bottom);
        return view;
    }

    public void setTextOnThePic(String top,String bottom){
        memetext_top.setText(top);
        memetext_bottom.setText(bottom);

    }

}
Riyana
  • 241
  • 13
  • 29

1 Answers1

2

Change extension in BottomSectionJava from android.app.Fragment to android.support.v4.app.Fragment

I think that you have in your imports line below:

import android.app.Fragment;

and you have to have

import android.support.v4.app.Fragment;

Now your fragment extends object Fragment from android.app package and you have to extends Fragment from android.support.v4.app package.

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
  • Thank u very very much. Actually I have two fragments. thats why I had to apply ur same suggestion for both two classes. Now I got rid of the main problem. my application is running. Only problem is nothing happens when I click the button to add text. It doesnt add text in the textView areas on the picture. However I won't interrupt u that much. If u have a time can u have look on my issue why button click isn't working? Otherwise its ok. XX – Riyana Feb 17 '15 at 23:32
  • I'm glad that I could help you. – Konrad Krakowiak Feb 18 '15 at 07:00
  • Where do you call setTextOnThePic(Sting, String) method? There is no any place where you initialise listener. – Konrad Krakowiak Feb 18 '15 at 08:01