Syntax error on token “else” - java

Please help, I want to make the button visible only if the checkbox is checked. But the token "else" gives me a syntax error. What do you think it could be? For a while I thought it could be some bracket but I don't really know.
package com.example.holaamigos;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public final static String EXTRA_SALUDO = "com.example.holaamigos.SALUDO";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtNombre = (EditText)findViewById(R.id.TxtNombre);
final Button btnHola = (Button)findViewById(R.id.BtnHola);
final CheckBox checkbox1 =(CheckBox)findViewById(R.id.checkBox1);
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton arg0,
boolean checked) {
if (checked)
Toast.makeText(checkbox1.getContext(), "Activo", Toast.LENGTH_LONG).show();
btnHola.setVisibility(0);
btnHola.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivitySaludo.class);
String saludo = txtNombre.getText().toString();
intent.putExtra(EXTRA_SALUDO, saludo);
startActivity(intent);
else
Toast.makeText(checkbox1.getContext(), "Inactivo", Toast.LENGTH_SHORT).show();
}
});
}
});
}
#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;
}
}

When you have multiple instructions in an if else statement, you should use {} i.e :
if (checked){
//your code goes here
}
else {
//One instruction so you can leaves braces but it's better to put them
Toast.makeText(checkbox1.getContext(), "Inactivo", Toast.LENGTH_SHORT).show();
}
A good practice is too always englobe your instructions in {} even if you have only one.
Read this :
In addition, the opening and closing braces are optional, provided
that the "then" clause contains only one statement
Deciding when to omit the braces is a matter of personal taste.
Omitting them can make the code more brittle. If a second statement is
later added to the "then" clause, a common mistake would be forgetting
to add the newly required braces. The compiler cannot catch this sort
of error; you'll just get the wrong results.

Change it for
boolean checked) {
if (checked)
{
Toast.makeText(checkbox1.getContext(), "Activo", Toast.LENGTH_LONG).show();
btnHola.setVisibility(0);
btnHola.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivitySaludo.class);
String saludo = txtNombre.getText().toString();
intent.putExtra(EXTRA_SALUDO, saludo);
startActivity(intent);
}
else
{
Toast.makeText(checkbox1.getContext(), "Inactivo" Toast.LENGTH_SHORT).show();
}
}

Related

"Expected resource of type layout/menu" in Android Studio

package com.example.nimehr.streaming;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.videostream.VideoViewActivity;
public class MainActivity extends Activity {
Button button;
public MainActivity() {
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(2130903040);
this.button = (Button)this.findViewById(R.id.myButton);
this.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, VideoViewActivity.class);
MainActivity.this.startActivity(intent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
this.getMenuInflater().inflate(2131165184, menu);
return true;
}
}
I'm getting the error "Expected resource of type layout/menu" at 2130903040 in this.setContentView(2130903040); and 2131165184in this.getMenuInflater().inflate(2131165184, menu); respectively. I fixed it here this.button = (Button)this.findViewById(2131230720); by changing it to R.id.MyButton
DO not do it this way:
this.setContentView(2130903040);
R class exists for a reason to avoid explicitly using ids like this, and to replace it with a more readable cleaner code:
this.setContentView(R.layout.layout_name_here);
From the docs:
When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory.

Android SDK - Changing Properties in Array List

Can anyone please help me out. I'm having problems changing my array list properties. I am more familiar with changing properties via the XML file. Here is my code:
package com.example.examproject;
import java.util.Random;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Intent;
public class Menu_Lists extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myAttractions = new String[2];
myAttractions[0] = "Grocery List";
myAttractions[1] = "Go Back";
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, myAttractions));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
switch(position) {
case 0: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
break;
}
default: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
}
}
}
#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;
}
}
What I would like to do is to change the background color. That's my main goal. I would also like each option to change color whenever the user hovers over a specific option.
Please go easy with the terminologies as well. I am still a beginner with Android SDK.
Thanks in advance.

Can't get android service to work

I'm very new to android. I want to create an application that turns off all sounds at the selected time. I created a service with some code and in Eclipse there's no errors, but when I press the button nothing happens. I can see in Application Manager that my program and the service SilentHours are running. Here's my code:
MainActivity.java
package com.example.silencecontrol;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_NAME = "sending silentHour value to service SilenceHours";
#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;
}
public void setSilenceHours(View view) {
EditText editText1 = (EditText)findViewById(R.id.editText1);
TextView textView1 = (TextView)findViewById(R.id.textView1);
if(editText1.length() > 0) {
Intent intent = new Intent(this, SilentHours.class);
String editText1String = editText1.getText().toString();
int silentHour = Integer.parseInt(editText1String);
intent.putExtra(EXTRA_NAME, silentHour);
this.startService(intent);
} else {
textView1.setText("Please enter the silence hour. ");
}
}
}
SilentHours.java
package com.example.silencecontrol;
import java.util.Calendar;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
public class SilentHours extends Service {
public SilentHours() {
}
protected void onHandleIntent(Intent intent) {
int silentHour = Integer.parseInt(intent.getStringExtra(MainActivity.EXTRA_NAME));
Calendar calendar = Calendar.getInstance();
int currentTime = calendar.get(Calendar.HOUR_OF_DAY);
if(currentTime >= silentHour) {
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
And by the way I can't #Override the onHandleIntent method. If I put the #Override annotation I get error:
The method onHandleIntent(Intent) of type SilentHours must override or implement a supertype method.
Is that annotation necessary?
The method you are looking for is named onStartCommand, not onHandleIntent.
And no, it's not necessary to add this annotation to an overriden method, it's just a very good practice as you can get sure that you respected the signature of the super class.
Let you IDE help you when you code. Simply type "on" then ask for completion to see which method you can override.

No enclosing instance of the type DonationsActivity is accessible

I'm extremely new to all of this coding stuff, and I've gotten pretty far on my own, but I can't seem to figure out this error. All help is appreciated.
"No enclosing instance of the type DonationsActivity is accessible in scope"
package com.ganttbros.shadowui;
import org.donations.DonationsActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class DonateActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donate);
final Button donate = (Button) findViewById(R.id.donatebutton);
donate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.donate, menu);
return true;
}
}
I should tell you that I am attempting to implement this library: https://github.com/dschuermann/android-donations-lib#readme
I have set up the button, but I just need to get it to launch the "DonationsActivity" when pressed.
use
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));
OR
startActivity(new Intent(v.getContext(), DonationsActivity.class));
instead of
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
for starting DonationsActivity Activity from DonateActivity Activity
Change this:
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
to:
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));

I want to make password protected android application

I want to make password protected android app, but when in this simple program system is not matching two strings.
package com.pokmgr;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainPM extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pm_layout);
final EditText pin = (EditText) findViewById(R.id.pinET);
final String pass = pin.getText().toString();
final String code = "ajaj";
Button enter = (Button) findViewById(R.id.enterBtn);
enter.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (pass.equals(code)) {
Intent in = new Intent(MainPM.this, Menu.class);
startActivity(in);
}
else {
Intent in = new Intent(MainPM.this, Menu2.class);
startActivity(in);
}
}
});
}
/*#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.pm_layout, menu);
return true;
}*/
}
I have made Menu and Menu2 class, but every time Menu2 class is accessed. Even if I enter same pass that is "ajaj" [in this code to test]
i have defined both activities in manifest file.
Can't understand why pass.eqals(code) is not working
The problem is that you are setting pass to the contents of the EditText when the activity gets created. Instead you have to retrieve the contents of your EditText inside the OnClickListener.
Like this:
public void onClick(View v) {
final String pass = pin.getText().toString();
if (pass.equals(code)) {
// do something
} else {
// do something different
}
}
Put pin.getText().toString(); inside onClick of button. You are setting variable pass before the user actually entered something in pinEt EditText.

Categories