0

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

enter image description here

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/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=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: Access 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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
David
  • 2,298
  • 6
  • 22
  • 56
  • 1
    `java.lang.IllegalStateException: Could not find method saveFTPSettings(View) in a parent or ancestor Context for android:onClick attribute defined on view class `. Open the XML of the View (yes, you have that even when working with the GUI) and check if there are any errors there – Zun Apr 23 '19 at 12:34
  • @ZUNJAE there weren't any errors, but I've changed my code to match this suggestion https://stackoverflow.com/a/15994591/6530111 - the app no longer crashes, but the button click event doesn't seem to fire – David Apr 23 '19 at 13:50

2 Answers2

3

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

}
Ameena Shafeer
  • 626
  • 3
  • 18
0

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
        .... />
Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
  • I've changed the XML code as you suggested, but didn't change anything else (e.g. the button, void code, attributes etc.) but the same thing happened, the app just shut down when I pressed it. – David Apr 23 '19 at 09:57