有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java应用程序在添加片段时崩溃

我正在尝试创建一个类似谷歌音乐的应用程序,带有导航抽屉和标签。 导航抽屉部分正在工作,但现在我无法让选项卡工作。添加片段(在main的onCreate末尾)会导致应用程序崩溃 (我注意到有时它不会崩溃?!,但在90%的情况下它会崩溃)。 我是Android新手,不知道问题来自哪里。错误消息似乎是描述性的,但我无法指出代码的一部分

我的主要活动是扩展AppCompatActivity。布局是主要的。xml:

<?xml version="1.0" encoding="utf-8"?>

<安卓.support.v4.widget.DrawerLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/drawer_layout"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <!-- The main content view -->
    <LinearLayout
        xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
        安卓:orientation="vertical"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:id="@+id/main_layout"
        安卓:weightSum="1">

        <!-- Toolbar -->
        <!-- http://stackoverflow.com/questions/26708230/where-do-i-define-xml-for-the-toolbar-widget-in-安卓-5-0 -->
        <include layout="@layout/toolbar_actionbar_with_headerbar" />

        <fragment
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:name="安卓.support.v4.app.ListFragment"
            安卓:id="@+id/fragment" />
    </LinearLayout>    

    <!-- The navigation drawer -->
    <安卓.support.v7.widget.RecyclerView
        安卓:id="@+id/RecyclerView"
        安卓:layout_width="250dp"
        安卓:layout_height="match_parent"
        安卓:layout_gravity="left"    
        安卓:background="#ffffff"
        安卓:scrollbars="vertical">    
    </安卓.support.v7.widget.RecyclerView>  

</安卓.support.v4.widget.DrawerLayout>

在main的onCreate函数结束之前,一切正常:

// Load Tab Fragment
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager != null) {
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    if (transaction != null) {
        main_tabs_frag = new TabbedActivity();
        if (main_tabs_frag != null) {
            transaction.add(R.id.fragment, main_tabs_frag);
            transaction.commit();
        }
    }
}

以下是选项卡式活动:

package com.example.xy.test;

import java.util.Locale;

import 安卓.os.Bundle;
import 安卓.support.v4.app.Fragment;
import 安卓.support.v4.app.FragmentManager;
import 安卓.support.v4.app.FragmentPagerAdapter;
import 安卓.support.v4.view.ViewPager;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.TextView;

public class TabbedActivity extends Fragment {

    /**
     * The {@link 安卓.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link 安卓.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link 安卓.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    public static final String TAG = TabbedActivity.class.getSimpleName();

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;


    public static TabbedActivity newInstance() {
        return new TabbedActivity();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tabbed, container, false);
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getChildFragmentManager());

        mViewPager = (ViewPager) v.findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        return v;
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.tab_1).toUpperCase(l);
                case 1:
                    return getString(R.string.tab_2).toUpperCase(l);
                case 2:
                    return getString(R.string.tab_3).toUpperCase(l);
            }
            return null;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_tabbed_content,
                    container, false);
            TextView dummyTextView = (TextView) rootView
                    .findViewById(R.id.section_label);
            dummyTextView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

}

fragement_标签内容。xml

<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TabbedActivity$DummySectionFragment" >

<TextView
    安卓:id="@+id/section_label"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content" />

</RelativeLayout>

“碎片”选项卡。xml

<安卓.support.v4.view.ViewPager
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:id="@+id/pager"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <!--
        This title strip will display the currently visible page title, as well as the page
        titles for adjacent pages.
    -->
    <安卓.support.v4.view.PagerTitleStrip
        安卓:id="@+id/pager_title_strip"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="top"
        安卓:background="#33b5e5"
        安卓:paddingBottom="4dp"
        安卓:paddingTop="4dp"
        安卓:textColor="#fff" />

</安卓.support.v4.view.ViewPager>

以下是错误输出:

08-07 09:26:30.511    1874-1874/com.example.thomas.noteapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.thomas.noteapp, PID: 1874
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thomas.noteapp/com.example.thomas.noteapp.main}: 安卓.view.InflateException: Binary XML file line #33: Error inflating class fragment
            at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at 安卓.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912)
            at 安卓.app.ActivityThread.access$900(ActivityThread.java:144)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
            at 安卓.os.Handler.dispatchMessage(Handler.java:102)
            at 安卓.os.Looper.loop(Looper.java:135)
            at 安卓.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: 安卓.view.InflateException: Binary XML file line #33: Error inflating class fragment
            at 安卓.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at 安卓.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at 安卓.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at 安卓.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at 安卓.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.thomas.noteapp.main.onCreate(main.java:46)
            at 安卓.app.Activity.performCreate(Activity.java:5937)
            at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at 安卓.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912)
            at 安卓.app.ActivityThread.access$900(ActivityThread.java:144)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
            at 安卓.os.Handler.dispatchMessage(Handler.java:102)
            at 安卓.os.Looper.loop(Looper.java:135)
            at 安卓.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalStateException: Fragment 安卓.support.v4.app.ListFragment did not create a view.
            at 安卓.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2200)
            at 安卓.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
            at 安卓.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:816)
            at 安卓.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:72)
            at 安卓.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:804)
            at 安卓.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
            at 安卓.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
            at 安卓.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at 安卓.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at 安卓.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at 安卓.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.thomas.noteapp.main.onCreate(main.java:46)
            at 安卓.app.Activity.performCreate(Activity.java:5937)
            at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at 安卓.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912)
            at 安卓.app.ActivityThread.access$900(ActivityThread.java:144)
            at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
            at 安卓.os.Handler.dispatchMessage(Handler.java:102)
            at 安卓.os.Looper.loop(Looper.java:135)
            at 安卓.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:694)

共 (2) 个答案

  1. # 2 楼答案

    在片段视图中,必须提及片段类名称,而不是列表片段

     <fragment
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="packageName.className"
            android:id="@+id/fragment" />
    

    确保你的“活动”扩展了“碎片活动”