简体   繁体   中英

Need more clarification on how to set text to a TextView inside Fragment

Iam having the same problem as in this question

Change TextView inside Fragment

and the solution written there works fine Here is the solution

  import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    public class DetailFragment extends Fragment {

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


        public void setText(String text){
            TextView textView = (TextView) getView().findViewById(R.id.detailsText);
            textView.setText(text);
        }

    }

My first Question -> WHY My application crashes whenever i try to set text in the TextView which is located inside a fragment in this way

Inside a method of class Main-Activity

TextView  tv = (TextView)getView().findViewById(R.id.textView_question); 
   tv.setText(question);

Second Question -> in the solution in method setText why cant i write this

TextView textView = (TextView) getView().findViewById(R.id.detailsText);
            textView.setText(text);

To

   TextView textView = (TextView)findViewById(R.id.detailsText);
            textView.setText(text);

Log Cat when i try to set text (Only Error)

  07-08 18:40:33.462: E/AndroidRuntime(27881): FATAL EXCEPTION: main
07-08 18:40:33.462: E/AndroidRuntime(27881): java.lang.IllegalStateException: Could not execute method of the activity
07-08 18:40:33.462: E/AndroidRuntime(27881):    at android.view.View$1.onClick(View.java:3071)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at android.view.View.performClick(View.java:3538)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at android.view.View$PerformClick.run(View.java:14330)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at android.os.Handler.handleCallback(Handler.java:608)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at android.os.Looper.loop(Looper.java:156)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at android.app.ActivityThread.main(ActivityThread.java:4987)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at dalvik.system.NativeStart.main(Native Method)
07-08 18:40:33.462: E/AndroidRuntime(27881): Caused by: java.lang.reflect.InvocationTargetException
07-08 18:40:33.462: E/AndroidRuntime(27881):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 18:40:33.462: E/AndroidRuntime(27881):    at android.view.View$1.onClick(View.java:3066)
07-08 18:40:33.462: E/AndroidRuntime(27881):    ... 11 more
07-08 18:40:33.462: E/AndroidRuntime(27881): Caused by: java.lang.NullPointerException
07-08 18:40:33.462: E/AndroidRuntime(27881):    at com.alisaeed.newramadan.MainActivity.question_displayer(MainActivity.java:64)
07-08 18:40:33.462: E/AndroidRuntime(27881):    ... 14 more

New code

public void  question_displayer(View v){


    Log.i("create","question_displayer()");
    RamadanData data_object = new RamadanData();


    FragmentTransaction fragment_question = getSupportFragmentManager().beginTransaction();
    fragment_question.replace(R.id.framelayout1, new fragment_question_displayer()  , null);
    fragment_question.addToBackStack(null);
    fragment_question.commit();

     TextView tv = (TextView)findViewById(R.id.textView_question);



    switch(v.getId()){
    case (R.id.buttonPrayer):
    {


        Log.i("create","prayer()");

        if(child_selected == true && adult_selected==false && teen_selected==false )
        {
            Log.i("create","child_selected");

           String question= data_object.question_output(flag_autoquestion, "child","prayer");      
           Log.i("create",question);

           tv.setText(question);

           flag_autoquestion++;
                                         }       




    }



}


}

In fragment you will call a text view like this

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.details, container, false);
        TextView textView = (TextView) view.findViewById(R.id.detailsText);
        String text = "sample text";
        textView.setText(text);
        return view;
    }

to set your text. On the other hand, in activity class it will be like this

setContentView(R.activity_layout);
TextView  tv = (TextView) findViewById(R.id.textView_question); 
tv.setText(question);

Add in class description:

TextView textView;

Define TextView in onCreateView(...)

View view = inflater.inflate(R.layout.details, container, false);

TextView textView = (TextView) view.findViewById(R.id.detailsText);

After you can use textView from any place of this class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM