【问题标题】:Why, after deleting the button in the Android Android studio’s layout, does it still see it?为什么在Android Android Studio的布局中删除按钮后,它仍然可以看到它?
【发布时间】:2019-10-06 11:07:45
【问题描述】:

我从布局中删除了注册按钮,android studio 看不到它,但是当应用程序启动时,它会出现。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:backgroundTint="@color/colorPrimaryDark"
    android:layout_height="match_parent"
    tools:context=".home.MainActivity">

    <FrameLayout
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/roundLayout"/>

    <com.github.florent37.shapeofview.shapes.RoundRectView
        android:id="@+id/roundLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:elevation="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:shape_roundRect_bottomLeftRadius="22dp"
        app:shape_roundRect_bottomRightRadius="22dp"
        app:shape_roundRect_topLeftRadius="8dp"
        app:shape_roundRect_topRightRadius="8dp">

        <com.ismaeldivita.chipnavigation.ChipNavigationBar
            android:id="@+id/chipNavigationMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/white"
            app:cnb_menuResource="@menu/bottom_menu"/>

    </com.github.florent37.shapeofview.shapes.RoundRectView>

</androidx.constraintlayout.widget.ConstraintLayout>

这里是调用activity_home 的类代码。我将activity_home代码转移到另一个布局后,它工作,但立即再次挂起,并没有改变。

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import androidx.fragment.app.Fragment
import cheprasov.egor.cchat.R
import cheprasov.egor.cchat.fragments.favorite.favorites
import cheprasov.egor.cchat.fragments.home.homeFragment
import cheprasov.egor.cchat.fragments.settings.settings
import com.google.firebase.auth.FirebaseAuth
import com.ismaeldivita.chipnavigation.ChipNavigationBar

class MainActivity : AppCompatActivity() {

    private lateinit var auth: FirebaseAuth
    private lateinit var mAthListner: FirebaseAuth.AuthStateListener
    private lateinit var menu: ChipNavigationBar
    private var fragment: Fragment? = null
    private lateinit var fragmentsHome: Fragment
    private lateinit var fragments: Fragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        init()

        if(fragment== null){
            val transaction = supportFragmentManager.beginTransaction()
            fragmentsHome = homeFragment()
            transaction.replace(R.id.nav_host_fragment,fragmentsHome)
            transaction.commit()
        }
        menu.setOnItemSelectedListener { id ->
            val option = when (id){
                R.id.home -> {
                    fragments = fragmentsHome
                    setFragment(fragments)
                }
                R.id.favorited -> {
                    fragments = favorites()
                    setFragment(fragments)
                }
                R.id.settings ->{
                    fragments = settings()
                    setFragment(fragments)
                }
                else -> finish()
            }
        }
    }

    private fun setFragment(fragment: Fragment){
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.nav_host_fragment,fragment)
        transaction.commit()
    }


    private fun init(){
        auth = FirebaseAuth.getInstance()
        menu = findViewById(R.id.chipNavigationMenu)
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
    }

    override fun onBackPressed() {
        super.onBackPressed()
        moveTaskToBack(true)
    }

    private fun signOut(){
        auth.signOut()
    }
}

[]

[]

[]

【问题讨论】:

  • 您在使用 Instant Run 吗?如果您没有禁用它 - 它可能会导致此问题。
  • @VladyslavMatviienko 我没有这个设置
  • 尝试使缓存无效并重新启动并从 android studio 模拟器中删除 apk 文件...它应该可以工作
  • 可能你有多个布局文件,而这不是你在 Activity 中使用的那个。
  • 禁用即时运行,然后应用无效缓存并在 AS 上重新启动

标签: android android-studio android-layout android-button


【解决方案1】:

最快的解决方案

尝试使用 Android Studio 上的运行或调试图标在模拟器上重新启动应用。

更清洁的解决方案

  1. 从模拟器中删除应用程序。
  2. 删除app/build/outputs/apk/debug/app-debug.apk中创建的apk文件

此问题通常发生在以下任一情况

  1. App不重新运行时,只改变布局
  2. 当您使用 Instant run 时,有时它可能不会立即重新渲染,这需要几秒钟..

【讨论】:

  • 没用,这个Button还是出现
  • 您检查的是否与删除按钮的视图相同
  • 是的,现在我添加了另一个按钮,但他没有看到它
  • 问题中给出的文件名称是什么?
  • activity_home.xml
猜你喜欢
  • 2022-10-16
  • 2018-06-25
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 2015-05-19
  • 1970-01-01
相关资源
最近更新 更多