"There is no applicable constructor? error appears when making app - java

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>

Use
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
to get a reference to PowerManager. You cannot instantiate it yourself with new.

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

Related

How can I remove the Android application label?

I made my first app today on my phone using an application named "A.I.D.E".
How can I remove my app name and label from the top of the main screen? I named my app "YT Studio", but I don’t want to show this name and label on top of my main screen. I want it to be blank. How can I do it?
I provided the android manifest.xml and main.java file for the reference.
File android manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ojhastudios.ytstudio" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="YT Studio"
android:theme="#style/AppTheme"
android:resizeableActivity = "true">
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
File main.xml
<LinearLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</LinearLayout>
The code:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getSupportActionBar().hide();
}
}
You can just call
getSupportActionBar().hide();
in your activity's onCreate() method.
The method given above does not work when using an Android 10 activity. It works with AppCompatActivity.
So, this answer will do the task.
In your AndroidManifest.xml, find your activity and then you should add this line in it:
<application>
...
<activity
android:name=".MainActivity"
android:theme="#style/Theme.MaterialComponents.Light.NoActionBar">
...
</activity>
...
</application>
Go to src/main/res/values and open file theme.xml:
Then change your style name to NoActionBar. It removes your action bar:
Go to res → values → themes
<style name="Theme.MyAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
...
</style>
Change it to Theme.MaterialComponents.Light.NoActionBar. The magic here is the NoActionBar and your toolbar will be removed completely, including the app title name. But you should know this, after you change it, it’s going to be removed from your entire project, not just the single activity.

Starting new activity using onOptionsItemSelected with Kotlin

I'm a newbie trying to learn Android Studio using Kotlin. My onOptionsItemSelected() is responding to the activity I set it to. Not sure what I'm doing wrong. Also, is onOptionsItemSelected the best way to go about having a responsive navigation bar?
I've tried implementing several options suggested by Stack Overflow, but most of them are either several years old or didn't work on my case.
Starting new activity from onOptionsItemSelected
How to implement a onMenuItemClickListener in android
These two threads are two of many examples that I've tried to use.
First Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.graphics.Color
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Button
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_second2.*
import java.util.*
import kotlin.collections.ArrayList
class SinglePlayerActivity1 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second2)
supportActionBar?.hide()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.nav_menu2, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId){
R.id.ic_home -> {
startActivity(Intent( this, MainActivity::class.java))
true
}
R.id.ic_people -> {
startActivity(Intent( this, TwoPlayerActivity1::class.java))
true
}
else -> super.onOptionsItemSelected(item)
}
}
nav_menu2.xml (menu)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ic_home"
android:icon="#drawable/ic_baseline_home_24"
android:title="Home"
tools:ignore="HardcodedText" />
<item
android:id="#+id/ic_people"
android:icon="#drawable/ic_baseline_people_24"
android:title="Two Players"
tools:ignore="HardcodedText" />
</menu>
activity_second2.xml (layout)
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="420dp"
android:layout_height="60dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.555"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/nav_menu2" />
Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.TicTacToe">
<activity android:name=".TwoPlayerActivity1" />
<activity android:name=".SinglePlayerActivity1" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Two items (home and two players) from the menu aren't responsive.
You're calling supportActionBar?.hide() which makes me think you're not using the Activity's built-in ActionBar. Are you using a Toolbar? You need to call setActionBar(toolbar) or actionBar = toolbar (Kotlin) if you want it to hook it up to the onCreateOptionsMenu and onOptionsItemSelected callbacks.
Edit:
You're using BottomNavigationView, so in that case you probably don't want to use Toolbar or the onOptionsItem system at all, and use OnNavigationItemSelectedListener instead. Check this out for more info: https://material.io/components/bottom-navigation/android#using-bottom-navigation

How can I read Files from Inter or External Storage (SD Card) in app using android studio?

I am making a file manager app, I started with reading files of the storage by using the following code but the App crashes on opening I dont know why, I didnt find any error in it. Please give any helpful suggestion.
Manifest -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="read.files"
android:installLocation="auto">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Main Activity XML -
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
</androidx.constraintlayout.widget.ConstraintLayout>
And JAVA -
package read.files;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ArrayAdapter;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ListActivity {
private List<String> fileList = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
ListDir(root);
}
void ListDir(File f) {
File[] files = f.listFiles();
fileList.clear();
for (File file : files){
fileList.add(file.getPath());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}
}
Actually I saw this from a youtube video I did all the thing correctly but I dont know the app opens for a second and crashes. I tried many other methods also but nothing helped......
EDIT
Got my mistake at fileList.add(file.getPath());
To fileList.add(file.getName());
Try adding this line to the Android Manifest File
android:requestLegacyExternalStorage="true"
This is used to read Shared Storage apart from the app's private storage. Read this if you are curious to know more...
Hope this helps. Feel free to ask for clarifications...
Edit: As #blackapps mentioned, this is necessary for Android Q and up.
So add this tools:targetApi="q" in AndroidManifest

Android file crashes on emulator and on physical device while requesting URL from Google Images

I know many of you will say "check your settings when creating the project," but I did. I checked and my physical device (Samsung S3) uses Android 4.3 and I set the minimum requirement to 2.2 and target to 4.3. I think it might be my code because I've been able to run other Android applications using the eclipse ADT emulator I just never tried it on an actual device and it just doesn't even show up in my emulator.
Here is my java code:
package com.example.bitmapdisplay;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
URL req = new URL("http://i1.cpcache.com/product_zoom/617914535/dickbutt_2_mug.jpg? side=Back&height=250&width=250&padToSquare=true");
ImageView image = (ImageView) findViewById(R.id.ivImage);
Bitmap mIcon_val= BitmapFactory.decodeStream(req.openConnection()
.getInputStream());
image.setImageBitmap(mIcon_val);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here is my XML layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Here is my XML Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bitmapdisplay"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.example.bitmapdisplay.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>
Need to look at the logCat for more info but one problem that i can see in your manifest is that you have intent-filters missing. Inside your application
android.intent.category.MAIN is given to the activity wich is going to launch at first.
Try this:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Add these two lines to your activity tab and this will tell android this is the main starting activity.
whether the message appears in the console messages like this example? :
uploading your.apk onto device '0123456789ABCDEF'
installing your.apk
starting activity

Beginner Android App Crashes

I'm doing a beginner android primer book and the code seems simple enough. However, it cannot run. When I try to run it, eclipse crashes.
Here's my java code:
package com.example.gamebeginner;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button button;
int touchCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button = new Button(this);
button.setText("Touch me!");
button.setOnClickListener(this);
setContentView(button);
}
public void onClick(View v){
touchCount++;
button.setText("Touched me " + touchCount + " times(s)");
}
}
activity.main xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
</RelativeLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gamebeginner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true" >
<activity
android:name="com.example.gamebeginner.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>
I'm on jdk version 1.6. any help would be much appreciated.
Make sure you device is connected and working correctly.
Go to window -> show view -> android -> devices click ok so that you
open the device list. Make sure you see your device listed in the
window.
Make sure that you click on the device so the device is highlighted.
Now go and click on your logcat tab. If you do not have the logcat
tab opened, go to window -> show view -> other -> android -> logcat.
Make sure you DO NOT select the deprecated version.
Also try killing and restarting the adb daemon.
adb kill-server and then adb start-server at a command prompt. If you do not have adb on your path, you will need to reference the full path to adb or do the command within the directory that contains adb.
Disconnecting and reconnecting the device solved the issue.
If your error message (in logcat, right?) is java se binary is not responding, I would venture to say that you are generating the code in the wrong way somehow. Are you using Eclipse or IntelliJ to build your app? What is the project type you are using - Java application, or Android application?
First add setContentView and afer View

Categories

Resources