Beginner Android App Crashes - java

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

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.

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

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

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

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

Google Maps API v3 shows GRAY BOXES only

New developer but I have spent the past few days trying to resolve this problem of gray boxes showing up instead of a map on Android device.I have read just about everything there is to read about these boxes but the solutions that are working for other people are not working for me.
The api key that I am using comes from keytool in my java JRE 7 folder using the command from bin folder:
keytool -list -alias androiddebugkey -keystore "C:\Users\ben.android\debug.keystore" -storepass android -keypass android -v
.... I believe this is the debug API key that I have read about here as well as other places.
Here is the error I am getting http://imgur.com/IoHMHQc. I believe I have highlighted the "interesting" part which is java.io.IOException: Server returned: 3.
It seems like that means my API key is faulty but I have made multiple new ones and none seem to fix the problem!
I am NOT uploading the application yet so I don't think the "signed/unsigned" exporting matters but that is because I don't understand it completely.
I have tried following multiple tutorials such as one from Lynda.com as well as various youtube tutorials, however they all are Google Maps v2 rather than v3 so that may be the issue. Help would be greatly appreciated and lifesaving!
Here is my main java file:
package com.bentseytlin.gmap2;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends MapActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Here is my xml 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"
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=".MainActivity" >
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/themap"
android:clickable="true"
android:apiKey="AIzaSyB1gqlqGQZCH1TlrDhp5BP9Pm9k4Jm_2co"
/>
</RelativeLayout>
And here is my Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bentseytlin.gmap2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name="com.bentseytlin.gmap2.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>
Thanks again!
Make sure you have followed all the steps in the below link
https://developers.google.com/maps/documentation/android/start. (api v2)
You should refer to google services library project in your map project
Download the Google Play services.Goto Windows. Goto Android Sdk Manager. Choose Google play services under extras. If not installed install the package.
Copy the google-play services_lib library project to your workspace. The library project can be found under the following path.
<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib library project .
Import the library project to your eclipse.
Click File > Import, select Android > Existing Android Code into Workspace, and browse the workspace import the library project. You can check if it is library project. Right click on the library project. Goto properties. Click Android on the left panel. You will see Is Library checked.
To refer to the library project
Right click on you android map project. goto properties. Choose Android. Click Add borwse and add the library project.
Assuming you have the key.
Make sure you have enabled google maps for android in the google api console under the services tab.
Your min sdk is 10. You should use Support Fragment.
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Your activity must extend FragmentActivity
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap();
Make sure you have added support library
Also make sure you imported the below
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;
Add these in manifest file
<permission
android:name="com.bentseytlin.gmap2.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.bentseytlin.gmap2.permission.MAPS_RECEIVE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
Add the key in manifest under application tag
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="key"/>
You are trying to use Google Maps Android API v1, which is deprecated since december 2012 and will not work for new projects since march 2013.
The easiest way to switch to API v2 is by 1) removing all your code related to maps, 2) following instructions here: https://developers.google.com/maps/documentation/android/start.

Categories

Resources