android - 使用片段中的活动

时间:2017-01-08 10:59:01

标签: android fragment

我是初学者,我正在尝试使用碎片操作三个页面来创建应用程序 这是我的代码: MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("Connect"));
        tabLayout.addTab(tabLayout.newTab().setText("Piloting"));
        tabLayout.addTab(tabLayout.newTab().setText("Setup"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
 }
Pageradpter
public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                TabFragment1 tab1 = new TabFragment1();
                return tab1;
            case 1:
                TabFragment2 tab2 = new TabFragment2();
                return tab2;
            case 2:
                TabFragment3 tab3 = new TabFragment3();
                return tab3;
            default:
                return null;
        }
    }



    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}
TabFragment1
public class TabFragment1 extends Fragment {

    Context context;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_device_list, container, false);
        context = rootView.getContext();
        Intent intent = new Intent(context, DeviceListActivity.class);
        getActivity().startActivity(intent);
        return rootView;

DeviceListActivity
protected void onCreate(View v) {
      //super.onCreate(savedInstanceState);
     // setContentView(R.layout.activity_device_list);

     // View row = (View) v.getParent();



      final ListView listView = (ListView) v.findViewById(R.id.list);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Device does not support Bluetooth
        } else {
            if (!mBluetoothAdapter.isEnabled()) {
                AlertDialog dialogAlert = new AlertDialog.Builder(this).create();
                dialogAlert.setTitle("Ricerca Drone");
                dialogAlert.setMessage("Attivare prima il Bluetooth");
                dialogAlert.setButton(DialogInterface.BUTTON_POSITIVE,"Attivare", new DialogInterface.OnClickListener(){


                    public void onClick(DialogInterface dialog, int which) {
                        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
                            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                        }
                    }
                });
        dialogAlert.show();

            }
        }
        // Assign adapter to ListView
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // launch the activity related to the type of discovery device service
                Intent intent = null;

                ARDiscoveryDeviceService service = (ARDiscoveryDeviceService) mAdapter.getItem(position);
                ARDISCOVERY_PRODUCT_ENUM product = ARDiscoveryService.getProductFromProductID(service.getProductID());
                switch (product) {
                    case ARDISCOVERY_PRODUCT_ARDRONE:
                        break;

                    case ARDISCOVERY_PRODUCT_MINIDRONE:
                    case ARDISCOVERY_PRODUCT_MINIDRONE_EVO_BRICK:
                    case ARDISCOVERY_PRODUCT_MINIDRONE_EVO_LIGHT:
                    case ARDISCOVERY_PRODUCT_MINIDRONE_DELOS3:
                        intent = new Intent(DeviceListActivity.this, MiniDroneActivity.class);
                        break;


                    default:
                        Log.e(TAG, "The type " + product + " is not supported by this sample");
                }

我想知道如何运行第一页DeviceListActivity代码,就像我在listview中使用nullpointer一样

感谢您的帮助

0 个答案:

没有答案