Java Android Studio调试按钮按下-事件未触发
我使用设计模式屏幕设计创建了这个屏幕 这方面的XML是Java Android Studio调试按钮按下-事件未触发,java,android,xml,debugging,Java,Android,Xml,Debugging,我使用设计模式屏幕设计创建了这个屏幕 这方面的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:tool
<?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>
我在线路上有断点
super.onCreate(savedInstanceState);
findViewById(R.id.btnSave).setOnClickListener(null);
及
但是,当我将应用程序Shift+F9调试到连接的设备OnePlus 6T时,没有一个断点被命中。此外,当我按下按钮且文本框为空时,不会显示消息警报
我做错什么了吗?我尝试过使用XML onClick:attribute,但得到了相同的结果,并且在设计模式中设置了相同的属性,但同样没有改变
编辑
根据答案中的建议,我目前有以下几点
@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 audit0.0:425186:avc:denied{read}for name=u:object\u r:vendor\u default\u prop:s0 dev=tmpfs ino=22761 scontext=u:r:untrusted\u app:s0:c129、c256、c512、c768 tcntext=u:object\r:vendor:vendor\u default\u prop\u 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 audit0.0:425187:avc:denied{read}for name=u:object\r:vendor\u default\u prop:s0 dev=tmpfs ino=22761 scontext=u:r:untrusted\u app:s0:c129、c256、c512、c768 tcontext=u:u:object\r:vendor\u default\u prop\u prop:s0
2019-04-24 14:25:57.896 28031-28031/com.example.my_test_app W/RenderThread:type=1400 audit0.0:425188:avc:denied{read}for name=u:object\u r:vendor\u default\u prop:s0 dev=tmpfs ino=22761 scontext=u:r:untrusted\u app:s0:c129、c256、c512、c768 tcontext=u:u:object\r:vendor\u default\u prop\u prop:s0
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 audit0.0:425189:avc:denied{read}for name=u:object\u r:vendor\u default\u prop:s0 dev=tmpfs ino=22761 scontext=u:r:untrusted\u app:s0:c129、c256、c512、c768 tcntext=u:object\r:vendor\u default\u prop\u prop:s0
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 audit0.0:425190:avc:denied{read}for name=u:object\u r:vendor\u default\u prop:s0 dev=tmpfs ino=22761 scontext=u:r:untrusted\u app:s0:c129、c256、c512、c768 tcntext=u:object\r:vendor:vendor\u default\u prop\u 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=10129com.example.my_test_app render阅读相同的17行
2019-04-24 14:25:58.252 28031-28057/com.example.my_test_app E/libc:拒绝访问查找属性vendor.debug.egl.swapinterval
.setOnClickListenernull不会分配任何OnClickListener;那么,您将如何调试该事件?而.getText.length可能应该是.getText.toString.length。。。这样就可以满足条件。您没有正确设置侦听器。添加以下代码:
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//add your conditions here
}
更改此行:
findviewbydr.id.btnSave.setOnClickListenernull
进入
findviewbydr.id.btnSave.setOnClickListenerbtnSaveListener
并从onCreate方法中删除以下行
然后创建btnSaveListener,如下所示:
private View.OnClickListener BTNSavelListener=新建View.OnClickListener{
@凌驾
公共视图v{
//下面是您在“onCreate”方法中更改的行下面的所有代码`
}
};
为什么要这样使用click listener:findViewByIdR.id.btnSave.setOnClickListenernull 改为这样做: findViewByIdR.id.btnSave.SetOnClickListener新建视图.OnClickListener{ @凌驾 公共视图v{ //将所有文本视图逻辑放在这里 } };
您的按钮单击侦听器实现错误,请尝试以下操作:
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
}
});
}
}
});
最后,这与我如何设置我的听众无关 将活动调用从my MainActivity启动类更改为StartActivity意味着正确调用了OnCreate代码 你
知道setOnCickListenernull清除侦听器吗?所以,很明显,单击之后将不会有任何操作…如果onCreate中的txtServer.getText.length==0,则也不会使sens。。。它应该在监听器onClick实现after EDIT中:监听器实现不在onCreate中。将所有代码移到那里。@NikosHidalgo将“private View.onClickListener..”移到受保护的void onCreate中,并显示不允许使用修饰符“private”here@NikosHidalgo我已经添加了logcat输出-这提供了任何见解吗?而且.getText.length可能应该是.getText.toString.length,应该没有区别……我已经按照您的建议更改了findViewById行,并从onCreate方法中删除了以下代码的其余部分。我创建了私有视图。onClickListener。。。把我的if语句放在那里。按下按钮时仍然没有结果或调试,尽管将侦听器的声明移到onCreate方法Hi Luca上方,但我已使用更多信息编辑了问题尝试将其写入onClick方法:Toast.makeTextftpDetails.this,try,Toast.LENGTH\u LONG.show并检查您是否看到我没有看到的内容-我认为这是设备问题,而不是代码问题。不会有任何更改here@David是否已删除空侦听器findViewByIdR.id.btnSave.setOnClickListenernull;然后在onCreate中添加了上面的代码?是的,我只有super.onCreate、setContextView、setTitle、TextView声明,然后是findViewByIdR.id.btnSave.setOnClickListenerBTNSavelListener@David删除您在评论中提到的最后一行,其中设置了不同的侦听器。本质上,这是设置同一侦听器的另一种方式,但正如您所拥有的一样,它缺少一些代码。这里所有的答案都非常正确,但措辞不同。确保你坚持一个,它应该会起作用!对不起,我解释得很糟糕。查看我当前拥有的内容的编辑。我也试过你的方法,但没有改变结果
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//add your conditions here
}
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
}
});
}
}
});