英文:
Android studio crashing after setOnClickListener
问题
以下是您要翻译的部分:
I want to create an app to download a song from an URL.
I made a Alertbox with EditText for the URL. This seems to work fine, but as soon as I want to assign the functions to the buttons inside the Alertbox, the app crashes as soon as I click on the Button to open the alertbox and I get a Nullpointer error.
See my cod below:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/url"
    android:hint="Enter URL"
    android:inputType="textPersonName"
    />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/downloadbtn"
        android:text="Download"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pastebtn"
        android:text="Paste"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cancelbtn"
        android:text="Cancel"/>
</LinearLayout>
</LinearLayout>
Kotlin代码:
package com.example.mymusicapp
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CancellationSignal
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
//import the button didnt fix it: import com.example.mymusicapp.R.id.downloadbtn
class MainActivity : AppCompatActivity() {
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val songbtn = findViewById<Button>(R.id.btnsong)
        val downloadbtn = findViewById<Button>(R.id.downloadbtn)
        val pastebtn = findViewById<Button>(R.id.pastebtn)
        val cancelbtn = findViewById<Button>(R.id.cancelbtn)
        songbtn.setOnClickListener{
            val mDialogView = LayoutInflater.from(this).inflate(R.layout.url_dialog, null)
            val mBuilder = AlertDialog.Builder(this)
                .setView(mDialogView)
                .setTitle("Download MP3")
            val mAlertDialog = mBuilder.show()
        /* as soon as I use this code, the app crashes after clicking the "btnsong":
            downloadbtn.setOnClickListener {
            mAlertDialog.dismiss()
            }
        }
    }
}
希望对您有所帮助。
英文:
I want to create an app to download a song from an URL.
I made a Alertbox with EditText for the URL. This seems to work fine, but as soon as I want to assign the functions to the buttons inside the Alertbox, the app crashes as soon as I click on the Button to open the alertbox and I get a Nullpointer error.
See my cod below:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/url"
    android:hint="Enter URL"
    android:inputType="textPersonName"
    />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/downloadbtn"
        android:text="Download"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pastebtn"
        android:text="Paste"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cancelbtn"
        android:text="Cancel"/>
</LinearLayout>
</LinearLayout>
Kotlin:
package com.example.mymusicapp
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CancellationSignal
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
//import the button didnt fix it: import com.example.mymusicapp.R.id.downloadbtn
class MainActivity : AppCompatActivity() {
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val songbtn = findViewById<Button>(R.id.btnsong)
        val downloadbtn = findViewById<Button>(R.id.downloadbtn)
        val pastebtn = findViewById<Button>(R.id.pastebtn)
        val cancelbtn = findViewById<Button>(R.id.cancelbtn)
        songbtn.setOnClickListener{
            val mDialogView = LayoutInflater.from (this). inflate(R.layout.url_dialog, null)
            val mBuilder = AlertDialog.Builder(this)
                .setView(mDialogView)
                .setTitle("Download MP3")
            val mAlertDialog = mBuilder.show()
        
        /* as soon as I use this code, the app crashes after clicking the "btnsong":
            downloadbtn.setOnClickListener {
            mAlertDialog.dismiss()
            }
        }
    }
}
答案1
得分: 1
你无法直接使用findviewbyid来访问对话框内的按钮。
val btn = mAlertDialog.findViewById<Button>(R.id.downloadbtn)
或者
val builder = AlertDialog.Builder(this)
val view = layoutInflater.inflate(R.layout.custom_dialog, null)
builder.setView(view)
val dialog = builder.create()
dialog.show()
val button = dialog.findViewById<Button>(R.id.my_button)
button.setOnClickListener {
    Log.d("TAG", "Clicked")
    dialog.dismiss()
}
英文:
You cannot access a button inside a dialog directly using findviewbyid.
val btn=mAlertDialog.findViewById<Button>(R.id.downloadbtn)
>or
val builder = AlertDialog.Builder(this)
val view = layoutInflater.inflate(R.layout.custom_dialog, null)
builder.setView(view)
val dialog = builder.create()
dialog.show()
val button = dialog.findViewById<Button>(R.id.my_button)
button.setOnClickListener {
  
    Log.d("TAG", "Clicked")
    dialog.dismiss() 
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论