简体   繁体   中英

Android: setText is set correctly but it is empty when i run the program in emulator

i am new to android but i have problem using settext method . i have used settext method but when i load the project in emulator then the textview there is empty . it is blank screen i have no error while loading inside emulator

the code i have written is

package com.coded.fragments;

public class ChildFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_main, container, false);

    return rootView;
}
public void setText(String item) { 
    item = "<p>ABCDEFGHI</p><br><p>JKLMNOPQRSTUVWXYZ</p>";
        TextView textview = (TextView) getView().findViewById(R.id.textView2);
    textview.setText(Html.fromHtml(item));

  }}

should i add any method to it and front end xml file i have written as this

activity_main.xml

<TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="216dp"
                android:padding="10dp"
                android:textAlignment="center"
                android:textColor="@color/white" />

please friends check,let me know and thanks for your valuable time.

if i use this above code when i load it onto emulator it is giving blank screen there is no text displayed which i am passing from item from above code

Try this..

public class ChildFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_main, container, false);
    String item = "<p>ABCDEFGHI</p><br><p>JKLMNOPQRSTUVWXYZ</p>";
    TextView textview = (TextView) rootView.findViewById(R.id.textView2);
    textview.setText(Html.fromHtml(item));

    return rootView;
}

Or

TextView textview;

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

    View rootView = inflater.inflate(R.layout.activity_main, container, false);
    textview = (TextView) rootView.findViewById(R.id.textView2);    
    String item = "<p>ABCDEFGHI</p><br><p>JKLMNOPQRSTUVWXYZ</p>";  
    setText(item);
    return rootView;
}
public void setText(String item) {                   
    textview.setText(Html.fromHtml(item));    
}

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