Why it doesn't crash when invoking a nonexistent api - android

I am running such a code piece on an Android emulator with api level 16:
package darklord.compilesdkversiontest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.test);
int result = linearLayout.getPaddingEnd();
Log.e("Test", result+"");
}
}
The getPaddingEnd is added in api level 17 so I am expecting this to crash on the emulator. There is indeed a warning in the Android Studio however it did compile and runs on the emulator. Furthermore, it even prints out the correct padding value. So why it doesn't throw some runtime expection? Here is the build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "darklord.compilesdkversiontest"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}

Related

error: package android.v7.app does not exist

Hi there stackoverflow community, hope you can help!
Im using udacity to learn android development and I have ran into a problem. there are a few errors that have come up:
error: package android.v7.app does not exist
error: cannot find symbol class AppCompatActivity
error: method does not override or implement a method from a supertype
error: cannot find symbol variable super
error: cannot find symbol method setContentView(int)
error: cannot find symbol class openNumbersList
error: cannot find symbol method startActivity(Intent)
Everything was working fine but I was following a tutorial and noticed that my imports in my Main Activity didn't match with what was on the tutorial so I changed:
"import..."
to
import android.v7.app.AppCompatActivity;
import android.os.Bundle;
also even before this, "AppCompatActivity" was in red:
public class MainActivity extends AppCompatActivity {
Main Activity:
package com.example.android.miwok;
import android.content.Intent;
import android.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
}
public void openNumbersList(View view){
Intent i = new Intent(this, openNumbersList.class);
startActivity(i);
}
}
Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "com.example.android.miwok"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:23.3.0'
implementation 'com.android.support:support-v4:23.3.0'
implementation 'com.android.support:design:23.3.0'
}
hope this helps.
Try your gradle as follow
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.android.miwok"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
}
I think that above code solve your issue
Anyone who faces this problem has to revise the coding of padding and alignment attributes.
The fault is simple.
All you have to do is to be careful with the attributes specifying.

Cannot resolve symbol MathUtils

I started a new project on Android Studio, targeting API 26, and did nothing but to add the following line:
import android.util.MathUtils;
and got the error: Cannot resolve symbol MathUtils
Here's my MainActivity.java:
package com.example.HeyJude.test2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.MathUtils;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And my build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.HeyJude.test2"
minSdkVersion 26
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
I tried using File > Invalidate Caches (with restart), but it didn't help. Any other ideas?
If you search for it, did you mean this?
android.support.v4.math.MathUtils

Can not resolve the symbol FragmentActivity

The Activity shows error while importing the FragmentActivity.
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.widget.TextView;
import java.util.ArrayList;
public class DetailActivity extends FragmentActivity
My build.gradle file
apply plugin: 'android'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 8
targetSdkVersion 23
versionCode 3
versionName "1.0.2"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:appcompat-v7:23.1.0'
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.android:support-v4:r7'
}
At First
Remove Unnecessary
compile 'com.google.android:support-v4:r7' // No Need
Then Clean-Rebuild and Do Gradle Your Project

Error at creating new Fragment on compileSdkVersion 22 on Android App : onAttach() method fails

I just use the menu to create new fragment and by default it's throwing an error with the fragment generated file, I did not add any code. The problem is in the onAttach() method
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
It says: "onAttach(android.app.Activity) in fragment cannot be applied to (android.content.Context)"
My imports on the fragment file:
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
My gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "license.j4ftech.com.mapevents"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'de.hdodenhof:circleimageview:2.0.0'
compile 'com.android.support:support-v4:22.2.0'
}
My gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "license.j4ftech.com.mapevents"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'de.hdodenhof:circleimageview:2.0.0'
compile 'com.android.support:support-v4:22.2.0'
}
How can I solve this issue?
The method onAttach(Context context) was introduced in API level 23. Your compile/target SDK version is 22 which is why the project cannot find the method definition. You can either switch back to using the old deprecated method onAttach(Activity a) or change the appcompat version to the latest (23.1.1) to be able to use the new method.
Use the latest support library:
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
in your gradle file and set compileSdkVersion/targetSdkVersion to 23.

LocationClient cannot resolved symbol in android studio?

Below is my build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.domore.googlelocationdemo"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
//compile 'com.google.android.gms:play-services:8.3.0'
compile 'com.google.android.gms:play-services:6.5.87'
}
Here,Is my MainActivity.java
package com.domore.googlelocationdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
public class MainActivity extends AppCompatActivity implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener{
LocationClien
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onDisconnected() {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
What will i need to do?
Please any one help me to solve out this error?
I was used a LocationRequest class but which parameter i need to set LocationRequeset class constructor?
Its because of the compiled sdk version as LocationClient is deprecated in API23 so you can use new libraries
com.google.android.gms.common.api.GoogleApiClient and
com.google.android.gms.location.LocationRequest.

Categories

Resources