【问题标题】:Kotlin App stopped working (LOGCAT included) - I am a Beginner in Coding [closed]Kotlin App 停止工作(包括 LOGCAT)-我是编码初学者 [关闭]
【发布时间】:2022-01-14 16:24:05
【问题描述】:

我不知道我做了什么,但我的应用程序不再工作 - 你能帮忙吗?

这是我尝试启动应用程序时得到的 Logcat 的第一行

2022-01-14 17:17:10.138 14076-14076/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.wit.location, PID: 14076
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.wit.location/org.wit.location.activities.LocationList}: android.view.InflateException: Binary XML file line #16 in org.wit.location:layout/list_of_locations: Binary XML file line #16 in org.wit.location:layout/list_of_locations: Error inflating class com.google.android.material.appbar.AppBarLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)

[...]

并且在我的 manifests.xml 文件中有一些下划线,我不知道它是否重要? enter image description here

 <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.wit.location.activities.LocationList">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:fitsSystemWindows="true"
        app:elevation="0dip"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:titleTextColor="@color/colorPrimary" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

如果你也需要这个文件

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import org.wit.location.R
import org.wit.location.adapters.LocationAdapter
import org.wit.location.adapters.LocationListener
import org.wit.location.databinding.ListOfLocationsBinding
import org.wit.location.main.MainApp
import org.wit.location.models.Location_Model

class LocationList : AppCompatActivity(), LocationListener {

    lateinit var app: MainApp
    private lateinit var binding: ListOfLocationsBinding
    private lateinit var refreshIntentLauncher : ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ListOfLocationsBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.toolbar.title = title
        setSupportActionBar(binding.toolbar)

        app = application as MainApp

        val layoutManager = LinearLayoutManager(this)
        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.adapter = LocationAdapter(app.locations.findAll(),this)


        registerRefreshCallback()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.mainmenu, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.item_add -> {
                val launcherIntent = Intent(this, BasicLocation::class.java)
                refreshIntentLauncher.launch(launcherIntent)
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onLocationClick(location: Location_Model) {
        val launcherIntent = Intent(this, BasicLocation::class.java)
        launcherIntent.putExtra("location_edit", location)
        refreshIntentLauncher.launch(launcherIntent)
    }

    private fun registerRefreshCallback() {
        refreshIntentLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult())
            { binding.recyclerView.adapter?.notifyDataSetChanged() }
    }
}

【问题讨论】:

  • 您阅读日志中的消息了吗?它会告诉您哪个布局文件以及该文件的哪一行有问题。
  • 请分享您的代码。这在您的布局 xml 文件中显示了一些错误
  • 我添加了代码。是的,我读过它,但正如我所说我是一个初学者,不幸的是我没有在 .xml 文件中找到错误
  • 请包含整个堆栈跟踪。

标签: android android-studio kotlin


【解决方案1】:

你用的是什么主题?您是否尝试过先删除 appbarlayout 以查看它是否崩溃?您可能需要在应用程序中使用材料主题

【讨论】:

  • 哇,谢谢你救了我!!!在theme.xml中的''Location''之后有一个点“。”,所以整个程序不起作用它就像这样&lt;style name="Theme.Location." parent="Theme.MaterialComponents.DayNight.NoActionBar"&gt;我现在删除了这个点它就像这样&lt;style name="Theme.Location" parent="Theme.MaterialComponents.DayNight.NoActionBar"&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 2020-07-25
  • 1970-01-01
  • 2012-04-03
相关资源
最近更新 更多