Error with "Method"

3

I'm trying to run a button and it accuses the following error:

  

java.lang.IllegalStateException: Could not find method java.view.Tools   (call_disconnect) (View) in a parent or ancestor Context for   android: onClick attribute defined on view class android.widget.Button   with id 'btn off'

res > layout > Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="view.Tools">

    <Button
        android:text="Desligar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/btnDesligar"
        android:layout_alignParentEnd="true"
        android:onClick="java.view.Tools (call_Desligar)" />

    <Button
        android:text="Reiniciar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnReiniciar"
        android:layout_below="@+id/btnDesligar"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:onClick="call_Reiniciar" />

    <Button
        android:text="Sair"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnReiniciar"
        android:layout_alignParentStart="true"
        android:id="@+id/btnSair"
        android:layout_alignParentEnd="true"
        android:onClick="call_Sair" />

</RelativeLayout>

java > view > tools

package view;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.seradmin.apptools.R;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

public class Tools extends Activity {

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    public void onStart() {

        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {

        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Main);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */

    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    public void call_Desligar(Tools tools) {

        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot -p"});
            proc.waitFor();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void call_Reiniciar(Tools tools) {

        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot"});
            proc.waitFor();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void call_Sair(Tools tools) {
        // Vázio
    }

}

OS: Windows 8.1 (x64)

App: Android Studio

    
asked by anonymous 14.12.2016 / 16:32

3 answers

4

As the response from @Borge B. , I believe there is another mistake besides this!

According to the documentation :

  

For this to work, the method must be public and accept a View as its only parameter.

Then try the following:

package view;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.seradmin.apptools.R;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
// add View
import android.view.View;

public class Tools extends Activity {


     public void call_Reiniciar(View tools) {}
     public void call_Desligar(View tools){}
     public void call_Sair(View tools){}


}
    
14.12.2016 / 16:44
3

The error is in the XML of the first button "Hang up":

android:onClick="java.view.Tools (call_Desligar)"

should be

android:onClick="call_Desligar"

If you analyze the error well it says it all:

  

java.lang.IllegalStateException: Could not find method java.view.Tools   (call_disconnect) (View) in a parent or ancestor Context for    android: onClick attribute defined on view class android.widget.Button    with id 'btn off'

    
14.12.2016 / 16:35
2

Try using this code:

The problem is in the onClick, you are defined as: "java.view.Tools (call_Turn)" ie you are leaving spaces in the text.

Please comment if you have solved your problem

    
14.12.2016 / 16:41