java - Android 应用程序启动后崩溃,没有任何错误

标签 java android android-fragments

我的 Android 应用程序有问题。以前工作正常,但启动后立即崩溃,没有任何错误,我不明白为什么。我在我的类中评论了(//)很多代码,只留下了对工作重要的东西,但仍然是同样的问题。我有一个 mainActivity 和 2 个 fragment 。

My project hiearchy

当我创建 fragment 时,它会自动创建文件夹布局并将 java 类放在那里。 fragment java类(List、Set):

public class List extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_list, container, false);
    }
}

主要 Activity 类别:

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link 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 android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

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

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        FloatingActionButton addButton = (FloatingActionButton) findViewById(R.id.add);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }

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

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

        @Override
        public Fragment getItem(int position) {
            switch(position) {
                case 0:
                    return new Set();
                case 1:
                    return new List();
                default:
                    return null;
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "ZADÁVÁNÍ";
                case 1:
                    return "SEZNAM";
            }
            return null;
        }
    }
}

activity_main.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_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="cz.sudoman281.kubirovacikalkulacka.MainActivity">

    <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" />

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

fragment .xml文件:

<FrameLayout 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"
    tools:context=".layout.List">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:text="List"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView6" />

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </GridLayout>
</FrameLayout>

最佳答案

当应用程序启动时,logcat 过滤器并不总是显示日志。我在集成库时遇到了类似的问题。这就是我找到问题所在的方法。

以 Debug模式启动应用程序,在 onCreate 方法的第一行设置断点,然后继续单步执行 (F8) 直到崩溃。

关于java - Android 应用程序启动后崩溃,没有任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415955/

相关文章:

java - 如何将多个文本字段添加到数组列表 JavaFX

java - 如何使用混淆器混淆选择性类?

BroadcastReceiver的onReceive函数中的Android startActivity

android - MVP:管理 Activity 和 fragment 的最佳实践

android - Android 中 Room 根据主键自动排序

android - 从 fragment 到 Activity 循环的 Intent ?

java - 如何使变量在所有 fragment 之间保持 Activity 状态

java - Google DRIVE API V3 - 获取带有名称的文件夹 ID

java - 根据用户输入打印菱形图案

java - 总是调用第一个选项卡