我试图将Fragment实例化到我的FragmenteActivity类,但Eclipse不允许我!
让我告诉你:
那是我的片段类:
public class VendorListFragment extends Fragment {
private VendorListAdapter adapter;
private ArrayList<Vendor> vendorList;
private VendorSQLiteHelper dbHelper;
private ListView listView;
public VendorListFragment() {
// empty constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_vendor_list, container, false);
listView = (ListView) rootView.findViewById(R.id.listView);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
this.setRetainInstance(true);
return rootView;
}
所以你可以看到它扩展了Fragment,对吗?但是在我的FragmentActivity类中,如上所示,当我编码
时fragmentTransaction.add(R.id.view_trade_show_vendor_fragment, new VendorListFragment());
Eclipse说:
FragmentTransaction类型中的方法add(int,Fragment)不是 适用于参数(int,VendorListFragment)
所以,Eclipse并不认为我的ViewTradeShowActivity是一个片段,即使使用Extends Fragment,还有什么问题?!
请参阅以下更多代码:
public class ViewTradeShowActivity extends FragmentActivity {
TextView viewTradeShowName;
TradeShow tradeShow;
Fragment vendorFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_trade_show);
tradeShow = getIntent().getParcelableExtra("tradeShow");
viewTradeShowName = (TextView) findViewById(R.id.view_trade_show_name);
viewTradeShowName.setText(tradeShow.getName());
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.view_trade_show_vendor_fragment, new VendorListFragment());
}
}
谢谢!
答案 0 :(得分:2)
您确定要有正确的导入吗?
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;