按下按钮而未达到点击事件时,应用会崩溃

时间:2019-04-23 09:30:39

标签: java android debugging

在我的Android Studio应用中,我设计了一个屏幕(不是使用XML,而是使用设计模式)。我已经将一个按钮的属性设置为类中的public method

这是我的按钮

设置onClick属性

enter image description here

表单的类代码

package com.example.my_test_app;

import android.content.ContentValues;
import android.content.DialogInterface;
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 {

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

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

public void saveFTPSettings(View v) {

    // Set controls
    Button btn = (Button) v;
    TextView txtServer = (TextView) findViewById(R.id.txtServer);
    TextView txtFolder = (TextView) findViewById(R.id.txtFolder);
    TextView txtUsername = (TextView) findViewById(R.id.txtUsername);
    TextView txtPassword = (TextView) findViewById(R.id.txtPassword);

    // 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
            }
        });
    }



    //MyDBHandler dbHandler = new MyDBHandler();
    //String _filePath = this.getFilesDir().toString() + "/";

    //SQLiteDatabase _db = SQLiteDatabase.openDatabase(_filePath + dbHandler.DATABASE_NAME, null, 0);

    //ContentValues initialValues = new ContentValues(4);
    //initialValues.put(dbHandler.COLUMN_FTP_SERVER, txtServer.getText().toString());
    //initialValues.put(dbHandler.COLUMN_FTP_FOLDER, txtFolder.getText().toString());
    //initialValues.put(dbHandler.COLUMN_FTP_USERNAME, txtUsername.getText().toString());
    //initialValues.put(dbHandler.COLUMN_FTP_PASSWORD, txtPassword.getText().toString());

    //_db.insert(dbHandler.TABLE_FTP, null, initialValues);

}

}

我在Button btn = (Button) v上设置了一个断点。但是,当我按下按钮时,不会出现断点,该应用程序仅关闭。

这是我在Android Studio调试窗口中看到的内容

  

E / libc:访问被拒绝找到属性“ vendor.debug.egl.swapinterval”   W / RenderThread:类型= 1400审核(0.0:1269):AVC:拒绝{读取}名称=“ 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 =文件许可= 0   D / AndroidRuntime:关闭VM   W / RenderThread:类型= 1400审核(0.0:1270):AVC:拒绝{读取}名称=“ 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 =文件许可= 0   E / libc:访问被拒绝找到属性“ vendor.debug.egl.swapinterval”   W / RenderThread:类型= 1400审核(0.0:1271):AVC:拒绝{读取}名称=“ 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 =文件许可= 0   E / libc:访问被拒绝找到属性“ vendor.debug.egl.swapinterval”   W / RenderThread:类型= 1400审核(0.0:1272):AVC:拒绝{读取}名称=“ 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 =文件许可= 0   E / libc:访问被拒绝找到属性“ vendor.debug.egl.swapinterval”   E / AndroidRuntime:致命异常:main       流程:com.example.my_test_app,PID:8293       java.lang.IllegalStateException:在父类或祖先上下文中找不到方法saveFTPSettings(View)的android:onView属性,该类在视图类android.support.v7.widget.AppCompatButton上标识为“ btnSave”           在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)           在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)           在android.view.View.performClick(View.java:6669)           在android.view.View.performClickInternal(View.java:6638)           在android.view.View.access $ 3100(View.java:789)           在android.view.View $ PerformClick.run(View.java:26145)           在android.os.Handler.handleCallback(Handler.java:873)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6863)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:537)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)   W / RenderThread:类型= 1400审核(0.0:1273):AVC:拒绝{读取}名称=“ 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 =文件许可= 0   E / libc:访问被拒绝找到属性“ vendor.debug.egl.swapinterval”   I /聊天:uid = 10129(com.example.my_test_app)RenderThread相同1行   E / libc:访问被拒绝找到属性“ vendor.debug.egl.swapinterval”   W / OPDiagnose:getService:OPDiagnoseService NULL   E / libc:访问被拒绝找到属性“ vendor.debug.egl.swapinterval”   I /聊天:uid = 10129(com.example.my_test_app)RenderThread相同的3行   E / libc:访问被拒绝找到属性“ vendor.debug.egl.swapinterval”   D / OSTracker:OS事件:崩溃   I / Process:正在发送信号。 PID:8293 SIG:9   与目标VM断开连接,地址:“ localhost:8600”,传输:“ socket”

为什么当我按下按钮时我的应用程序关闭?我可以在文本框中输入正确的内容,只是按一下按钮似乎可以将其关闭。

2 个答案:

答案 0 :(得分:4)

确保该函数像saveFTPSettings()这样开始,并且还将一个日志放入该函数中,以便一旦进入该函数,就可以将其保存在log-cat中。

尝试下面的代码,而不是在属性的onClick上指定它。

Button btn;

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

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

    btn            = findViewById(R.id.btn_name_in_xml);
    btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveFTPSettings();
            }
        });

}

答案 1 :(得分:0)

我遇到了同样的问题,在我的情况下,我将XML中的Button更改为android.support.v7.widget.AppCompatButton并有效。

更改

<Button
        .... />

<android.support.v7.widget.AppCompatButton
        .... />