Reputation: 2027
I'm a noob in Android programming and I've experienced an error while working with fragments, it says I can't refer to a non-static method(beginTransaction) from a static context. Here's the code:
public class User extends ActionBarActivity implements AdapterView.OnItemClickListener{
private ActionBarDrawerToggle actionBarDrawerToggle;
private DrawerLayout drawerLayout;
private ListView navList;
private android.support.v4.app.FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
//DrawerLayout (za xml)
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout);
ListView navList = (ListView)findViewById(R.id.navlist);
ArrayList<String> navArray = new ArrayList<String>();
navArray.add("Latest");
navArray.add("Popular conversations");
navArray.add("New Message");
navArray.add("Settings");
navArray.add("Log out");
navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1,navArray);
navList.setAdapter(adapter);
navList.setOnItemClickListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrawer,R.string.closedrawer);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
// delete phps?
loadSelection(0);
}
// *** Yes, I'm aware of the errors down below..
@Override
private void loadSelection(int i){
navList.setItemChecked(i, true);
switch(i){
case 0:
homefragment homefragment = new homefragment();
fragmentTransaction = FragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentholder, homefragment);
fragmentTransaction.commit();
break;
//ostali slucajevi l8er..
case 3:
NewMsg NewMsg = new NewMsg();
fragmentTransaction = FragmentManager.beginTransaction(); // <----- ??????????
fragmentTransaction.replace(R.id.fragmentholder,NewMsg);
fragmentTransaction.commit();
break;
}
}
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(Bundle savedInstanceState);
actionBarDrawerToggle.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_user, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} else if (id == android.R.id.home) {
if (drawerLayout.isDrawerOpen(navList)) {
drawerLayout.closeDrawer(navList);
} else {
drawerLayout.openDrawer(navList);
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
loadSelection(position);
drawerLayout.closeDrawer(navList);
}
}
Edit: Link removed, code added.
Upvotes: 1
Views: 2621
Reputation: 2201
This is actually not related to fragments, but is more general. The problem with FragmentManager.beginTransaction()
is that FragmentManager
is a static reference to a class, when you should instead be calling the method an object of the class. The solution is to use
getSupportFragmentManager().beginTransaction();
Alternatively you can add
android.support.v4.app.FragmentManager fragmentManager;
to the variables of your class. Then you can do in your code in the onCreate
method where you have
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
use only
fragmentManager = getSupportFragmentManager();
which works since you have declared that your class has the variable fragmentManager
. Then in your code where the errors arise, replace the capitalized FragmentManager
with just fragmentManager
.
Edit: made a mistake when reading your code, the latter method works now too.
Upvotes: 3