片段中的按钮无法通过应用程序点击/片段中的视图没有响应

Button in fragment is unclickable via the app / Views in fragment aren't responding

我的片段中的视图根本没有响应,textview.setText(str) 不起作用,当我尝试通过应用程序单击按钮时,除了使用 "view.performClick()" 正在调用 onClick 函数。

public class FragmentOne extends Fragment implements OnClickListener{

private TextView tvA;
private Button btnSync;
public String levelscore;

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

  levelscore = getArguments().getString("scoreKey");
  Toast.makeText(view.getContext(), levelscore, 1000).show(); //Outputs score
  tvA = (TextView)view.findViewById(R.id.tvTotalScore);
  Toast.makeText(view.getContext(), "test1", 1000).show(); //Outputs 'test1'
  btnSync = (Button)view.findViewById(R.id.sync);
  Toast.makeText(view.getContext(), btnSync.getText().toString() , 1000).show(); //Outputs 'SyncScore'
  btnSync.setOnClickListener(this);
  Toast.makeText(getActivity(), "BtnClickable: "+btnSync.isClickable(), 2500).show(); //Outputs 'BtnClickable: true'
  btnSync.performClick(); //Calls the onClick function
  tvA.setText("Test");  //Does nothing

  if(levelscore != null){
      tvA.setText(levelscore); //Does nothing
  }else{
      tvA.setText("Error!");  //Does nothing
  }


   //Inflate the layout for this fragment



  return inflater.inflate(
          R.layout.fragment_one, container, false);
  }

onClick 函数:

@Override
public void onClick(View v) {
Toast.makeText(getActivity(), levelscore, 1000).show(); //Outputs score (via performClick)
Log.d("SCORE", levelscore); //Shows the score in LogCat (via performClick)
switch (v.getId()) {
case R.id.sync:
    Toast.makeText(getActivity(), levelscore, 1000).show(); //Outputs score (via performClick)
    tvA.setText(levelscore); //Does nothing (even via performClick)
    break;

default:
    break;
}
}

改变这个

 return inflater.inflate(
      R.layout.fragment_one, container, false);

 return view;

因为您已经在 view 对象中扩充了一个视图,所以您需要 return view.

return inflater.inflate(
      R.layout.fragment_one, container, false);`

问题所在

return view;

这是因为您从 onCreateView() return return inflater.inflate( R.layout.fragment_one, container, false); - 您重新初始化了布局,但它最终没有您之前初始化的任何内容。 使用 return view; 代替