1

以下のコードをご確認ください

    @SuppressWarnings("deprecation")
    public class AndroidTabLayoutActivity extends TabActivity implements OnTabChangeListener {
        TabHost tabHost;
        TabWidget tabWidget;
        Menu menu;
        View v;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tabWidget = getTabWidget();
            tabHost = getTabHost();
            tabHost.setOnTabChangedListener(this);

            TabSpec eventbyspec = tabHost.newTabSpec("EventBy");
            // eventbyspec.setIndicator("EventBy",
            // getResources().getDrawable(R.drawable.icon_photos_tab));
            eventbyspec.setIndicator("EventBy");
            Intent eventbyIntent = new Intent(this, MainActivity.class);
            eventbyspec.setContent(eventbyIntent);

            // Tab for category
            TabSpec categoryspec = tabHost.newTabSpec("Category");
            categoryspec.setIndicator("Category");
            Intent categoryIntent = new Intent(this, CatagoryEvent.class);
            categoryspec.setContent(categoryIntent);


            // Adding all TabSpec to TabHost
            tabHost.addTab(eventbyspec); // Adding eventby tab
            tabHost.addTab(categoryspec); // Adding category tab    
        }


        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
               MenuInflater inflater = getMenuInflater();
               int tab = getTabHost().getCurrentTab();

               if (tab==1)
                   inflater.inflate(R.menu.menu, menu); 
               return true;
            }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {
            case R.id.music:
                // Single menu item is selected do something
                // Ex: launching new activity/screen or show alert message
                Intent music_intent = new Intent(getApplicationContext(),MusicFragment.class);
                startActivity(music_intent);
                return true;
            case R.id.theatre:
                Intent music_intent = new Intent(getApplicationContext(),TheatreFragment.class);
                startActivity(music_intent);
                return true;

            default:
                return super.onOptionsItemSelected(item);
            }
        }

    }

MusicFragment.java の場所

                public class MusicFragment extends Fragment implements OnClickListener {

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.music_layout, container, false);
            LinearLayout mLayout = (LinearLayout) v
                    .findViewById(R.id.catagory_linearlayout);

            mLayout.setBackgroundResource(R.drawable.music_full);
            TableLayout tableLayout = (TableLayout) v.findViewById(R.id.maintable);
            tableLayout.removeAllViews();
            for (int i = 0; i < 50; i++) {

                TableRow tableRow = new TableRow(v.getContext());
                tableRow.setPadding(10, 10, 10, 10);
                tableRow.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                TextView tv = new TextView(v.getContext());
                tv.setTextColor(getResources().getColor(R.color.black_overlay));

                tv.setText("random text here 11");


                tableRow.addView(tv);

                tv.setOnClickListener(this);


                tableLayout.addView(tableRow, new TableLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            }
            return v;
        }

        public void onClick(View arg0) {
            startActivity(new Intent(getActivity(), TextActivity.class));
        }

    }

私のCatagoryEvent.javaは

    public class CatagoryEvent extends FragmentActivity {

        SectionsPagerAdapter mSectionsPagerAdapter;
        ViewPager mViewPager;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.catagory_layout);      

            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);
        }

        public class SectionsPagerAdapter extends FragmentPagerAdapter {
            public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }

             @Override
                public Fragment getItem(int position) {
                    Fragment fragment;
                    switch (position) {
                        case 0:
                            fragment = new MusicFragment();
                            break;
                        case 1:
                            fragment = new TheatreFragment();
                            break;                 
                        default:
                            fragment  = null;
                            break;
                    }
                    return fragment;

                }
            @Override
            public int getCount() {
                // Show 3 total pages.
                return 2;
            }       
            @Override
            public CharSequence getPageTitle(int position) {
                Locale l = Locale.getDefault();
                switch (position) {
                case 0:
                    return getString("Music");
                case 1:
                    return getString("Theatre");

                }
                return null;
            }   
        }
    }

問題

問題はクリック メニュー項目にあります MusicFragment.java が呼び出されず、androidmanifaste で宣言するように求められましたが、androidmanifaste で宣言できません

アクティビティからフラグメント クラスを呼び出す方法はありますか?

4

1 に答える 1

1

いいえ、できません。コードを見ると、すでに問題を自分で確認できます。'startActivity' を使用して Fragment を開始します。

Fragment を存続させるには、Activity 内でホストする必要があります。ただし、このフラグメントを空のアクティビティにラップし、代わりに onClick からそのアクティビティを呼び出すことができます。

于 2013-06-24T07:50:53.147 に答える