[英]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. 我在片段类内部创建了一个名为“ changeText”的方法,该方法将在创建片段后立即更改该片段的两个TextView。 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.
问题是我无法从我的活动中调用方法“ changeText”,因为我收到错误消息“对于类型Fragment,未定义方法changeText(String [])”,就像它不存在一样。
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: 这是我的活动文件中的方法,在这里我将其称为changeText方法:
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
. 在您的
Activity
, newFragment
被声明为Fragment
。 A simple Fragment
does not have the changeText
method, or any other method of your subclass for that matter. 一个简单的
Fragment
没有changeText
方法或子类的其他任何方法。
The solution is to declare newFragment
as a fragment_corretto
: 解决方案是将
newFragment
声明为fragment_corretto
:
fragment_corretto newFragment = new fragment_corretto();
PS. PS。 You should follow the convention of using pascal case for naming classes.
您应遵循使用pascal大小写命名类的约定。 In other words, don't call it
fragment_corretto
, but rather FragmentCorretto
换句话说,不要将其命名为
fragment_corretto
,而是将其命名为FragmentCorretto
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.