AppCompatActivity 中的片段

Posted

技术标签:

【中文标题】AppCompatActivity 中的片段【英文标题】:Fragments in AppCompatActivity 【发布时间】:2017-04-26 00:10:16 【问题描述】:

如果我想在我的滑动视图活动中使用 Fragments,我可以使用这个扩展吗?

public class Overview_Screen extends AppCompatActivity 

我的 Fragment 类使用这个:

public class MessagesFragmentPortrait extends android.support.v4.app.Fragment 

我问这个是因为我的应用程序不断崩溃... 这是我的主要活动:

public class Overview_Screen extends AppCompatActivity 

    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        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) 
        getMenuInflater().inflate(R.menu.menu_overview_screen, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        int id = item.getItemId();

        if (id == R.id.action_settings) 
            Intent settings_intent = new Intent(this, Settings_Screen.class);
            startActivity(settings_intent);
            return true;
        

        return super.onOptionsItemSelected(item);
    

    public static class SectionsPagerAdapter extends FragmentPagerAdapter 

        public SectionsPagerAdapter(FragmentManager fm) 
            super(fm);
        

        @Override
        public Fragment getItem(int i) 
            switch (i) 
                case 0:
                    return new AccountFragmentPortrait();
                case 1:
                    return new FeedFragmentPortrait();
                default:
                    return new MessagesFragmentPortrait();
            
        

        @Override
        public int getCount() 
            return 3;
        

        @Override
        public CharSequence getPageTitle(int position) 
            switch (position) 
                case 0:
                    return "Account";
                case 1:
                    return "Feed";
                case 2:
                    return "Messages";
            
            return null;
        
    

这是我的堆栈跟踪:

FATAL EXCEPTION: main
  Process: de.lukas_schroederoutlook.connect, PID: 3364
  java.lang.RuntimeException: Unable to start activity ComponentInfode.lukas_schroederoutlook.connect/de.lukas_schroederoutlook.connect.Overview_Screen:
  android.view.InflateException: Binary XML file line #41: Binary XML file line #41: Error inflating class fragment
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6119)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
   Caused by: android.view.InflateException: Binary XML file line #41: Binary XML file line #41: Error inflating class fragment
   Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
   Caused by: java.lang.NullPointerException
      at java.lang.VMClassLoader.findLoadedClass(Native Method)
      at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:742)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:362)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
      at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:469)
      at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2384)
      at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
      at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
      at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
      at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:77)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
      at de.lukas_schroederoutlook.connect.Overview_Screen.onCreate(Overview_Screen.java:42)
      at android.app.Activity.performCreate(Activity.java:6679)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6119)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最后,这是我的概览屏幕 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_
    android:layout_
    android:fitsSystemWindows="true"
    tools:context="de.lukas_schroederoutlook.connect.Overview_Screen">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_
    android:layout_
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_
        android:layout_
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_
        android:layout_ />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_
    android:layout_
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<fragment
    android:layout_
    android:layout_
    android:id="@+id/feed_fragment_portrait"/>

<fragment
    android:layout_
    android:layout_
    android:id="@+id/feed_fragment_landscape"/>

还有我的概览屏幕片段 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.lukas_schroederoutlook.connect.Overview_Screen$PlaceholderFragment">

<TextView
    android:id="@+id/section_label"
    android:layout_
    android:layout_ />

【问题讨论】:

请分享您使用的代码并写下错误代码 你有一个错误 de.lukas_schroederoutlook.connect/de.lukas_schroederoutlook.connect.Overview_Screen xml 端。请分享这个xml代码 【参考方案1】:

您需要在&lt;fragment&gt; 标签中指定class 属性。

<fragment
    android:layout_
    android:layout_
    android:id="@+id/feed_fragment_portrait"/>

<fragment
    android:layout_
    android:layout_
    android:id="@+id/feed_fragment_landscape"/>

在这两者上添加class="your.packagename.SomeFragment"(如果你真的需要两者...通常你有不同的布局文件用于土地空间和肖像,而不是两者合二为一)


看起来您使用的是 ViewPager,那么您真的需要 &lt;fragment&gt; 标记吗?我认为您可以删除它们。

【讨论】:

【参考方案2】:

你需要这样的东西:

<fragment
  android:name="de.lukas_schroederoutlook.connect.FeedFragmentPortrait"
  android:layout_
  android:layout_
  android:id="@+id/feed_fragment_portrait"/>

或者使用

class=

正如板球所说。

【讨论】:

这表示你可以使用 "name=..." developer.android.com/guide/components/fragments.html 这里说“name=”和“class=”是一样的。 ***.com/questions/10162983/… 我在发布之前查看了我的一个项目,它使用“name=",我认为这是当时唯一的方法。

以上是关于AppCompatActivity 中的片段的主要内容,如果未能解决你的问题,请参考以下文章

如何给非AppCompatActivity添加Toolbar

片段管理器空引用异常

找不到 androidx.appcompat.app.AppCompatActivity 的类文件

AppCompatActivity 不推荐使用 onActivityResult()

AnroidStudio编译错误:import android.support.v7.app.AppCompatActivity

android.support.v7.app.AppCompatActivity