Error with "Method"

Asked

Viewed 62 times

3

I am trying to run a button and it accuses of the following error:

java.lang.Illegalstateexception: Could not find method java.view.Tools (call_Desligar)(View) in a Parent or Ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'btnDesligar'

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

3 answers

4


As reported by @Jorge B. , I believe there’s 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.

So try it this way:

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){}


}
  • Remove the instruction as Jorge B. spoke but now says that: Cannot resolve Symbol 'View'

  • @Junior you have to add the missing libraries to View.

  • I’m new to Android Studio actually with Android... How can I do this inside the file contains only: package view; and other Ipmports...

  • I edited the repsota there ! includes the import android.view.View;

  • 1

    @Junior Android Studio adds this for you if you do Alt + Enter.

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 everything:

java.lang.Illegalstateexception: Could not find method java.view.Tools (call_Desligar)(View) in a Parent or Ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'btnDesligar'

  • It was without "java.view.Tools" and only with call_Desligar it still presented the same error. Thank you!

  • @Junior when it’s like this you have to put as it was, not to confuse things, see Thiago’s answer.

  • @Junior, what’s your mistake now?

  • In: public void call_Desligar(View tools) > ---- shows this error: View Cannot resolve Symbol 'View'.

2

Try to use this code:

The problem is in onClick, you are set to: "java.view.Tools (call_Desligar)" or you are leaving spaces in the text.

Comment if solved your problem

  • As you and Jorge B. have spoken I have done it and as I have presented it here was an attempt to resolve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.