当前位置: 首页 > 知识库问答 >
问题:

当按下按钮而没有到达单击事件时,应用程序崩溃

岳正浩
2023-03-14

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

这是我的纽扣

设置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);

}

E/libc:拒绝访问查找属性“vendor.debug.egl.swapinterval”W/renderthread:type=1400审核(0.0:1269):avc:拒绝{read}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}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:s0tclass=file permissive=0 e/libc:拒绝访问查找属性“vendor.debug.egl.swapinterval”w/renderthread:type=1400审核(0.0:1271):avc:拒绝{read}for scontext=u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext=u:object_r:vendor_default_prop:s0tclass=file permissive=0 e/libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval”w/renderthread:type=1400审核(0.0:1272):avc:拒绝{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 e/libc:访问拒绝查找属性“vendor.debug.egl.swapinterval”e在Android的父或祖先上下文中找不到方法saveFTPSettings(View):在Android.support.v7.app.appcompatviewinflater$DeclaredonClickListener.ResolveMethod(AppCompatviewWinflater$DeclaredonClickLister.java:424)在Android.support.v7.app.appCompatviewWinflater$DeclaredonClickListener.onClick(AppCompatviewWinflater$DeclaredonClickLister.java:381)在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.app.activityThread.loop(looper.java:193)在Android.app.activityThread.main(activityThread.java:6863)在java.lang.reflect.methode(原生方法)在ygoteinit.main(zygoteinit.java:858)w/RenderThread:type=1400 audit(0.0:1273):avc:拒绝{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:s0tclass=file permissive=0 e/libc:访问test_app)呈现线程相同的1行e/libc:拒绝访问查找属性“vendor.debug.egl.swapinterval”w/opdiagnose:getservice:opdiagnoseservice NULL e/libc:Access拒绝查找属性“vendor.debug.egl.swapinterval”I/chatty:uid=10129(com.example.my_test_app)呈现线程相同的3行E/libc:Access拒绝查找属性“vendor.debug.egl.swapinterval”D/ostracker:OS事件:崩溃I/process:发送信号。PID:8293SIG:9与目标VM断开连接,地址:'localhost:8600',传输:'socket'

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

共有1个答案

严狐若
2023-03-14

确保函数像这样开始saveFTPSettings(),并在函数中放置一个日志,这样一旦进入log-cat就可以获得它。

请尝试下面的代码,而不是在onClick of properties上指定它。

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

}
 类似资料: