[英]Set text for textview that is in a fragment from another fragments onClick event
我是新来的,请多多包涵。
我正在尝试通过片段的onClick方法在textview中设置文本,但我不断收到此错误:
无法从静态上下文引用非静态字段“ textView”
我尝试过的所有方法都无效,因此我需要帮助。
我有这个片段,其中有一个按钮,我想设置textView的文本:
PullFragment.java
public class PullFragment extends Fragment implements View.OnClickListener {
public PullFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_pull, container, false);
//The button
ImageButton toolbarButtonPull;
toolbarButtonPull = view.findViewById(R.id.toolbarButtonPull);
toolbarButtonPull.setOnClickListener(this);
return view;
}
@Override
public void onClick (View v) {
switch (v.getId()) {
case R.id.toolbarButtonPull:
// Here is the button onClick event
break;
}
}
}
这是另一个具有textView的片段:
PlaceholderFragment.java
public class PlaceholderFragment extends Fragment {
public TextView textView;
public PlaceholderFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_pull, container, false);
// Here is the textView I want to change
textView = rootView.findViewById(R.id.textView);
return rootView;
}
}
我在onClick事件中尝试了以下方法:
PlaceholderFragment.textView.setText("Test");
//Or trying to call public void Test which just does "textView.setText("Text");"
PlaceholderFragment.Test();
无法从静态上下文引用非静态字段“ textView”
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.textView.setText("Text");
//Or trying to call public void Test which just does "textView.setText("Text");"
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.Test();
尝试在空对象引用上调用虚拟方法'void android.widget.EditText.setText(java.lang.CharSequence)'
也许我缺少一些简单的东西,但是在这方面我没有取得任何进展。
Google建议使用接口在片段之间进行通信,请参阅文档: https : //developer.android.com/training/basics/fragments/communication
另外,我认为这是一个很好的教程,使用活动的共享ViewModel在片段之间传输数据。
第一个问题 :
我在onClick事件中尝试了以下方法:
PlaceholderFragment.textView.setText("Test"); //Or trying to call public void Test which just does "textView.setText("Text");" PlaceholderFragment.Test();
无法从静态上下文引用非静态字段“ textView”
Non-static field 'textView' cannot be referenced from a static context
错误Non-static field 'textView' cannot be referenced from a static context
因为您必须在不实例化对象的情况下才能访问变量或类的方法。 除非您将其设为静态变量或静态方法。 因此,您需要像这样将它们设为静态:
public class PlaceholderFragment extends Fragment {
public static TextView textView;
public PlaceholderFragment() {}
...
public static void test() {
...
}
}
第二个问题 :
PlaceholderFragment placeholderFragment = new PlaceholderFragment(); placeholderFragment.textView.setText("Text"); //Or trying to call public void Test which just does "textView.setText("Text");" PlaceholderFragment placeholderFragment = new PlaceholderFragment(); placeholderFragment.Test();
尝试在空对象引用上调用虚拟方法'void android.widget.EditText.setText(java.lang.CharSequence)'
错误: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
。 这是因为在您拨打以下电话时:
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.textView.setText("Text");
placeholderFragment
尚未完全实例化。 这是因为Fragment创建不是一个同步过程。 因此,当您调用placeholderFragment.textView.setText("Text");
尚未创建textView并将其绑定到片段。 您需要通过参数设置文本,或者等待直到片段完全创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.