简体   繁体   中英

cannot resolve symbol “setText” SherlockActionBar fragment

I have a child fragment in my application that im trying to test functionality in. The idea is that the textview will update to mimic whatever is entered into the edittext. However currently it doesn't like the setText and i have no idea why. I've checked to see whether i had missed an import and i think the widgets are defined correctly. Any advice on how to solve this problem would be great.

import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class WeightFragment extends SherlockFragment {

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

EditText weight = (EditText) getActivity().findViewById(R.id.weightInput);
TextView label = (TextView) getActivity().findViewById(R.id.weightResult);

String result = weight.getText().toString();
label.setText(result);

}

Put the code INSIDE a method, like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_weight, container, false);

    EditText weight = (EditText) getActivity().findViewById(R.id.weightInput);
    TextView label = (TextView) getActivity().findViewById(R.id.weightResult);

    String result = weight.getText().toString();
    label.setText(result);
    return rootView;
}

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