Cannot resolve symbols getIntent(), findViewById, etc - java

Cannot resolve symbol findViewById(), getApplicationContext(), and many more basic functions, maybe I did something wrong with the Resources file, but I don't know what to do now to correct.
package com.example.apple.onlinesql;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class Answer extends AppCompatActivity {
TextView questionTextView, answerTextView;
String question, answer;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
Bundle bundle = getIntent().getExtras();
question = bundle.getString("question");
answer = bundle.getString("answer");
/*cannot resolve symbol findViewById()*/
questionTextView = (TextView) findViewById(R.id.questionTextView);
answerTextView = (TextView) findViewById(R.id.answerTextView);
questionTextView.setText(question);
answerTextView.setText(answer);
}
}

The application hasn't build successfully, as #vasilis mentioned try to clean and rebuild your project. And if that not solve the case, restart your studio and in worst case restart you system.

You appear to be running into the corrupt cache bug in Android Studio.
To fix: File > Invalidate Caches / Restart

I got the way although not very efficient but I got the way. I copied my entire project to a different location and opened it from there. MAGIC it worked for me.

Related

Button type does not exist in Android Studio?

I am working on my first app in Android Studio and have created a button "buttonFish" in the xml creator that I now want to put an action onto in the mainactivity.java.
This is my code:
package com.example.acfaunapedia;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Private Button butf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
butF = (Button) findViewById(R.id.buttonFish);
}
}
It can find my Button but it doesnt recognize the Type "Button".
Any ideas on how to fix this?
Error Message Link
EDIT:
I created an empty activity to start with, but I don't think thats relevant.
Alt-Enter is your friend. This Android Studio shortcut key will prompt you for solution(s) to error messages.
In this case, it will probably prompt you to add import android.widget.Button in your source, which should resolve the problem.
ALSO:
In the future, please copy/paste the error message text into your post. "Text" is generally more helpful than "screenshots" ;)
Here's the error (from your screenshot):
public class MainActivity extends AppCompatActivity {
Private Button butf; // <-- Cannot resolve symbol 'butf'
As nanofarad pointed out, the problem isn't "Buttom" per se (although that IS a problem), it's that you said "Private" instead of keyword private.
Alt-Enter should help with ANY error.
And once again: "Generally, text is better than screenshots"

Log.d not showing log.d in Logcat Android Studio

Android studio does not show any Logs I wrote in my code. I have tried putting the log to verbose and debug. I am using 'No filters'. Why is 'Oncreatetestlog' not showing up in my logcat?
package com.example.keydown;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
Log.d("Oncreatetestlog", "onCreate() Restoring previous state");
/* restore state */
} else {
Log.d("Oncreatetestlog2", "onCreate() No saved state available");
/* initialize app */
}
}
}
Go to File -> invalidate caches / Restart. And let Android Studio index your project again. It works for me.
After hours of searching and trying, I found out it did not have to do with Android Studio, but that my phone didn't allow Logging. See this answer for more information.
Alternatively, you could use System.out.println() to show the text in logcat
Replace
Log.d("Oncreatetestlog", "onCreate() Restoring previous state");
With
System.out.println("Oncreatetestlog onCreate() Restoring previous state");
If Log.d is not showing in your Logcat just replace Log.d with Log.wtf Like this:
Replace:
Log.d("tag",""+catstr);
With:
Log.wtf("tag",""+catstr);
It Works..happy coding;)
Please check in "Run" on the right side of "Debug" on the bottom of the IDE.
in my case i actually did everything what they told but yet it's not working. so i checked there and i fount all the log over there.
thanks

Manually registering a new activity in manifest.xml

tl;dr Look at title + netbeans
I've been writing a game as I have been learning both Java and Android*. Most of it has gone without a hitch- but here we go.
To my understanding- each screen (main menu, gameplay, highscores, ect) is a separate Activity and therefore needs a different layout.
Problem 1:
EDIT: Solved. Thanks Daniel.
Problem 2: Then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
package lolfighter.notriot;
import android.app.Activity;
import android.os.Bundle;
import lolfighter.notriot.nojoke.R.*;
// #author DEVELOPMENT
public class GameActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(layout.main_1);
}
Also why is the bundle icicle? I have not specified this anywhere.
Edit- Not solved
*- I am in a partnership of sorts with a friend- the original plan was that he would handle all the code and I would handle all the graphic resources (He took an actual class for the Java language) But he has ran into a roadblock. I can't ask him because he has limited internet access (lunch breaks only) and lives in a different state (we go to college together)
Problem 2: then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
Change layout.main_1 to R.layout.main_1.
Also why is the bundle icicle? I have not specified this anywhere.
You can rename the variable to be whatever you want, it doesn't matter.

activity_main cannot be resolved or not a field

I am a beginner in android and I was trying to make a simple application on android and this "activity_main" error is not getting cleared.I have gone through all the available answers
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu); return true;
}
}
Clean project then try to run it. I faced the same problem. You can also do this:
Remove from your code following imports import android.R; or import your.application.packagename.R;
Now clean the project and run it.
Let me explain(put more detail) what exactly is happening with the above code.
FYI, there are two types of R.java file exists:
one is your project's R.java file, which you would use to access resources/layout of your project.
second is Android's R.java file which contains ID/index of the native resources like anim, color and many more resources, which you can access by using android.R.color.black way.
Mistake:
Now, you are making mistake by importing android.R which is not allowing you to access main_activity.xml of your project.
Solution:
Remove android.R and import R file of your project. You can press CTRL + SHIFT + O simply, will display available options.
U seem to have imported wrong R.java class
problem is in the first line "import android.R"
use your applications R.java class
Make sure you have the activity_main.xml file spelled correctly. I experienced the same problem due to having a misspelled file name.

Android Mediaplayer

i want to play an audio on my android app using the Mediaplayer class.
my problem is on the R.java part.
to better understand my problem, i'll have to show a part of my code
audioControl = MediaPlayer.create(context, R.raw.forward_100hz);
audioControl.start();
so, the problem is on the forward_100hz, which is my wav file which is stated that it cannot be resolved or it is not a field.
how can i resolve this problem?
I think the problem is with your imports. Probably you've already imported the android.R class, but not the R class of your project.
Make sure the file is stored in the folder res/raw
Try deleting the R file generated by eclipse if the file is in place
Make sure the file forward_100hz is in res/raw folder
Check your imports and remove import android.R;
If on Eclipse press Ctrl + Shift + O to auto suggest import and you should import
import your.package.name.R;
If 3 does not work check your manifest file and then res directory for error which might be causing the problem by not generating R.java
As the second parameter of the create method put the following path:
Uri path = Uri.parse("android.resource://<package-name>/"+ R.raw.forward_100hz);
First sure that folder/raw exists and create the Mediaplayer before of on create
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player=MediaPlayer.create(MainActivity.this,R.raw.forward_100hz);
player.start();
}
}

Categories

Resources