我是Android编程中的菜鸟,我在使用片段时遇到错误,它说我不能从静态上下文引用非静态方法(beginTransaction)。这是代码:
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);
}
}
编辑:已删除链接,已添加代码。
答案 0 :(得分:3)
这实际上与片段无关,但更为通用。 FragmentManager.beginTransaction()
的问题是FragmentManager
是对类的静态引用,而您应该将该方法调用为该类的对象。解决方案是使用
getSupportFragmentManager().beginTransaction();
或者你可以添加
android.support.v4.app.FragmentManager fragmentManager;
到你班级的变量。然后,您可以使用
中的onCreate
方法在代码中执行此操作
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
仅使用
fragmentManager = getSupportFragmentManager();
,因为您声明您的类具有变量fragmentManager
,所以它有效。然后在出现错误的代码中,使用FragmentManager
替换大写的fragmentManager
。
编辑:在阅读代码时犯了一个错误,后一种方法现在也有效。