【问题标题】:How to set text in a fragment?如何在片段中设置文本?
【发布时间】:2014-02-08 18:01:10
【问题描述】:

我正在尝试将我的应用程序转换为片段与活动,以便更好地遵循带有导航抽屉的 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);

cannot resolve method findviewbyid 是错误

【问题讨论】:

    标签: android android-fragments android-xml


    【解决方案1】:

    试试这个,你需要通过root view获取root view并使用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;
    
    }
    

    【讨论】:

      【解决方案2】:

      将 onCreateView() 改成这个-

      @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
      {
              View view = inflater.inflate(R.layout.toptaste_layout, container, false);
              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) view.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 view;
      
          }
      

      【讨论】:

      • this 应该是getActivity()
      猜你喜欢
      • 2022-01-09
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多