Android 如何在片段中设置文本?

Android 如何在片段中设置文本?,android,android-fragments,android-xml,Android,Android Fragments,Android Xml,我正在尝试将我的应用程序转换为片段与活动,以便更好地遵循带有导航抽屉的android设计指南 我有一个带有按钮的片段,按下按钮时执行此代码以启动新片段: FragmentManager man=getFragmentManager(); FragmentTransaction tran=man.beginTransaction(); Fragment_one=new Fragment1(); tran.add(R.id.main, Fragment

我正在尝试将我的应用程序转换为片段与活动,以便更好地遵循带有导航抽屉的android设计指南

我有一个带有按钮的片段,按下按钮时执行此代码以启动新片段:

FragmentManager man=getFragmentManager();
        FragmentTransaction tran=man.beginTransaction();
        Fragment_one=new Fragment1();
        tran.add(R.id.main, Fragment_one);//tran.
        tran.addToBackStack(null);
        tran.commit();
它试图加载的片段如下所示:

public class BPTopBeers extends Fragment {

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

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);

        String title = "Top Beers on Beer Portfolio";
        TextView topTitle = (TextView) findViewById(R.id.topTasteTitle);
        topTitle.setText(title);



        //construct url
        String url = "myURL";

        Log.d("myUrl", url);

        //async task goes here
        new GetYourTopTasteBeers(this).execute(url);

        // Inflate the layout for this fragment
        //todo: change to discover layout
        return inflater.inflate(R.layout.toptaste_layout, container, false);

    }



}
我在上面代码的这一行上得到一个错误:

TextView topTitle = (TextView) findViewById(R.id.topTasteTitle);

无法解析方法findviewbyid是错误

尝试此操作,您需要获取根视图并使用根视图使用findviewbyid

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

    View v = inflater.inflate(R.layout.toptaste_layout, container, false);

    // rest of the code
    TextView topTitle = (TextView) v.findViewById(R.id.topTasteTitle);

    return v;

}

试试这个,您需要通过使用根视图获取根视图并使用findViewById

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

    View v = inflater.inflate(R.layout.toptaste_layout, container, false);

    // rest of the code
    TextView topTitle = (TextView) v.findViewById(R.id.topTasteTitle);

    return v;

}

试试这个,您需要通过使用根视图获取根视图并使用findViewById

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

    View v = inflater.inflate(R.layout.toptaste_layout, container, false);

    // rest of the code
    TextView topTitle = (TextView) v.findViewById(R.id.topTasteTitle);

    return v;

}

试试这个,您需要通过使用根视图获取根视图并使用findViewById

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

    View v = inflater.inflate(R.layout.toptaste_layout, container, false);

    // rest of the code
    TextView topTitle = (TextView) v.findViewById(R.id.topTasteTitle);

    return v;

}
将onCreateView()更改为-

将onCreateView()更改为-

将onCreateView()更改为-

将onCreateView()更改为-


这个
应该是
getActivity()
这个应该是
getActivity()
这个应该是
getActivity()
这个应该是
getActivity()