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.
Related
This question already has answers here:
setsupportactionbar() throws error
(6 answers)
Closed 4 years ago.
I use Android studio 3.1.2. I get this error message. How can I solve this Issue?
error: cannot find symbol this.setSupportActionBar(toolbar);
My App build.gradle
apply plugin: 'com.android.application'
compileSdkVersion 27
buildToolsVersion '28.0.0 rc1'
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.XXXX.XXXX"
manifestPlaceholders = [onesignal_app_id : "9dfhdfg-art455-3455-8dfgh45-fghe345340fh45",
// Project number pulled from dashboard, local value is ignored.
onesignal_google_project_number: "REMOTE"]
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
multiDexEnabled true
flavorDimensions "default"
productFlavors {
dev {
// Enable pre-dexing to produce an APK that can be tested on
// Android 5.0+ without the time-consuming DEX build processes.
dimension "default"
}
prod {
// The actual minSdkVersion for the production version.
minSdkVersion 16
dimension "default"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-media-compat:27.1.1'
implementation 'com.android.support:support-core-utils:27.1.1'
implementation 'com.android.support:customtabs:27.1.1'
implementation 'com.android.support:animated-vector-drawable:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.google.android.gms:play-services-ads:15.0.0'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'it.neokree:MaterialTabs:0.11'
implementation files('libs/YouTubeAndroidPlayerApi.jar')
implementation files('libs/picasso-2.4.0.jar')
implementation 'de.hdodenhof:circleimageview:1.3.0'
implementation 'com.onesignal:OneSignal:3.+#aar'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.google.firebase:firebase-core:15.0.0'
implementation 'com.google.android.gms:play-services-analytics:15.0.0'
implementation 'com.android.support:gridlayout-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
}
apply plugin: 'com.google.gms.google-services'
My error Java Page:
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
public class CategoryItem extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.category_item_grid);
toolbar = (Toolbar) this.findViewById(R.id.toolbar);
toolbar.setTitle(Constant.CATEGORY_TITLE);
this.setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
It also shows this error message:
Can not solve methed 'setSupportAcationBar'import
android.support.v7.widget.Toolbar;
How can i solve it?. What is the slovuation?. anyoney one
Use AppCompatActivity instead of ActionBarActivity
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
I have never used any kind of JUnit before,so from what I search there is a good tool called roboelectric.
I try to run a simple test as shown in their website but the assertThat() and shadowOf() are not recognised.
Here is the test
#RunWith(RobolectricTestRunner.class)
#Config(constants = BuildConfig.class)
public class MainActivityTest {
#Test
public void clickingLogin_shouldStartLoginActivity() {
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.login).performClick();
Intent expectedIntent = new Intent(activity, LoginActivity.class);
//This line doesn't work.
assertThat(shadowOf(activity).getNextStartedActivity()).
isEqualTo(expectedIntent);
}
}
And this how I set Roboelectric in gradle.
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "testing.theo.robotutorial"
minSdkVersion 14
targetSdkVersion 24
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:24.0.0'
testCompile "org.robolectric:robolectric:3.0"
}
Any ideas?
Thanks.
Replace with the following assertion will work.
assertTrue(shadowOf(activity).getNextStartedActivity().filterEquals(expectedIntent));
You can refer to answer by Hoisie in Roboelectric github issue #2627 for details.
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'
}
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.