【问题标题】:Fragment not work when actionbar activity start in android?当动作栏活动在android中启动时片段不起作用?
【发布时间】:2014-10-07 13:27:29
【问题描述】:

你好,我在第一次启动时在操作栏活动中实现列表视图,但它不起作用

我尝试了下面的代码,但它对我不起作用我刚刚开始学习操作栏

MainActivity.java

public class MainActivity extends ActionBarActivity implements
        NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the
     * navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in
     * {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

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

        mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        Fragment fr=new FragmentOne();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager .beginTransaction().replace(R.id.container, fr);

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments



    }


    public void restoreActionBar() {


        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class FragmentOne extends Fragment {


            private String[] mStrings={
                    "http://androidexample.com/media/webservice/LazyListView_images/image0.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image1.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image2.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image3.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image4.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image5.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image6.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image7.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image8.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image9.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image10.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image0.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image1.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image2.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image3.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image4.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image5.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image6.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image7.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image8.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image9.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image10.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image0.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image1.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image2.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image3.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image4.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image5.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image6.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image7.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image8.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image9.png",
                    "http://androidexample.com/media/webservice/LazyListView_images/image10.png"

            };

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {


                return inflater.inflate(R.layout.fragmentone,container,false);

            }


            @Override
            public void onActivityCreated(Bundle savedInstanceState) {

                super.onActivityCreated(savedInstanceState);

                ListView listview=(ListView) getActivity().findViewById(R.id.listView);

                  // Create custom adapter for listview
                LazyImageLoadAdapter adapter=new LazyImageLoadAdapter(getActivity(), mStrings);

                //Set adapter to listview
                listview.setAdapter(adapter);


            }

        }
}

【问题讨论】:

  • 请分享更多信息:例如日志,什么是异常等等。乍一看,我看到你应该在你的 onCreate 方法中提交你的片段事务。修复它,这可能是原因之一;)
  • 没有得到你的点提交意味着?
  • 对不起,我的意思是:fragmentManager .beginTransaction().replace(R.id.container, fr)。 commit(); 请不要忘记附上有关错误的更多信息,以使其更容易
  • 感谢它的工作
  • 只有一个问题当前 FragmentOne() 类在同一个活动中,如果我在活动之外声明这个类意味着作为一个新类而不是它给我错误 Fragment fr=new FragmentOne();将 fr 转换为 FragmentOne() 如果我将 fr 转换为 Fragmentone() 而不是 fragmentManager .beginTransaction().replace(R.id.container,fr).commit();在这个替换中给我错误更改 fr 到片段如果我想在活动之外使用 fragmenone() 我该怎么办

标签: android android-actionbar fragment


【解决方案1】:

提交交易!

fragmentManager.beginTransaction().replace(R.id.container, fr).commit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多