AppCompatActivity中的碎片

时间:2016-12-10 18:07:16

标签: android android-fragments nullpointerexception

如果我想在滑动视图活动中使用片段,我可以使用此扩展吗?

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

My 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 ComponentInfo{de.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 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_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="de.lukas_schroederoutlook.connect.Overview_Screen">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        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_width="match_parent"
        android:layout_height="wrap_content" />

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

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

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/feed_fragment_portrait"/>

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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_width="match_parent"
android:layout_height="match_parent"
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_width="wrap_content"
    android:layout_height="wrap_content" />

我的概述屏幕片段xml:

sudo apt-get install g++

2 个答案:

答案 0 :(得分:3)

您需要在class代码中指定<fragment>属性。

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/feed_fragment_portrait"/>

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/feed_fragment_landscape"/>

在这两者上添加class="your.packagename.SomeFragment"(如果你确实需要两者......通常你有不同的版面文件用于landspace和portrait,而不是两者都有)

看起来你正在使用ViewPager,所以你真的需要<fragment>标签吗?我想你可以删除它们。

答案 1 :(得分:0)

你需要这样的东西:

status

或使用

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

正如板球所说。