【问题标题】:How to use onCreateView of AppCompatActivityAppCompatActivity的onCreateView如何使用
【发布时间】:2015-11-03 07:27:36
【问题描述】:

背景信息:我目前有一个正在运行的 AppCompatActivity。但是现在,一切都在onCreate 方法中。我担心它会导致我最近在使用 java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! 的 PagerAdapter 时遇到的一些小错误这些错误仅在我非常快速地来回切换活动时才会发生。我想知道修复这个是否会修复错误。

系统要求: 最小 SDK:15,目标 SDK:23(在撰写本文时,或任何最新版本)

问题:如何将我的用户界面初始化代码移动到onCreateView 方法中?

我熟悉如何实现android.support.v4.app.FragmentonCreate方法,像这样:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_songdetail, container, false);
    rootView.findViewById(R.id.info).setVisibility(View.INVISIBLE);

    return rootView;
}

但是对于 AppCompatActivity 没有类似的选项。这些是我的选项,来自AppCompatActivity 文档:

View onCreateView(String name, Context context, AttributeSet attrs)

View onCreateView(View parent, String name, Context context, AttributeSet attrs)

这是我的代码: (我省略了onCreate和onCreateView之外的方法)

public class DealPage extends AppCompatActivity
{
    private Deal deal;
    private Poll poll;
    private Video video;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        RealmDatabase.setRealmInstance(this);

        //----------- UNPACK EXTRAS -----------
        String date = getIntent().getExtras().getString(KeyStrings.EXTRA_DATE);

        //---------------- PREREQUISITE INITIALIZATION ----------
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deal_page);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_with_spinner);
        setSupportActionBar(toolbar);

        //------------ Enable "UP" navigation ---------------
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getDataFromDatabase(date);

        //----------------- THEMES AND COLORS ------------------

        //Set up colors
        final int backgroundColor = this.deal.getTheme().getBackgroundColor().getColor();
        final int accentColor = this.deal.getTheme().getAccentColor().getColor();
        String themeForeground = this.deal.getTheme().getForeground();
        final int foreground = generateForegroundColor(themeForeground);
        final String foregroundWebView = generateForegroundWebViewString(themeForeground); // This is necessary because HTML does not have an alpha channel.

        //Set toolbar colors
        toolbar.setBackgroundColor(accentColor);
        toolbar.setTitleTextColor(backgroundColor);

        //Set Page Background Color
        RelativeLayout dealPageBackground = (RelativeLayout) findViewById(R.id.deal_page_background);
        dealPageBackground.setBackgroundColor(backgroundColor);

        //----------- INITIALIZE THE ACTUAL DEAL PAGE STUFF ----------------

        //Title
        TextView title = (TextView) findViewById(R.id.title);
        title.setText(this.deal.getTitle());
        title.setTextColor(foreground);

        //Price
        TextView price = (TextView) findViewById(R.id.price);
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
        price.setText(fmt.format(this.deal.getItems().first().getPrice()));
        price.setTextColor(foreground);

        //ViewInBrowser
        setUpViewInBrowserButton(backgroundColor, accentColor);

        AndDown andDown = new AndDown();

        //Set up "linkColorHTML"
        String linkColorHTML = generateLinkColorHTML(accentColor);

        //Features
        setUpFeaturesView(andDown, backgroundColor, linkColorHTML, foregroundWebView);

        //More Specs button
        setUpMoreSpecsButton(backgroundColor, foreground, (Spinner) findViewById(R.id.spinner));

        //Story Title
        TextView storyTitle = (TextView) findViewById(R.id.story_title);
        storyTitle.setText(this.deal.getStory().getTitle());
        storyTitle.setTextColor(accentColor);

        //Story Body
        setUpStoryBody(andDown, backgroundColor, linkColorHTML, foregroundWebView);

        //Specs Title
        TextView specsTitle = (TextView) findViewById(R.id.specs_title);
        specsTitle.setText(this.deal.getTitle());
        specsTitle.setTextColor(accentColor);

        //Specs
        setUpSpecificationsView(andDown, backgroundColor, linkColorHTML, foregroundWebView);

        //Set up ViewPager
        ViewPager viewPager = (ViewPager) findViewById(R.id.photos_view_pager);
        viewPager.setAdapter(new PhotoPagerAdapter(this, this.deal.getPhotos()));

        // Setup spinner
        setUpSpinner(accentColor, backgroundColor, toolbar);
    }


    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs)
    {
        return super.onCreateView(name, context, attrs);
    }

}

【问题讨论】:

    标签: android oncreate


    【解决方案1】:

    您必须使用 PlaceholderFragment。 尝试这个:

    公共类 RestaurantListActivity 扩展 AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { ... if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment(), "fragmentName").commit(); } } public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {...}

    【讨论】:

      猜你喜欢
      • 2017-07-20
      • 2023-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多