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

时间:2015-01-21 12:19:45

标签: android android-fragments

我的片段中的视图根本没有响应,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 func:

@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;
}
}

3 个答案:

答案 0 :(得分:1)

更改此

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

 return view;

因为您已经在view对象中夸大了视图,因此您需要返回view

答案 1 :(得分:1)

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

是问题

return view;

答案 2 :(得分:0)

这是因为您从return inflater.inflate( R.layout.fragment_one, container, false);返回onCreateView() - 您重新初始化布局,并且最终没有您先前初始化的内容。 请改用return view;