I can't get the context of my activity for some reason. Note - It was working before but now Android Studio shows an error but does not stop my app compiling and running as expected. I've added my code further down but ultimately I think the problem is somewhere else because if I try to get the activity context in a new, empty activity, I get an error.
package com.example.myapp
public class TestActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Context context = this; // Error here
}
}
The error is:
Incompatible types.
Required: android.content.Context
Found: com.example.myapp.TestActivity
This error only happens for this project. My searches for an answer have yielded no positive results. In fact I can't find anything on the exact issue I'm facing.
What I have tried to fix this:
this instead of MainActivity.this - same error as above
getApplicationContext() - cannot resolve method error
getActivity().getApplicationContext() - same error as #2
Clean & Rebuild project / Sync Project with Gradle Files
Restarting Android Studio
Android Studio versions 2.3.3 & 3.0 - same issue
I'm new to Android development so if you have a solution for me, please phrase it as simply as possible. Thanks in advance. Here is my code - I get the error in the onClick method where it says MainActivity.this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the Numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);
// If View is present, set a click listener on that View
if(numbers != null) {
numbers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
startActivity(numbersIntent);
}
});
}
}
}
I found a solution and that was to add google() to my project's build.gradle file like so:
allprojects {
repositories {
jcenter()
google()
}
}
I also deleted the project and cloned it again so it could build from scratch. Not sure whether that helped or not.
Related
Hello I'm starting in Android Studio and I wanted to know how to pass from one activity to another. I've tried different methods which I've seen in youtube tutorials but all of them show me the same error:
Cannot resolve symbol 'activity_menu'.
Does anyone know how to solve it?
Here is my code:
public class Menu extends AppCompatActivity {
Button boton_start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
boton_start=(Button) findViewById(R.id.boton_menu);
boton_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(activity_menu.this,activity_dishes.class);
startActivity(in);
}
});
}
}
The same happens with the other activity, but I suppose that the solution is the same for both.
You've mentioned the layout file names instead of name of the activities
Your code:
Intent in = new Intent(activity_menu.this,activity_dishes.class);
It should be:
Intent in = new Intent(Menu.this,Dishes.class);
Mention the name of the activity, not layout files.
Try getting activity_menu like this: R.layout.activity_menu
This is my login.xml.
This is my java file. There are no red marks in my java file, but it is showing error while compiling.
In the following lines.
textButton = (Button) findViewById(R.id.TextButton);
fingerprintButton = (Button) findViewById(R.id.FingerprintButton);
I keep getting this error:
Error:(52, 48) error: cannot find symbol variable TextButton
Error:(53, 55) error: cannot find symbol variable FingerprintButton
I have declared these vairables and included the in string.xml as well. I am new to android studio. Please help me.
emmm...here you want to access IDs of Button right? In Android arch, ids are defined in R.java and no need to edit anything because Android Studio would generate automatically. Things you need to do is simply add your id in your layuot.xml, with a xml tag names android:id='#+id/textButton' under a Button tag. Not String.xml.
Rename the buttons as text_button and fingerprint_button in your login.xml. Then make the changes accordingly in your LoginActivity.java like the following.
textButton = (Button) findViewById(R.id.text_button);
fingerprintButton = (Button) findViewById(R.id.fingerprint_button);
Clean and rebuild your application.
Try this :-
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
public Button textButton, fingerPrintButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
textButton = (Button) findViewById(R.id.textButton);
fingerPrintButton = (Button) findViewById(R.id.fingerPrintButton);
textButton.setOnClickListener(this);
fingerPrintButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v == textButton)
{
//Your Custom Code
}
else if(v == fingerPrintButton)
{
//Your Custom Code
}
}
This is my first app and I'm having some trouble.
When I run the app it crashes and I don't know how to fix this error.
public class MainActivity extends AppCompatActivity {\
TextView outputBottom = (TextView)findViewById(R.id.output);
}
public void play_the_big_lie(View view) {
the_big_lie.start();
outputBottom.setText("ObamaCare, the big lie");
}
String literal in setText cannot be translated
This is not an error and you can safely ignore it (unless you need translation in your app).
It is simply a notification by Android Studio that you should be using a string resource file instead of a hard-coded string.
If I had to guess at the source of the issue since you did not post the logcat, it is this.
You can't use findViewById before setContentView because there is no view to "find".
Please try something like the following code
public class MainActivity extends AppCompatActivity {
private TextView outputBottom;
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
outputBottom = (TextView)findViewById(R.id.output);
}
There are two issues here. First, you can't use findViewById until you have "created" things and have a view to find things with, so as the previous answer you want to separate them.
public class MainActivity extends AppCompatActivity {
private TextView outputBottom;
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
outputBottom = (TextView)findViewById(R.id.output);
}
...
}
To set text, it's better not to "hardcode" with a string in quotes, but to create a string file and ask for it, which will look more like this:
outputBottom.setText(R.string.my_words);
Here are the developer notes on strings.
If you're using Android Studio there are tutorials for how to make that happen.
You can ignore these warning by adding
lintOptions {
disable 'MissingTranslation'
}
to the gradle.build file.
I was working fine. My p.c powered off due to light. When I start again my android studio, there came a lot of errors in my all java files. How can I solve them?
public class MainActivity extends Activity {
private boolean detectEnabled;
private TextView textViewDetectState;
private Button buttonToggleDetect;
private Button buttonExit;
FlashLight flashLight;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewDetectState = (TextView) findViewById(R.id.textViewDetectState);
buttonToggleDetect = (Button) findViewById(R.id.buttonDetectToggle);
buttonToggleDetect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setDetectEnabled(!detectEnabled);
}
});
buttonExit = (Button) findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this , FlashLight.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void setDetectEnabled(boolean enable) {
detectEnabled = enable;
Intent intent = new Intent(this, CallDetectService.class);
if (enable) {
// start detect service
startService(intent);
buttonToggleDetect.setText("Turn off");
textViewDetectState.setText("Detecting");
}
else {
// stop detect service
stopService(intent);
buttonToggleDetect.setText("Turn on");
textViewDetectState.setText("Not detecting");
}
}
}
Here is a screenshot of my issue:
Because we don't have enough information, we can't tell you. This has happened to me before though, so I can give you a few tips:
Just wait until the gradle build is done, often times there are random errors which resolve themselves after a few minutes. This especially happens with resources that you are getting based on an id, R from R.id shows as an error until the gradle build is finished
Clean your project. Build->Clean Project
Rebuild Project. Build->Rebuild Project
Restart Android Studio
Restart Computer
Honestly, we can't give you more information than that until we get more details. Chances are it's number 1 that is the problem, so just wait.
{Rich}
Build--> Clean Project
Build-> Rebuild Project
Go to File
Then Invalidate Cache/ Restart
After that hit Invalidate and Restart
It's not a pleasant moment when your computer suddenly turns off and corrupting your project files. Sadly, when using Windows, you have a small hope that your files integrity are still intact when the Windows suddenly turn off due to power failure or having a friendly BSOD. I've experienced project error twice in a month because of it. Windows can't be counted for reliability, tried Linux instead.
To prevent another files error (or should I says, corrupted files?), you should always back up your project. Use a Version control systems (VCS) like git. Then copy/clone your project to USB flash drive. Or you can use a free private repository for git:
Bitbucket
GitLab
Always, always, always backup.
Build->Clean Project. This will solve your problem.
I get that error whenever I attempt to run this app. It's simple right now, I'm just trying to make sure the first button does what I want it to. It also mentions NullPointerException.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
calendarButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ComponentName cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
Intent i = new Intent();
i.setComponent(cn);
startActivity(i);
}
});
}
This (and declaring the buttons) are all I have done in the activity's Java file. I have set placeholders for listeners on other buttons but commented them out until I figure this one.
I have tried starting a new project running the same code (even pieced the layout back together) to make sure the Manifest wasn't accidentally messed with. Same issue.
I derped. Hard. I assigned the button variables a value during instantiation (which I have above onCreate) rather than INSIDE onCreate. I fixed it myself. Sorry about that.