【问题标题】:"App keeps stopping" pop ups and give the error java.lang.RuntimeExcpetion“应用程序不断停止”弹出窗口并给出错误 java.lang.RuntimeException
【发布时间】:2022-01-16 04:56:50
【问题描述】:

每当我在模拟器上运行项目时,它都会给我错误应用程序不断停止并且我无法运行该应用程序。调试代码时没有错误。所以我检查了 Logcat,然后它给了我错误 java.lang.RuntimeException。我不知道错误在哪里,或者根本不是代码的问题?请有人帮助我...

  1. 主要活动代码

     class MainActivity1 : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     setContentView(R.layout.activity_main)
    
     val circularProgressBar: CircularProgressBar = findViewById(R.id.circularProgressBar)
    
     circularProgressBar.setProgressWithAnimation(65f,1000)
    
     findViewById<SeekBar>(R.id.seekBarProgress).onProgressChanged{
         circularProgressBar.progress = it
     }
     }
     private fun SeekBar.onProgressChanged(onProgressChanged: (Float)-> Unit){
     setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
             override fun onProgressChanged(seekBar: SeekBar?, progress : Int, 
     fromUser: Boolean) {
             onProgressChanged(progress.toFloat())
             }
    
             override fun onStartTrackingTouch(seekBar: SeekBar?) {
    
             }
    
             override fun onStopTrackingTouch(seekBar: SeekBar?) {
    
             }
         })
     }
    

    }

  2. Logcat Image of the Logcat

【问题讨论】:

    标签: android android-studio kotlin android-emulator runtimeexception


    【解决方案1】:

    你在一个空对象上调用setProgressWithAnimation。我建议你不要使用findViewById。使用 ViewBinding 或至少使用 kotlinx。

    【讨论】:

    • 感谢您的帮助。我是这个应用程序开发的新手,并试图了解它是如何工作的。那我可以请你更详细地解释一下吗?
    • 能否请您给我您的主要活动 XML 文件?或者您也可以查看this 问题。顺便说一句,您可以阅读有关 viewBinding here 的信息
    【解决方案2】:

    这是我的主要活动 xml 文件。 (不知道有没有帮助)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.anadroid.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity1"
        android:background="@color/white"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/bg_header"
            android:orientation="horizontal"
            android:layout_marginBottom="20dp"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fontFamily="@font/poppins_semibold"
                android:text="AkiKomi"
                android:gravity="center_horizontal"
                android:layout_marginStart="15dp"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:textSize="25sp"/>
    
        </LinearLayout>
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/my_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      相关资源
      最近更新 更多