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
Remove the instruction as Jorge B. spoke but now says that: Cannot resolve Symbol 'View'
– Júnior
@Junior you have to add the missing libraries to
View
.– Jorge B.
I’m new to Android Studio actually with Android... How can I do this inside the file contains only: package view; and other Ipmports...
– Júnior
I edited the repsota there ! includes the import android.view.View;
– Thiago Luiz Domacoski
@Junior Android Studio adds this for you if you do Alt + Enter.
– Jorge B.