选项卡式活动Android

时间:2017-12-05 20:38:46

标签: android android-fragments android-tabbed-activity

我正在开发一个Android应用,我有3个标签:订单状态,订单详情,订单信息。 我的问题是当我尝试将Fragment添加到此应用程序时收到错误消息。 这是我的代码:

orderTabbedFraagment.java

package com.example.kamran.bluewhite;

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

public class OrderInfoTabbed extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_info_tabbed);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);



    }


    @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_order_info_tabbed, 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;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_order_info_tabbed, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    orderInfoFragment a = new orderInfoFragment();
                    return a;
            }
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Order Details";
                case 1:
                    return "Order Info";
                case 2:
                    return "Order Status";

            }
            return null;
        }
    }
}

orderInfoFragment .java

package com.example.kamran.bluewhite;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

/**
 * Created by rahafo on 12/5/2017.
 */


public class orderInfoFragment extends Fragment{
    double sum=0;
    DatabaseReference mDatabase;
    ListView shoppingList;
    ArrayList<Shopping_cart> Shopping_cart;
    DatabaseReference Shopping_cartRef;
    DatabaseReference ShoppingcartProduct;
    ProgressDialog mProgressDialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.activity_order_info,container,false);

        mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setMessage("Loading Products..");
        mProgressDialog.show();
        Intent i = getActivity().getIntent();

        String uid = i.getExtras().getString("name","");
        Shopping_cart = new ArrayList<Shopping_cart>();
        LinearLayout productList = (LinearLayout) view.findViewById(R.id.shoppingList);

        mDatabase = FirebaseDatabase.getInstance().getReference();


        Shopping_cartRef = mDatabase.child("Order");
        ShoppingcartProduct = Shopping_cartRef.child(uid).child("products");

        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {

                    Shopping_cart product = ds.getValue(Shopping_cart.class);

                    Shopping_cart.add(product);
                    adapterShopping itemsAdapter = new adapterShopping(getActivity(), Shopping_cart);
                    shoppingList = (ListView) view.findViewById(R.id.listViewshopping);
                    shoppingList.setAdapter(itemsAdapter);



                }

                mProgressDialog.dismiss();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        };
        ShoppingcartProduct.addListenerForSingleValueEvent(eventListener);



        return view;
    }
}

我得到的错误是:错误:(134,28)错误:不兼容的类型:orderInfoFragment无法转换为Fragment 但orderInfoFragment正在扩展片段,所以我不知道是什么问题! 我做错了吗?

1 个答案:

答案 0 :(得分:0)

您正在混合imports,这是一个非常常见的问题。要么你需要坚持支持库提供的组件,如Fragment和东西(所以你应该有import android.support....个条目) - 这就是你在OrderInfoTabbed类代码中所拥有的。或者使用plaftorm提供的Fragment和东西 - 正如你在orderInfoFragment课中所做的那样。但是虽然你可以混合使用它们,从技术上来说可以在你的代码中使用,但它们是 NOT 等效的,你可以看到它们不能从一个转换为另一个。

作为解决方案,删除import android.app....条目并重新添加(请参阅Android Studio can help you)这一次注意您选择哪一个作为AS将显示选项列表。

如果不确定你想要哪一个支持lib,那就是我认为你想要的那个。