我是新来的,请多多包涵。
我正在尝试通过片段的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)'
也许我缺少一些简单的东西,但是在这方面我没有取得任何进展。
答案 0 :(得分:0)
Google建议使用接口在片段之间进行通信,请参见文档:https://developer.android.com/training/basics/fragments/communicating
此外,我认为this是一个很好的教程,使用活动的共享ViewModel在片段之间传输数据。
答案 1 :(得分:-1)
第一个问题:
我在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
的原因是,如果不实例化对象就无法访问变量或类的方法。除非您将其设为静态变量或静态方法。因此,您需要像这样将它们设为静态:
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();
尝试在null上调用虚拟方法'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并将其绑定到该片段。您需要通过参数设置文本,或者等到片段完全创建为止。