Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我应该在片段中的何处定义TextView以多次更改其内容?_Java_Android_Android Layout_Android Fragments_Android Studio - Fatal编程技术网

Java 我应该在片段中的何处定义TextView以多次更改其内容?

Java 我应该在片段中的何处定义TextView以多次更改其内容?,java,android,android-layout,android-fragments,android-studio,Java,Android,Android Layout,Android Fragments,Android Studio,因此,我有一个片段,其中包含多个文本视图,当用户单击“下一步”按钮或“上一步”按钮时,这些文本视图会发生更改 在onCreateView方法中,我有: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.infla

因此,我有一个片段,其中包含多个文本视图,当用户单击“下一步”按钮或“上一步”按钮时,这些文本视图会发生更改

在onCreateView方法中,我有:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View inflatedView = inflater.inflate(R.layout.fragment_time_table, container, false);


    update(inflatedView,worker.current.getTarih(),worker.current.getHicri(),worker.current.getGün(),worker.current.getImsak(),worker.current.getSabah(),worker.current.getGüneş(),worker.current.getÖğle(),worker.current.getIkindi(),worker.current.getAkşam(),worker.current.getYatsı());


    // Inflate the layout for this fragment
    return inflatedView;


}
关于我的更新方法

public void update(View dimension,String tarih, String hicri, String gun, String imsak, String sabah, String gunes, String ogle, String ikindi, String aksam, String yatsi){

    TextView gunView = (TextView) dimension.findViewById(R.id.gun);
    TextView imsakView = (TextView) dimension.findViewById(R.id.imsak);
    TextView sabahView = (TextView) dimension.findViewById(R.id.sabah);
    TextView gunesView = (TextView) dimension.findViewById(R.id.gunes);
    TextView ogleView = (TextView) dimension.findViewById(R.id.ogle);
    TextView ikindiView = (TextView) dimension.findViewById(R.id.ikindi);
    TextView aksamView = (TextView) dimension.findViewById(R.id.aksam);
    TextView yatsiView = (TextView) dimension.findViewById(R.id.yatsi);


    gunView.setText(gun);
    imsakView.setText(imsak);
    sabahView.setText(sabah);
    gunesView.setText(gunes);
    ogleView.setText(ogle);
    ikindiView.setText(ikindi);
    aksamView.setText(aksam);
    yatsiView.setText(yatsi);
}

在用户单击时调用update方法。每次调用
update()
时,我都会重新定义textview我尝试在onCreate方法上定义TextView,但我一直得到NPE。

您必须这样更改

通过这种方式,您的文本视图不会每次都被重新定义

更改:

public class ViewFragment extends Fragment {

    public TextView gunView, imsakView, sabahView, gunesView, ogleView, ikindiView, aksamView, yatsiView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View inflatedView = inflater.inflate(R.layout.content_main, container, false);
        gunView = (TextView) inflatedView.findViewById(R.id.gun);
        imsakView = (TextView) inflatedView.findViewById(R.id.imsak);
        sabahView = (TextView) inflatedView.findViewById(R.id.sabah);
        gunesView = (TextView) inflatedView.findViewById(R.id.gunes);
        ogleView = (TextView) inflatedView.findViewById(R.id.ogle);
        ikindiView = (TextView) inflatedView.findViewById(R.id.ikindi);
        aksamView = (TextView) inflatedView.findViewById(R.id.aksam);
        yatsiView = (TextView) inflatedView.findViewById(R.id.yatsi);
        update("1", "2", "3", "4", "5", "5", "6", "7", "8", "9");  //remove view from here
        // Inflate the layout for this fragment
        return inflatedView;
    }

    public void update(String tarih, String hicri, String gun, String imsak, String sabah, String gunes, String ogle, String ikindi, String aksam, String yatsi) {
        gunView.setText(gun);
        imsakView.setText(imsak);
        sabahView.setText(sabah);
        gunesView.setText(gunes);
        ogleView.setText(ogle);
        ikindiView.setText(ikindi);
        aksamView.setText(aksam);
        yatsiView.setText(yatsi);
    }
}

这是我的想法,希望它能起作用,我自己还没试过。如果您想定义TextView,而不是一次又一次地重新定义它,为什么不在使用gradle选项编译(编译'com.jakewharton:ButterKnife:7.0.1')之后,在片段类内部使用ButterKnife进行定义呢


我打赌
worker
的某些部分为空。。。如果没有
onCreateView
onActivityCreated
中的logcat思想,很难判断(您需要在
onCreateView
中返回一个
View
实例变量,因为您需要在
onCreateView
之外访问它)您应该将
worker.current
作为一个参数传递给
update
方法。拆分一个对象的所有字段没有意义哦,对了,改变了,这是如何防止NullPointerException的?您所做的只是将update方法中的一些行移到onCreateViews中。我以前尝试过,但是更新方法出现了错误。我收到“无法解析符号…”请检查我的更新答案。我没有收到任何错误。@BillyBatson当然,您的代码会编译,但问题是为什么会有NPE。我猜FindViewByDhere中的某些id丢失或不正确,主要问题现在已经解决了@BillyBatson不需要每次都定义textviews。请尽量尊重原始代码的意图。您可以在评论中建议使用库
    public class MyFragment extends Fragment {

        @Bind(R.id.textView)TextView tv;
        @Bind(R.id.textView)TextView tv2, tv3 etc;
    ...
    ...
        @Override
        OnActivityCreated(Bundle savedInstance){
          ButterKnife.Bind(getActivity());
   }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

          tv.setText(String.format(Locale.US, "ABCD %d", i));
    }