我正在尝试一些 ActionBarSherlock 片段标签。我目前在我的代码中遇到了一些问题。我在片段中使用了单独的 Tablisteners。如果可能,请更正我的代码。
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class FragmentsTab1 extends SherlockFragmentActivity implements
ActionBar.TabListener{
private Fragment mFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment1);
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
mFragment = new FragmentsTab1(); //Type mismatch: cannot convert from FragmentsTab1 to Fragment
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
错误代码:类型不匹配:无法从 FragmentsTab1 转换为 Fragment
迪克西特·帕特尔的建议
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentTransaction;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class FragmentsTab1 extends SherlockFragmentActivity implements
ActionBar.TabListener{
private Fragment mFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment1);
}
/**
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
mFragment = new FragmentsTab1();
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
**/
public void onTabSelected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
mFragment = new FragmentsTab1(); //Type mismatch: cannot convert from FragmentsTab1 to Fragment
ft.add(android.R.id.content, mFragment); //The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, Fragment)
ft.attach(mFragment); //The method attach(Fragment) in the type FragmentTransaction is not applicable for the arguments (Fragment)
}
public void onTabUnselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(mFragment); //The method remove(Fragment) in the type FragmentTransaction is not applicable for the arguments (Fragment)
}
public void onTabReselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}