【问题标题】:App crashes when pressing a button without reaching click event按下按钮而不达到点击事件时应用程序崩溃
【发布时间】:2019-09-12 11:03:12
【问题描述】:

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

这是我的按钮

设置onClick 属性

表单的类代码

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: type=1400 audit(0.0:1269): 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=文件许可=0 D/AndroidRuntime:关闭虚拟机 W/RenderThread: type=1400 audit(0.0:1270): 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=文件许可=0 E/libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval” W/RenderThread: type=1400 audit(0.0:1271): 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=文件许可=0 E/libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval” W/RenderThread: type=1400 audit(0.0:1272): 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=文件许可=0 E/libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval” E/AndroidRuntime: 致命异常: main 进程:com.example.my_test_app,PID:8293 java.lang.IllegalStateException:无法在父或祖先上下文中找到方法 saveFTPSettings(View) 为 android:onClick 属性定义在视图类 android.support.v7.widget.AppCompatButton 与 id '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: type=1400 audit(0.0:1273): 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=文件许可=0 E/libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval” I/chatty: 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/chatty: uid=10129(com.example.my_test_app) RenderThread 相同的 3 行 E/libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval” D/OSTracker:操作系统事件:崩溃 I/Process:发送信号。 PID:8293 SIG:9 与目标VM断开连接,地址:'localhost:8600',传输:'socket'

为什么当我按下按钮时我的应用程序会关闭?我可以很好地输入文本框,只是按下按钮似乎可以关闭它。

【问题讨论】:

  • java.lang.IllegalStateException: Could not find method saveFTPSettings(View) in a parent or ancestor Context for android:onClick attribute defined on view class 。打开视图的 XML(是的,即使在使用 GUI 时也有)并检查那里是否有任何错误
  • @ZUNJAE 没有任何错误,但我已更改我的代码以匹配此建议stackoverflow.com/a/15994591/6530111 - 应用程序不再崩溃,但按钮单击事件似乎没有触发

标签: java android debugging


【解决方案1】:

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

改变

<Button
        .... />

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

【讨论】:

  • 我已按照您的建议更改了 XML 代码,但没有更改任何其他内容(例如按钮、无效代码、属性等),但同样的事情发生了,应用程序在我按下了它。
【解决方案2】:

确保函数像这样 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();
            }
        });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多