从Fragment开始Acitvity

时间:2015-04-24 03:36:26

标签: java android android-fragments android-activity android-listview

我的MainActivity(FragmentActivity) ViewPagerFragmentPagerAdapter OrdersFragment创建了ListView OnItemClickListenerOrdersFragment现在我想开始新的活动public class OrdersFragment extends Fragment { ListView lv; private ArrayList<Order> d = new ArrayList<Order>(); private ordersAdapter adapter; public OrdersFragment (){ } public OrdersFragment setArguments(ArrayList<Order> orders){ if(adapter == null) return this; adapter.setData(orders); Log.d("fragment", "zmiana"); adapter.notifyDataSetChanged(); return this; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //d.add(new Order()); Log.d("fragment", "orders fragment created" + d.toString()); lv = (ListView) inflater.inflate(R.layout.orders_list, container, false); lv.setOnItemClickListener(itemClicked); adapter = new ordersAdapter(inflater, d); lv.setAdapter(adapter); return lv; } public ArrayList<Order> getOrders(){ return d; } OnItemClickListener itemClicked = new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString()); startActivity(new Intent(OrdersFragment.this, OrderActivity.class)); } }; } ,但我无法弄清楚如何做到这一点。

这是Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.类:

android.support.v4.app.Fragment.startActivity

它说Intent构造函数是未定义的,当我移动到startActivity时它会显示import android.app.Activity; import android.content.Intent; 并且它声明这个函数在1 - You need to get the related coordinate for each element on the swf file 2 - Click on the coordinate you get in step 1st 3 - Check current link 中定义但是我实际导入了这个:

You could use
1 - FlashSelenium
2 - Sikuli
3 - FlexMonkey
4 - FlexWebdriver class

我做错了什么?

3 个答案:

答案 0 :(得分:0)

Fragment中,我们必须将上下文作为getActivity()传递,请使用以下代码替换您的代码。

OnItemClickListener itemClicked = new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
       startActivity(new Intent(getActivity(), OrderActivity.class));
   }
};

答案 1 :(得分:0)

将此传递上下文设为getActivity()intent

 OnItemClickListener itemClicked = new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
            startActivity(new Intent(getActivity(), OrderActivity.class));
        }
    };

答案 2 :(得分:0)

OnItemClickListener itemClicked = new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        if(getActivity() != null) {
            Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
            startActivity(new Intent(getActivity(), OrderActivity.class));
        }
    }
};

getActivity()将为您提供可用于启动新Activity的Activity上下文。在极少数情况下,getActivity()可以返回null,因此检查getActivity()不为null可能不会对您造成伤害。