简体   繁体   中英

Android - The method “…” is undefined for the type Fragment

I created a method called "changeText" inside a fragment class, that would change two TextViews of that fragment, right after the fragment creation. The problem is that I'am not able to call the method "changeText" from my activity, because I get the error message "The method changeText(String[]) is undefined for the type Fragment", like it doesn't exist.

Where am I doing wrong?

This is my fragment class:

package com.example.quizone_2;

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.TextView;

public class fragment_corretto extends Fragment {

TextView questionTv, answerTv;

public fragment_corretto() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_corretto, container,
            false);

    questionTv = (TextView)rootView.findViewById(R.id.question);
    answerTv = (TextView)rootView.findViewById(R.id.answer);

    return rootView;
}

public void changeText(String[] text){
    questionTv.setText(text[1]);
    answerTv.setText(text[0]);
}

}

And this is the method in my activity file, where I call the changeText method:

public void answerTrue(View view){

    if(info[2] == "1"){

        // Create new fragment and transaction
        Fragment newFragment = new fragment_corretto();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction.replace(R.id.container, newFragment);

        // Commit the transaction
        transaction.commit();

        newFragment.changeText(info);

    }else{

        // Create new fragment and transaction
        Fragment newFragment = new Fragment_sbagliato();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction.replace(R.id.container, newFragment);

        // Commit the transaction
        transaction.commit();

                    newFragment.changeText(info);

    }

}

Thank you very much in advance.

In your Activity , newFragment is declared as a Fragment . A simple Fragment does not have the changeText method, or any other method of your subclass for that matter.

The solution is to declare newFragment as a fragment_corretto :

fragment_corretto newFragment = new fragment_corretto();

PS. You should follow the convention of using pascal case for naming classes. In other words, don't call it fragment_corretto , but rather FragmentCorretto

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM