Android Studio调试按钮按下 - 事件未触发

问题描述 投票:-2回答:6

我使用设计模式屏幕设计创建了这个屏幕。

enter image description here

这个XML是

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_height="match_parent"
    tools:context=".ftpDetails">

    <Button
        android:id="@+id/btnTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="11dp"
        android:layout_marginLeft="11dp"
        android:layout_marginBottom="16dp"
        android:text="@string/test"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/txtPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginLeft="96dp"
        android:layout_marginTop="292dp"
        android:width="250dp"
        android:ems="10"
        android:importantForAutofill="no"
        android:inputType="textPassword"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="o" />

    <EditText
        android:id="@+id/txtUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="216dp"
        android:width="250dp"
        android:ems="10"
        android:importantForAutofill="no"
        android:inputType="text"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="o" />

    <EditText
        android:id="@+id/txtFolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginLeft="96dp"
        android:layout_marginTop="140dp"
        android:width="250dp"
        android:ems="10"
        android:importantForAutofill="no"
        android:inputType="text"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="o" />

    <EditText
        android:id="@+id/txtServer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginLeft="96dp"
        android:layout_marginTop="68dp"
        android:width="250dp"
        android:ems="10"
        android:importantForAutofill="no"
        android:inputType="text"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="o" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="225dp"
        android:text="@string/username"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:text="@string/password"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintStart_toStartOf="@+id/textView4"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="17dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="150dp"
        android:text="@string/ftp_folder"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="75dp"
        android:text="@string/ftp_server"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/login_credentials"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:text="@string/save"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

这个问题基于XML的底部,按钮btnSave。

在我的这个活动的Java类中,我试图像这样创建一个onClickListener

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ftpDetails extends AppCompatActivity{
private TextView txtServer;
private TextView txtFolder;
private TextView txtUsername;
private TextView txtPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_ftp_details);
    setTitle(R.string.ftpTitle);

    txtServer = (TextView) findViewById(R.id.txtServer);
    txtFolder = (TextView) findViewById(R.id.txtFolder);
    txtUsername = (TextView) findViewById(R.id.txtUsername);
    txtPassword = (TextView) findViewById(R.id.txtPassword);

findViewById(R.id.btnSave).setOnClickListener(null);

    // Check that all text boxes have a value in them
    if (txtServer.getText().length() == 0)
    {
        // MESSAGE BOX
        AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle("Enter Server");
        msg.setMessage("Please enter a server address.");
        msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Leave this blank, this will mean nothing happens, the msg just disappears
            }
        });
    }

    if (txtFolder.getText().length() == 0)
    {
        // MESSAGE BOX
        AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle("Enter Folder");
        msg.setMessage("Please enter a folder to use.");
        msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Leave this blank, this will mean nothing happens, the msg just disappears
            }
        });
    }

    if (txtUsername.getText().length() == 0)
    {
        // MESSAGE BOX
        AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle("Enter Username");
        msg.setMessage("Please enter your username.");
        msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Leave this blank, this will mean nothing happens, the msg just disappears
            }
        });
    }

    if (txtPassword.getText().length() == 0)
    {
        // MESSAGE BOX
        AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle("Enter Server");
        msg.setMessage("Please enter a your password.");
        msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Leave this blank, this will mean nothing happens, the msg just disappears
            }
        });
    }
  }
}

我有断点

super.onCreate(savedInstanceState);

findViewById(R.id.btnSave).setOnClickListener(null);

if (txtServer.getText().length() == 0)

但是,当我将应用程序(Shift + F9)调试到我连接的设备(OnePlus 6T)时,没有任何断点被命中。此外,当我按下按钮并且文本框为空时,不会显示消息警报。

我做错了什么吗?我尝试过使用XML onClick:属性,但结果相同,并且在设计模式下设置相同的属性,但同样没有变化。

编辑

根据答案中的建议,我目前有这个

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_ftp_details);
    setTitle(R.string.ftpTitle);

    txtServer = (TextView) findViewById(R.id.txtServer);
    txtFolder = (TextView) findViewById(R.id.txtFolder);
    txtUsername = (TextView) findViewById(R.id.txtUsername);
    txtPassword = (TextView) findViewById(R.id.txtPassword);

    findViewById(R.id.btnSave).setOnClickListener(btnSaveListener);
}

private View.OnClickListener btnSaveListener =new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // my code in here
        }
    }

但我仍然没有结果。

编辑2

经过进一步调查后,当我按下按钮时,logcat窗口显示以下信息

2019-04-24 14:25:57.866 28031-28031 / com.example.my_test_app W / RenderThread:type = 1400 audit(0.0:425186):avc:denied {read} for name =“u:object_r:vendor_default_prop:s0 “dev =”tmpfs“ino = 22761 scontext = u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext = u:object_r:vendor_default_prop:s0 tclass = file permissive = 0

2019-04-24 14:25:57.875 28031-28057 / com.example.my_test_app E / libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval”

2019-04-24 14:25:57.886 28031-28057 / com.example.my_test_app E / libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval”

2019-04-24 14:25:57.876 28031-28031 / com.example.my_test_app W / RenderThread:type = 1400 audit(0.0:425187):avc:denied {read} for name =“u:object_r:vendor_default_prop:s0 “dev =”tmpfs“ino = 22761 scontext = u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext = u:object_r:vendor_default_prop:s0 tclass = file permissive = 0

2019-04-24 14:25:57.896 28031-28031 / com.example.my_test_app W / RenderThread:type = 1400 audit(0.0:425188):avc:denied {read} for name =“u:object_r:vendor_default_prop:s0 “dev =”tmpfs“ino = 22761 scontext = u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext = u:object_r:vendor_default_prop:s0 tclass = file permissive = 0

2019-04-24 14:25:57.903 28031-28057 / com.example.my_test_app E / libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval”

2019-04-24 14:25:57.916 28031-28031 / com.example.my_test_app W / RenderThread:type = 1400 audit(0.0:425189):avc:denied {read} for name =“u:object_r:vendor_default_prop:s0 “dev =”tmpfs“ino = 22761 scontext = u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext = u:object_r:vendor_default_prop:s0 tclass = file permissive = 0

2019-04-24 14:25:57.923 28031-28057 / com.example.my_test_app E / libc:拒绝查找属性“vendor.debug.egl.swapinterval”

2019-04-24 14:25:57.942 28031-28057 / com.example.my_test_app E / libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval”

2019-04-24 14:25:57.926 28031-28031 / com.example.my_test_app W / RenderThread:type = 1400 audit(0.0:425190):avc:denied {read} for name =“u:object_r:vendor_default_prop:s0 “dev =”tmpfs“ino = 22761 scontext = u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext = u:object_r:vendor_default_prop:s0 tclass = file permissive = 0

2019-04-24 14:25:57.954 28031-28057 / com.example.my_test_app E / libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval”

---------系统的开头

2019-04-24 14:25:58.237 28031-28057 / com.example.my_test_app I / chatty:uid = 10129(com.example.my_test_app)RenderThread相同的17行

2019-04-24 14:25:58.252 28031-28057 / com.example.my_test_app E / libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval”

java android xml debugging
6个回答
0
投票

.setOnClickListener(null)不会指定任何OnClickListener;那你打算如何调试那个事件呢?并且.getText().length()可能应该是.getText().toString().length() ...以便可以满足条件。


0
投票

你没有正确设置监听器。添加以下代码:

Button btnSave = (Button) findViewById(R.id.btnSave);

btnSave.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //add your conditions here
    }

0
投票

改变这一行:

findViewById(R.id.btnSave).setOnClickListener(null);

findViewById(R.id.btnSave).setOnClickListener(btnSaveListener);

并从onCreate方法中删除以下行。

然后创建btnSaveListener如下:

private View.OnClickListener btnSaveListener =new View.OnClickListener() {

   @Override
   public void onClick(View v) {
      // here goes all the code belove the line you change in the method `onCreate`
   }
};

0
投票

为什么你使用这样的点击监听器:findViewById(R.id.btnSave).setOnClickListener(null);

改为:

findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //put all your textView logic here
            }
        });

0
投票

您的按钮单击侦听器实现是错误的,请尝试这样:

 findViewById(R.id.action_ask).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Check that all text boxes have a value in them
                if (txtServer.getText().length() == 0)
                {
                    // MESSAGE BOX
                    AlertDialog.Builder msg = new AlertDialog.Builder(this);
                    msg.setTitle("Enter Server");
                    msg.setMessage("Please enter a server address.");
                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Leave this blank, this will mean nothing happens, the msg just disappears
                        }
                    });
                }

                if (txtFolder.getText().length() == 0)
                {
                    // MESSAGE BOX
                    AlertDialog.Builder msg = new AlertDialog.Builder(this);
                    msg.setTitle("Enter Folder");
                    msg.setMessage("Please enter a folder to use.");
                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Leave this blank, this will mean nothing happens, the msg just disappears
                        }
                    });
                }

                if (txtUsername.getText().length() == 0)
                {
                    // MESSAGE BOX
                    AlertDialog.Builder msg = new AlertDialog.Builder(this);
                    msg.setTitle("Enter Username");
                    msg.setMessage("Please enter your username.");
                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Leave this blank, this will mean nothing happens, the msg just disappears
                        }
                    });
                }

                if (txtPassword.getText().length() == 0)
                {
                    // MESSAGE BOX
                    AlertDialog.Builder msg = new AlertDialog.Builder(this);
                    msg.setTitle("Enter Server");
                    msg.setMessage("Please enter a your password.");
                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Leave this blank, this will mean nothing happens, the msg just disappears
                        }
                    });
                }
            }
        });

0
投票

最后,这不是我如何设置我的听众的问题。

将我的MainActivity启动类中的活动调用更改为StartActivity意味着正确调用了OnCreate代码。

© www.soinside.com 2019 - 2024. All rights reserved.