简体   繁体   中英

App crashes when pressing a button without reaching click event

In my Android Studio app, I've designed a screen (not using XML, but using design mode). I've set the attribute of one of my buttons to be a public method within the class.

This is my button

Setting the onClick attribute

在此处输入图片说明

The form's class code

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

}

}

I have put a breakpoint on Button btn = (Button) v . However, when I press the button, the breakpoint doesn't get hit, the app just closes.

This is what I get in the debug window in Android Studio

E/libc: Access denied finding property "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=file permissive=0 D/AndroidRuntime: Shutting down VM 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=file permissive=0 E/libc: Access denied finding property "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=file permissive=0 E/libc: Access denied finding property "vendor.debug.egl.swapinterval" W/Ren derThread: 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=file permissive=0 E/libc: Access denied finding property "vendor.debug.egl.swapinterval" E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.my_test_app, PID: 8293 java.lang.IllegalStateException: Could not find method saveFTPSettings(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnSave' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381) at android.view.View.performClick(View.java:6669) at android.view.View.performClickInternal(View.java:6638) at android.view.View.access$3100(View.java :789) at android.view.View$PerformClick.run(View.java:26145) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6863) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) at 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=file permissive=0 E/libc: Access denied finding property "vendor.debug.egl.swapinterval" I/chatty: uid=10129(com.example.my_test_app) RenderThread identical 1 line E/libc: Access denied finding property "vendor.debug.egl.swapinterval" W/OPDiagnose: getService:OPDiagnoseService NULL E/libc: Acce ss denied finding property "vendor.debug.egl.swapinterval" I/chatty: uid=10129(com.example.my_test_app) RenderThread identical 3 lines E/libc: Access denied finding property "vendor.debug.egl.swapinterval" D/OSTracker: OS Event: crash I/Process: Sending signal. PID: 8293 SIG: 9 Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

Why is my app closing when I press the button? I can type into the textboxes fine, it's just the button press that seems to close it.

Make sure the function begins like this saveFTPSettings() and also put a log inside the function so you will get it on log-cat once you are inside it.

Try below code instead of specifying it on 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();
            }
        });

}

I had the same problem and in my case, I changed Button in XML to android.support.v7.widget.AppCompatButton and it worked.

change

<Button
        .... />

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM