Android: Unable to set getContext() in non-static method - requires API Level 23 - android-studio

Below, is some source code which is being called from a non-static method. I'm getting the error
"Call requires API level 23 (current min is 15);
android.app.Fragment#getContext"
android.content.Context context = (Context) getContext();
How can I get the context object?
Under the object explorer > Gradle Scripts > build.gradle, I see this. Looks like version 23 to me. Am I looking in the correct location?
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.test.test"
minSdkVersion 15
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.2.1'
compile 'com.android.support:support-v4:23.2.1'
}
Looks like I have Android Studio 1.5.1. I just chose the SettingsActivity project template in Android Studio. And added one class to that default code.

You need to import import android.support.v4.app.Fragment instead of import android.support.app.Fragment;

Pass getActivity() instead of getContext() as Context.

One way to solve was to cast "this" (activity) to (Context) inside the Activity onCreate method.

Related

Cannot resolve android annotation for sharedPreferences

I imported this project from eclipse. everything is sorted out but there is this one issue I'm stuck with.
I've a sharedPreference interface which is done using annotations
import com.googlecode.androidannotations.annotations.sharedpreferences.DefaultBoolean;
import com.googlecode.androidannotations.annotations.sharedpreferences.DefaultString;
import org.androidannotations.annotations.sharedpreferences.SharedPref;
#SharedPref
public interface RememberMePrefs {
#DefaultString("")
String email();
#DefaultString("")
String password();
#DefaultBoolean(true)
boolean enabled();
}
when I try to use this interface like this :
#Pref
RememberMePrefs_ rememberMePrefs;
it says "Cannot resolve symbol RememberMePrefs_"
this is my Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "xxxxxx"
minSdkVersion 16
targetSdkVersion 29
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
def AAVersion = '4.6.0'
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
apt "org.androidannotations:androidannotations:$AAVersion"
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
implementation "org.androidannotations:androidannotations-api:$AAVersion"
implementation files('libs/android-support-v4.jar')
implementation files('libs/androidannotations-api-2.7.1.jar')
implementation files('libs/gson-2.2.2.jar')
implementation files('libs/saripaar-11242012.jar')
implementation files('libs/spring-android-core-1.0.1.RELEASE.jar')
implementation files('libs/spring-android-rest-template-1.0.1.RELEASE.jar')
implementation files('libs/actionbarsherlock.jar')
}
Any help will be appriciated as I've never worked on annotations before.
You are using the latest AndroidAnnotations library in your build.gradle, but importing the old namespace in your Java code.
You should import:
import org.androidannotations.annotations.sharedpreferences.DefaultBoolean;
import org.androidannotations.annotations.sharedpreferences.DefaultString;
import org.androidannotations.annotations.sharedpreferences.SharedPref;
This should be done in other classes as well, probably.

How change Android Studio project api version to 23

I use Android Studio3.2.
I create a project use api version 28.
Now I need change api version to 23.
So I edit build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
defaultConfig {
applicationId "com.example.administrator.myapplication"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}
And I change "AppCompatActivity" to "Activity"
But building faild:
Android resource linking failed
Output: error: resource android:style/TextAppearance.Material.Widget.Button.Borderless.Colored not found.
error: resource android:style/TextAppearance.Material.Widget.Button.Colored not found.
.............................
Try to look inside you XML for a view that has style type mentioned in that error.
Also, try to go File/ InvalidateCaches
compileSdkVersion and targetSdkVersion should match and apart from that
you have to include the following support library to support api 23
implementation 'com.android.support:appcompat-v7:23.0.0'
then now sync project and check.
Hope this will help

Error: Annotation processors must be explicitly declared now

I got an error in the execution of the app, how can I solve this?
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.google.code.gson:gson:2.8.0'
api 'com.google.firebase:firebase-messaging:11.8.0'
api 'com.google.android.gms:play-services-maps:11.8.0'
api 'com.google.android.gms:play-services-location:11.8.0'
implementation 'de.hdodenhof:circleimageview:2.0.0'
api 'com.android.support:design:27.1.1'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'
implementation 'com.github.bumptech.glide:glide:4.5.0'
implementation 'com.android.support:cardview-v7:27.1.1'
api project(':mobicomkit')//Note: use this for customization
//api 'com.applozic.communication.message:mobicomkit:5.8.2'
implementation 'com.daprlabs.aaron:cardstack:0.3.1-beta0'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.android.support.constraint:constraintlayout:1.1.2'
compile 'ai.api:sdk:2.0.7#aar'
compile 'ai.api:libai:1.4.8'
}
I got the error below:
Annotation processors must be explicitly declared now. The following
dependencies on the compile classpath are found to contain annotation
processor. Please add them to the annotationProcessor configuration.
- log4j-core-2.2.jar (org.apache.logging.log4j:log4j-core:2.2)
Add the following line in dependencies.
annotationProcessor 'org.apache.logging.log4j:log4j-core:2.2'
For more information: enter link description here

Design view not getting displayed on Android Studio

I just imported a project, but when i try to enter design view this shows up:
My current gradle version is the latest 26.0.2 by the way. I'm not sure what to do, anyone have an idea?
Here are some Gradle Scripts:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion
defaultConfig {
applicationId "com.example.Ron.myapp"
minSdkVersion 15
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.android.support:design:23.1.1'
}
and
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I solved the problem myself. I upgraded the compileSdkVersion aswell as the minSdkVersion along with the dependencies. It now looks like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion
defaultConfig {
applicationId "com.example.Ron.myapp"
minSdkVersion 18
targetSdkVersion 26
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:26.0.2'
compile 'com.android.support:design:26.0.2'
compile 'com.android.support:cardview-v7:26.0.2'
}
please update your support lib and compileSdkVersion like below.
compileSdkVersion = 25
buildToolsVersion = "25.0.2"
minSdkVersion = 18
// Library versions
supportLibraryVersion = "25.0.0"
also add ConstraintLauout gradle in gradle file.
compile 'com.android.support.constraint:constraint-layout:1.0.2'
For More details please check : https://stackoverflow.com/a/45115111/1343788
known problem
just add
compile 'com.android.support.constraint:constraint-layout:1.0.2'
in your gradle file,
it will fix it

Error:(24, 0) Project with path ':wear' could not be found in project ':mobile'. Android Studio

I got the following error in Android Studio while creating project...
Error:(24, 0) Project with path ':wear' could not be found in project ':mobile'.
This is my build.gradle file code, I want to resolve this issue. Any help appreciated...
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.user.usereventdemo"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
wearApp project(':wear')
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
Looking at your settings.gradle, is it including :wear,?
if you don't want codes of wear, then delete :wear, and wearApp project(':wear') in your build.gradle.
if you want, then you have to find codes of wear, I'm afraid.
Under Gradle Scripts folder in Android Studio, locate "settings.gradle".
Open it and add ':wear' to the end of include statement.
It should look something like this:
include ':mobile', ':glass', ':wear'

Resources