سؤال

I am attempting to convert my app to fragments vs activities in order to better follow the android design guidlines with a navigation drawer.

I have a fragment with a button, that when pressed executes this code to launch a new fragment:

FragmentManager man=getFragmentManager();
        FragmentTransaction tran=man.beginTransaction();
        Fragment_one=new Fragment1();
        tran.add(R.id.main, Fragment_one);//tran.
        tran.addToBackStack(null);
        tran.commit();

the fragment it attempts to load is this:

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);

    }



}

I am getting an error on this line in the above code:

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

cannot resolve method findviewbyid is the error

هل كانت مفيدة؟

المحلول

Try this, you need to get root view and use findViewById by using root view.

@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;

}

نصائح أخرى

Change onCreateView() to this-

@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;

    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top