gpt4 book ai didi

java - Eclipse 无法将我的类(class)识别为 fragment

转载 作者:行者123 更新时间:2023-11-29 21:12:21 25 4
gpt4 key购买 nike

我正在尝试为我的 FragmenteActivity 类实例化一个 fragment ,但 Eclipse 不允许我这样做!

让我告诉你:

那是我的 fragment 类:

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 说:

The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, VendorListFragment)

所以,Eclipse 不认为我的 ViewTradeShowActivity 是 fragment ,即使有扩展 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());

}
}

谢谢!

最佳答案

您确定导入正确吗?

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

关于java - Eclipse 无法将我的类(class)识别为 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22416461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com