symbol not recognized buttonplay - java

I have been trying all day to get this little code to work I got farther because I skipped it but needed to come back to it my buttonPlay is not being recognized and my book is telling me I need to import a directive. How do I this
// this is the entry point to our game
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Here we set our UI layout as the view
setContentView(R.layout.activity_main);
// Get a reference to the button in our layout
final Button buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);

I presume that you are talking about this line:
final Button buttonPlay = (Button) findViewById(R.id.buttonPlay);
The compiler is complaining about this part: R.id.buttonPlay. It is saying that that use of buttonPlay is an undefined symbol.
The R class is actually automatically generated from the XML file that contains your app's UI design. It includes names for various elements. The most likely explanation of the compilation error is that either "buttonPlay" has not been defined in the XML, or you deleted it, or you have not spelled the name the same (including capitalization) in the two places.

Related

I have run error at on Create on Android studio

I get a massage says "app is keep stopping" when I run the project on my mobile.
the error at on Create(MainActivity.java:16)
at the line 16 set Content View method
help me
The method setContentView() sets the View that your MainActivity should display.
The View is usually declared in layout/activity_main.xml.
Some error seems to appear when trying to call this method in your application.
A common reason why android applications crash there is calling some methods in the overridden method onCreate(Bundle savedInstanceState) in a wrong order.
Check if your code follows this pattern:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView call
// other things
}
I cannot help you further now, please copy and paste the stacktrace (crash information) into your question. When your application just crashed on an emulator, you can see it in the Run-section (usually red text).

findViewById causes NullPointerException for no apparent reason

I am new to Android development. My app is compiling perfectly fine (no errors, no warnings), but it crashes on launch. I have diagnosed the cause of the crash to be a NullPointerException, and I discovered that if I remove TextView output = (TextView) findViewById(R.id.outputbox); as well as all the lines depending on this declaration, the app launches fine. I did research (both on and off Stack Exchange, and about 5 pages of Google results) and none of the solutions I have found worked, which is why I am asking this question. I have ran setContentView(R.layout.activity_main); before TextView output = (TextView) findViewById(R.id.outputbox); and outputbox is declared in activity_main.xml. This is not a duplicate of the existing questions because the answers to the other ones did not solve my problem.
most probably your declaration and initialization of the variable output is in the class. This is not going to work. The method findViewById will work only after the setContentView method has been invoked. That is why you have to declare your variable in the class, but initialize it in the method onCreate immediately after the setContentView method:
public class MainActivity extends AppCompatActivity {
TextView output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = (TextView) findViewById(R.id.outputbox)
}
}
The method findViewById searches for your component in the xml. For this to work, you have to point out what xml file will this Activity be using. This is done in the setContentView(R.layout.activity_main); command. It is a common mistake, so no worries :)

Android fingerprint cancellationSignal FINGERPRINT_ACQUIRED_TOO_FAST

What I do:
I am using the following tutorial to grasp the basic understanding on how to use fingerprint authentication in Android:
http://joerichard.net/android/android-fingerprint-example/
I basically follow it except that I added a button and I want to authenticate a user when the button is clicked.
So I have the following changes
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
authentication();
}
});
...
protected void authentication(/* Bundle savedInstanceState */) {
//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
...
My problem:
When I click the button the following notification is called (in "FingerprintHandler.java"):
Toast.makeText(appContext,
"Authentication error\n" + errString ,
Toast.LENGTH_LONG).show();
The errString is Fingerprint operation canceled.
and the Error message ID is 5.
By searching I found here that it correspond to FINGERPRINT_ACQUIRED_TOO_FAST, however I don't understand why this happen.
What I tried:
I tried to click the button while having my finger on the scanner but
the notification is immediate which make me believe that it does not
even listen/scan for a fingerprint.
I have also tried to make it work without the button but in that case
nothing happens.
If you see what could go wrong or have directions in which I could investigate let me know.
Thank you.
My sensor was not working very well. It takes many tries to have a reading (and some time it does not work at all).
Thus I would re-try by clicking on the button causing the error.
The function would not have any reading from the scanner when clicked again the button and give the error FINGERPRINT_ACQUIRED_TOO_FAST.
The fact that this specific error (TOO_FAST) is trigger didn't help me in my situation because as I explained this error message is trigger when clicking the button.
If anyone encounter the same problem give more time/chances to your sensor to recognize that a finger is being read.

How to set setContentView in another method?

I have an activity_main.xml with
tools:context="com.example.android.newwine.MainActivity"
and
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="goToNext"/>
In my MainActivity.java file I have
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
and
public void gaNaarNext(View view){
setContentView(R.layout.activity_main2);
}
I have an activity_main2.xml file with
tools:context="com.example.android.newwine.MainActivity"
When I touch the button next now the activity_main2.xml will be used as ContentView, so that's correct, but when I do anything then (like touching a RadioButton or doing doesn't matter what) in my activity_main2.xml file what calls a method, it says: "New Wine is stopped." Whatever I do what calls a method, the app crashes. I tried much and if I'm going to paste everything I tried here this will be a long, long question:).
But has someone an answer? I really don't know what to do now, so I thought; let's ask something on Stackoverflow. Thanks already!
You might prefer to use another activity or fragments to change your UI as using the method setContentView() multiple times is not recomended because of all the drawing involved.
Since it seems like you don't want to switch activities fragments may be the easiest to use.
This stackoverflow question addresses that matter as well.
After you change your content view, the UI Elements on the screen have changed. All of the variables that you previously set up now point to the old screen - which no longer exists. Once you change your content view, you must set these variables to the correct elements on that layout file. Also, you may want to check out Fragments like Carlos Sifuentes said.
When I do anything then (like touching a RadioButton or doing doesn't matter what) in my activity_main2.xml file what calls a method, it says: "New Wine is stopped." Whatever I do what calls a method, the app crashes.
The problem is you have used activity_main2.xml but you did'nt get the reference of RadioButton of activity_main2.xml. You should get all the view reference after setting new layout using setContentView() and then use as per your needs.
Update your gaNaarNext() method as below:
public void gaNaarNext(View view){
setContentView(R.layout.activity_main2);
// For example: If your activity_main2 contains radio_button then do this
RadioButton radioButton = (RadioButton) findViewById(R.id.radio_button);
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// Do something
}
});
}
Hope this will help~

Android Development: Button- findViewById?

What is the intent behind this piece of code?
Button sButton = (Button) findViewById(R.id.button_1);
In your XML files, you create IDs for the widgets you put on the screen.
In that code, you are creating a button reference (sButton) to be the button corresponding to the ID of button_1
findViewById searches for a button based on its ID - the ID is located in the R.java file of your project: Project Folder > gen > package > R.java
The R.java file holds references to everything (or basically everything?) in your project. It's an essential part of it.
That is why the parameter of findViewById is R.id.button_1 because you are searching for the ID of button_1 in the R class (it's a static field).
You are then casting that ID of the button_1 to a button with the (Button) in front of the findViewById.
Extra note:
If you look at the R.java class, you have it declared as: public final class R { so that's where the R in R.id.button_1 comes from.
Then you have another inner class like this: public static final class id { so that's where the id comes from in R.id.button_1
Then, in the id inner class, you have this (amoung other things):
public static final int button_1=...; where the ... would be some code to represent the int value. That's where the button_1 comes from in R.id.button_1
So bascially, R.id.button_1 goes to the R class, then the id inner class, then accesses the actual int value of the id name.
it finds a view in the associated layout XML by it's id, and casts it to a Button. Was it what you were asking?
Its used to find a Button which you have created in XML and bring it into the Java code so you can work with it. If you didnt do this you would not be able to give the button anything to do.

Categories

Resources