(TextView)setText()方法返回空白,不起作用

时间:2015-12-20 14:33:50

标签: java android xml textview

我正在开发基于Navigation Drawer的Android应用程序。 NavMainActivity处理片段。 EventFragmentHomeFragmentProfileFragment

我使用 Parse SDK 进行后台作业。 Parse做得很好。 但是在EventFragment课程中,TextView的{​​{1}}方法无法执行任何操作。我检查了这个问题并使用setText()Snackbar来测试我的后台服务运行良好。

Toast

public class EventFragment extends Fragment implements View.OnClickListener {

    /*
    Initiliazing the params and our view.
     */
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    View view;

    boolean isPressed = false;


    private String mParam1;
    private String mParam2;
    public String eventDescription;
    public ParseFile userImage;

    private OnFragmentInteractionListener mListener;

    public EventFragment() {
        // Required empty public constructor
    }


    public static EventFragment newInstance(String param1, String param2) {
        EventFragment fragment = new EventFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




    }


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

        /*
        FloatingAB used for material design guidelines.
        We listen for clicks but in this version user can click many times we need to
        implement opposite button reaction like " Participate " and " Not Participate " state changes.
         */

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

        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
        fab.setOnClickListener(this);

        ParseQuery<ParseObject> query = ParseQuery.getQuery("EventData");
        query.whereEqualTo("objectId", "uEglVemWEF");
        query.getFirstInBackground(new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                if (object == null) {
                    Log.d("EventData", "The get the first method request failed!");

                } else {

                    eventDescription = object.getString("eventContext");
                    userImage = object.getParseFile("eventPhoto");

                    Snackbar.make(view, eventDescription, Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();

                }
            }
        });

        TextView z = (TextView)view.findViewById(R.id.eventDescriptionText);
        z.setText(eventDescription);

        if (ParseUser.getCurrentUser().getBoolean("eventA")) {
            fab.setImageResource(R.drawable.ic_highlight_remove_24dp);
            isPressed =true;
        }

        return view;

    }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您尝试异步(在另一个线程上)获取eventDescription,然后使用主线程中的值。到目前为止它肯定还是空的。尝试在done()

中的query.getFirstInBackground回调中设置TextView