Android Studio: Application crashes at start - java

my application crashes at start and I have no idea why. I'm testing it on a real device (Samsung Galaxy S3 GT-I9300). Here is the exception:
EDIT:
So, that's what I've added to onCreate():
walking.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
travel_mode = "walking";
walking.setImageResource(R.drawable.walking_sel);
biking.setImageResource(R.drawable.regular_biking);
driving.setImageResource(R.drawable.car_icon);
transit.setImageResource(R.drawable.transit_icon);
}
});
biking.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
travel_mode = "biking";
walking.setImageResource(R.drawable.walking);
biking.setImageResource(R.drawable.regular_biking_sel);
driving.setImageResource(R.drawable.car_icon);
transit.setImageResource(R.drawable.transit_icon);
}
});
Before I've added this piece of code, everything worked correctly.
And here is AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lukssoftware.kaart" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-library android:name="com.google.android.maps" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="***" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="lukssoftware.kaart.kaart_welcome"
android:screenOrientation="portrait"
android:label="kaart" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Any ideas?
Thanks.

Please post more code and there is nothing to do with manifest here post layout xml and activity code where you have defined walking and biking findViews().
Just check it out that you have defined findViewsById of both walking and biking before you are calling setOnClickListener method.

Does your application still compile successfully after deleting the two modules?
If it doesn't compile, look for the lines in your classes or xml files which have yellow or red exclamation marks. That should alert you to where the problem is coming from.
You should also take a good look at line 64 in your *welcome.java class.

Related

OnActivityResult always returns RESULT CANCELED (Code: 0) on Android 11

Sadly, Android 11 storage restrictions are too hard and confusing.
I'm trying to pick a document using the below code
fun onFile(activity: AppCompatActivity?, fragmentContext: Fragment?, isMultiple: Boolean?, pFileMimeTypes: Array<String>?): Intent {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
action = Intent.ACTION_OPEN_DOCUMENT
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, isMultiple ?: false)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
val fileMimeTypes = pFileMimeTypes ?: FileUtil.mimeTypes
intent.type = if (fileMimeTypes.size == 1) fileMimeTypes[0] else "*/*"
if (!fileMimeTypes.isNullOrEmpty()) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, fileMimeTypes)
}
val list = activity?.packageManager?.queryIntentActivities(intent, PackageManager.MATCH_ALL)
if (list?.size!! > 0) {
if (fragmentContext == null) {
activity.startActivityForResult(
Intent.createChooser(
intent,
activity.getString(R.string.m_selectFile_txt)
), FILE_CODE
)
} else {
fragmentContext.startActivityForResult(
Intent.createChooser(
intent,
activity.getString(R.string.m_selectFile_txt)
), FILE_CODE
)
}
}
// returning intent is not being used anywhere else
return intent
}
Although I have all files access permission Environment.isExternalStorageManager() but still after selecting document(PDF) from the external storage (download or any folder), OnActvityResult is being called with result code 0. I need the Uri of the selected document to proceed further.
For any other reference, below is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="*****Hidden*******">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_STORAGE_PERMISSION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:exported="true"
android:name=".Main2Activity"
android:launchMode="singleTask"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.attachmentmanager"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider"
tools:replace="android:resource" />
</provider>
</application>
<queries>
<intent>
<action android:name="android.intent.action.OPEN_DOCUMENT" />
<!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
<data android:mimeType="*/*" />
</intent>
<intent>
<action android:name="android.intent.action.PICK" />
<!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
<data android:mimeType="*/*" />
</intent>
</queries>
</manifest>
I must be missing some point. Everything is working fine below Android 11
Thanks in Advance.
startActivityForResult is deprecated
Refer this link
Use Activity Results API
Official Docs

Restart service on device reboot

I have a program with a service and if I reboot my device also the service should Restart. But this only works on the Emulator if I try this on my real device the service doesn't start at all.
Does someone know what I'm doing wrong or why it only works on Emulator?
BroadcastReviever:
#Override
public void onReceive(Context context, Intent intent) {
//we double check here for only boot complete event
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
{
//here we start the service
Intent serviceIntent = new Intent(context, UploadService.class);
context.startService(serviceIntent);
}
}
Manifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS_PRIVILEGED"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Upload FTP"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name=".BootCompletedIntentReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter >
</activity>
<service
android:name=".UploadService"
android:isolatedProcess="false"
android:exported="true"
android:enabled="true"/>
</application>
The emulator is at API 23 and real device is API 27.
I'm Building for a min API Level 23 and max API Level 27
EDIT
Now I have also tried the program with a emulator and android API 27 and there when I start my program and then I restart the emulator, the emulator doesn't start any more. As soon as the emulaor has started he begins to reboot again and that in a endless loop. (Real device starts normal just doesn't restarts service)
try to use LOCKED_BOOT_COMPLETED receiver as follow:
<receiver
android:name=".BootReceiver"
android:directBootAware="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
<!-- For pre-N devices -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
don't forget permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
take a look here for more explanation
Now I fixed in on my own it was easier then i thought
public class BootCompletedIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i=new Intent(context, YourClass.class);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
context.startForegroundService(i);
}
else {
context.startService(i);
}
}
I had some weird experience if I don't put full URI,
for example .BootCompletedIntentReceiver would become com.company.BootCompletedIntentRecevier.
I know it sounds dumb but I had many weird regressions when not explicitly stating things on the Manifest with modern SDKs.

Android Exception NameNotFoundException

I don't understand why happens this exception.
In my Main activity, in the method onCreate I have this code at first:
try {
PackageInfo info = this.getPackageManager().getPackageInfo("com.mypacket", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.e("Hash", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
Log.e("Hash", "Error: NameNotFoundException");
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
Log.e("Hash", "Error: NoSuchAlgorithmException");
}
The exception is NameNotFoundException, I want to solve it because I think it causes problem with the Facebook SDK.
Thank you all.
If helps, this is my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myPackageName.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<supports-screens android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<permission android:name="com.myPackageName.app.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.myPackageName.app.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.myPackageName.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.myPackageName.app.permission.C2D_MESSAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:name="#string/app_name"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/app_id"/>
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.myPackageName.app" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
<activity
android:name="com.myPackageName.app.SplashScreen"
android:screenOrientation="portrait"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Google Maps V2 API KEY -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="apy key" />
</application>
</manifest>
You got your package name wrong. For safety, always use Context.getPackageName().
PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_SIGNATURES);
Your package name is com.myPackageName.app not com.mypacket.
BTW you can obtain package name by Context#getPackageName()
This issue occurs when you have generate hash key project and the application for which you are creating hash key are not on the some workspace / directory, so exception arises NameNotFoundException.
Cheers :)
It means that either you are giving wrong package name or maybe you are not sure about the package name.
Case 1. if you know about your package name, just cross check it.
Case 2. if you are not sure about the package name ( this situation may also arise when you did some code refactoring i.e. Rename Package )
There are multiple approaches to fix it
package name is mentioned in the top of the AndroidManifest.xml
If it is still not resolved to get package name by
Context.getPackageName();
or
Log.d();
In Logcat check the application column package name should be mentioned there.
This can also happen if you have the app already installed. If for example you were to debug your app and then test the release .apk on the same device without uninstalling the debug version. If that is the case, just uninstall and then the install should work.
Uninstall your app from your device then run it again through the android studio. It helps me

Android Bluetooth_admin permission error

Regarding Android bluetooth discovery.
Getting this error from manifest permission error. However, the only errors that I found posted, was that they forgot to put the permission outside the application. This is however not the case here.
The program is tested on a Galaxy S2 running Jelly Bean 4.1.
The error is as follows:
03-19 13:08:03.933: W/dalvikvm(12616): threadid=1: thread exiting with uncaught exception (group=0x413082a0)
03-19 13:08:03.938: E/AndroidRuntime(12616): FATAL EXCEPTION: main
03-19 13:08:03.938: E/AndroidRuntime(12616): java.lang.SecurityException: Need BLUETOOTH_ADMIN permission: Neither user 10004 nor current process has android.permission.BLUETOOTH_ADMIN.
Basically the manifest looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.motioncontrol"
android:versionCode="1"
android:versionName="1.0" >
<user-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:debuggable="true"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.motioncontrol.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AccelerometerDat"
android:label="#string/accelerator_title" />
<activity
android:name=".GyroscopeData" />
<activity
android:name=".Gravitydat" />
<activity
android:name=".Linearacc" />
<activity
android:name=".Bluetooth" />
<activity
android:name=".CheckBluetooth" />
<activity
android:name=".About"
android:label="#string/about_title"
android:theme="#android:style/Theme.Dialog" />
</application>
</manifest>
And the class where the error occurs:
private void doDiscovery() {
if (D) Log.d(TAG, "doDiscovery()");
// Indicate scanning in the title
setProgressBarIndeterminateVisibility(true);
setTitle(R.string.scanning);
Log.d(TAG, "indicates scanning");
// Turn on sub-title for new devices
findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
Log.d(TAG, "Set up sub-title for new devices");
// If we're already discovering, stop it
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
Log.i(TAG, "Already running discovering, stop it!");
}
// Request discover from BluetoothAdapter
mBtAdapter.startDiscovery();
Log.d(TAG, "Request discover form BluetoothAdapter");
}
Hope for an answer, what is going wrong :).
Thanks!
Change
<user-permission android:name="android.permission.BLUETOOTH_ADMIN" />
to
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<user-permission> is used to regulate the access to the current component by other application. You should try <uses-permission> to get the privilege.

google map does not show up

I'm trying to display the google map for an android project. I followed all the steps without any problems, but when i launch my application, the map simply doesn't show up, i just have a white screen.
This is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.map.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.map.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="MyKeyXXXXXXX" />
<activity
android:name="com.map.HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.map.MapActivity"
android:label="#string/title_activity_map" >
</activity>
</application>
</manifest>
Does anyone see something wrong ?
thanks in advance
Here are the steps you should follow:
Grab the Google Play services SDK (It's where the Maps API is)
Get the API key
Set the key in the Application Manifest
Last but not least, don't forget that Google Maps will only show if your app is signed with the key you provided to get the Maps key.
For example, if you've used a production certificate and you're testing the app with the debug key, you won't be able to see the maps.
I had this problem too. Your API key is not the giant series of two characters. On the Google Maps API console, after you have created your project and enabled Google Maps, you need to go to the API Access page, and click on "Create a new Android Key" if you don't have one already.
Your API Key that you use in your app is the ~40 character string that appears on the new key. Paste that into the Manifest and it should work.

Categories

Resources