Android Studio 在设置 setOnClickListener 后崩溃。

huangapple go评论59阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
android:padding=&quot;10dp&quot;&gt;

&lt;EditText
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:id=&quot;@+id/url&quot;
    android:hint=&quot;Enter URL&quot;
    android:inputType=&quot;textPersonName&quot;
    /&gt;
&lt;LinearLayout
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;horizontal&quot;&gt;
    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/downloadbtn&quot;
        android:text=&quot;Download&quot;/&gt;
    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/pastebtn&quot;
        android:text=&quot;Paste&quot;/&gt;
    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/cancelbtn&quot;
        android:text=&quot;Cancel&quot;/&gt;
&lt;/LinearLayout&gt;

</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(&quot;MissingInflatedId&quot;)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val songbtn = findViewById&lt;Button&gt;(R.id.btnsong)
        val downloadbtn = findViewById&lt;Button&gt;(R.id.downloadbtn)
        val pastebtn = findViewById&lt;Button&gt;(R.id.pastebtn)
        val cancelbtn = findViewById&lt;Button&gt;(R.id.cancelbtn)
        songbtn.setOnClickListener{
            val mDialogView = LayoutInflater.from (this). inflate(R.layout.url_dialog, null)
            val mBuilder = AlertDialog.Builder(this)
                .setView(mDialogView)
                .setTitle(&quot;Download MP3&quot;)
            val mAlertDialog = mBuilder.show()
        
        /* as soon as I use this code, the app crashes after clicking the &quot;btnsong&quot;:
            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&lt;Button&gt;(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&lt;Button&gt;(R.id.my_button)
button.setOnClickListener {
  
    Log.d(&quot;TAG&quot;, &quot;Clicked&quot;)
    dialog.dismiss() 
}

huangapple
  • 本文由 发表于 2023年2月16日 06:43:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定