-2

My name is Ibrahim. I have been developing since I was 14. I became interested in computers & cybernetics ever since I came to this great land you call America. I have lately been developing an app on my tablet ( using Android IDE or AIDE). Lately I have been working on an app for users who their power button is either stuck or broken. It allows them to shut it off so it can reserve power while charging. After finishing today, I got this error saying "There is no applicable constructor found". I need help. Here is my MainActivity.java code:

package com.mycompany.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
import android.os.PowerManager.*;
import android.content.Context.*;
import android.content.*;
import android.os.PowerManager.*;
import java.text.*;
import java.util.logging.*;
import java.lang.reflect.*;


public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button shutdown = (Button) findViewById(R.id.Shut_Down);

        shutdown.setOnClickListener(new OnClickListener() {
             PowerManager pm =new PowerManager();

                @Override
                public void onClick(View p1)
                {
                    // will shut off android


                }



        });




                }
                }

And here is my main.xml page:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
        android:text="shutdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Shut_Down"/>
    <Button
    android:text="reboot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/Reboot"/>

</LinearLayout>

and here is my AndroidManifest.xml code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
laalto
  • 150,114
  • 66
  • 286
  • 303

2 Answers2

2

Use

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

to get a reference to PowerManager. You cannot instantiate it yourself with new.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • I uused that one, but it turns out I replaced (Context.POWER_SERVICE) with (Context.Powermanager) by accident. This one works. Thank you :) – MCOM_Android Feb 14 '15 at 19:32
  • I tried to compile it, but when i did, the app crashed as i pressed the button. Any suggestions? – MCOM_Android Feb 14 '15 at 19:34
  • Start with the stacktrace. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this I have no experience with AIDE but if you can't get to see the logcat with it, it's of little use. – laalto Feb 14 '15 at 19:36
1

My name is Ibrahim. I have been developing since I was 14. I became interested in computers & cybernetics ever since I came to this great land you call America.

Nice to meet you, mate XD

Use

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

As the other guy said, but I dont think you can shut the device down with that class, only reboot it, for which you also need to set up the permission in the manifest

CyborgFish
  • 356
  • 6
  • 13