Reputation: 4427
Following this guide I'm trying to setup a fragment but I have an error here:
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, myfrag).commit();
Can't resolve method add(int, com.mypackage.MyFragment)
I have a FrameLayout with a fragment_container id and MyFragment Extends Fragment... So I'm not sure what I'm doing wrong here... First time using fragments...
Here is my code
Main Activity:
public class MainFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_fragmentactivity);
int frag = getIntent().getExtras().getInt("fragment");
if(findViewById(R.id.fragment_container) != null) {
if(savedInstanceState != null) return;
MyFragment myfrag= new MyFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, myfrag).commit();
...
MyFragment:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.settings_devicelist, container, false);
}
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
listView = (ListView)getView().findViewById(R.id.lvHosts);
...
settings_framentactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Upvotes: 1
Views: 729
Reputation: 4427
Not sure if my code in my question is a bug, but written out longhand everything is fine.
MyFragment frag = new MyFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.commit();
Upvotes: 0
Reputation: 2411
Make sure you're importing android.support.v4.app.Fragment
class and not android.app.Fragment
Upvotes: 2