切换到多窗口模式时,我的Android应用程序崩溃

时间:2018-07-31 06:03:46

标签: android kotlin

切换到多窗口模式时,我的Android应用程序崩溃。 当此应用程序仅显示在屏幕上并且我长时间单击多任务按钮时,该应用程序崩溃。
相比之下,从家用启动器进行标准启动时,不会出现此问题。

这是堆栈跟踪。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.otk1fd.simplemio/com.otk1fd.simplemio.activities.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4699)
    at android.app.ActivityThread.-wrap18(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
 Caused by: java.lang.IllegalStateException: Fragment com.otk1fd.simplemio.fragments.ConfigFragment did not create a view.
    at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3569)
    at android.app.FragmentController.onCreateView(FragmentController.java:98)
    at android.app.Activity.onCreateView(Activity.java:6219)
    at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.otk1fd.simplemio.activities.MainActivity.onCreate(MainActivity.kt:38)
    at android.app.Activity.performCreate(Activity.java:7009)
    at android.app.Activity.performCreate(Activity.java:7000)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4699)
    at android.app.ActivityThread.-wrap18(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是MainActivity的部分代码
MainActivity.kt:38setContentView(R.layout.activity_main)

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

private lateinit var navigationView: NavigationView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    MioUtil.setUp(this)
    HttpErrorHandler.setUp(loginFunc = { startOAuthWithDialog() }, showErrorMessageFunc = { errorMessage -> Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show() })

    setContentView(R.layout.activity_main)

    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)

    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val actionBarDrawerToggle = ActionBarDrawerToggle(
            this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close)
    drawerLayout.addDrawerListener(actionBarDrawerToggle)
    actionBarDrawerToggle.syncState()

    navigationView = findViewById(R.id.nav_view)
    navigationView.setNavigationItemSelectedListener(this)
    navigationView.menu.getItem(0).isChecked = true

    supportActionBar?.title = getString(R.string.menu_coupon)

    val defaultFragment = CouponFragment()
    val fragmentTransaction = fragmentManager.beginTransaction()
    fragmentTransaction.replace(R.id.fragment, defaultFragment)
    fragmentTransaction.commit()
}

这是activity_main.xml的MainActivity布局文件。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

我的应用程序的完整代码在这里。
https://github.com/otsuka-kohei/simple-mio/tree/issue%2365

我检查了ConfigFragment(扩展为PreferenceFragment),但是出现此问题。 例如,禁用ConfigFragment并在没有ConfigFragment的情况下进行构建。

我的代码有什么问题? 请帮我。 谢谢。

2 个答案:

答案 0 :(得分:0)

您忘了包括布局。

layout="@layout/yourlayout"

应该是

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/yourlayout" />

答案 1 :(得分:0)

我找到了解决方法。
我必须像下面这样制作content_main.xml中使用的activity_main.xml / app_bar_main.xml /

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.otk1fd.simplemio.activities.MainActivity"
tools:showIn="@layout/app_bar_main">

<fragment
    android:id="@+id/fragment"
    android:name="com.otk1fd.simplemio.fragments.ConfigFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.otk1fd.simplemio.activities.MainActivity"
tools:showIn="@layout/app_bar_main">

<FrameLayout
    android:id="@+id/fragmentLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

我不能在布局xml文件中将Fragment布局组件与可切换的Fragment对象一起使用。
如果使用可切换片段,则必须在布局xml文件中使用FlameLayout。
谢谢你的sasikumar。