Button Onclick does not work - java

EDIT!
I have this eror if i remove 1 from Oncreate1
http://i.stack.imgur.com/pYTLv.png
I don't know why my code it doesn't work, I have no errors but nothing happens.
Some help?
here is the code:
#Override
public void onBackPressed() {
super.onBackPressed();
}
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
addButtonClickListner();
}
public void addButtonClickListner() {
Button btnNavigator = (Button)findViewById(R.id.play);
btnNavigator.setOnClickListener(new OnClickListener() {
public void onClick(View arg) {
Intent intent =new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
}
}

Your code is in a method called onCreate1(). It's not an activity lifecycle method. Nothing happens because your code is not run. Rename the method to onCreate().
It's a good habit to always add the #Override annotation to methods where you intend to override a framework method, such as here. If the annotated method doesn't really override a method, you'll get a compile-time error.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
Button btnNavigator = (Button)findViewById(R.id.play);
btnNavigator.setOnClickListener(new OnClickListener(){
public void onClick(View arg)
{
Intent intent =new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
}
});
}
Try this

This should work
#override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
Button btnNavigator = (Button)findViewById(R.id.play);
btnNavigator.setOnClickListener(new OnClickListener(){
public void onClick(View arg)
{
Intent intent =new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
}
});
}

Make sure #Override annotation always come with override method (e.g onCreate, onResume,...)
Your method name is a new one "onCreate1" please rename it to "onCreate".
Maybe look like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
addButtonClickListner();
}
public void addButtonClickListner(){
Button btnNavigator = (Button)findViewById(R.id.play);
btnNavigator.setOnClickListener(new OnClickListener(){
public void onClick(View arg) {
Intent intent =new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
}
});
}
Beside that, you should print out some thing every where you want to know how to the app work.
Log.d("some text","some text");

Related

Cannot resolve symbol view

this public void onClick (View view) gives error
cannot resolve symbol view
whats the issue?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCamera = (Button) findViewById(R.id.btnCamera);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view){
//view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
Your question is mostly just a typo, but to point out the problem, you are passing an instance of anonymous class to the Button#setOnClickListener method. The correct syntax for this is:
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
Note carefully that the anonymous class is demarcated using { ... } braces, in which you are overriding and providing an alternative implementation for the onClick() method.

illegal start of expression

It says Expression expected but no recommendations as to what symbol is missing. I'm using android studio. I suppose it's a simple fix I'm just not sure what symbol.
Heres my code:
public class CloudActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
},
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
}, // Where the problem is
));
}
#Override
public void onClick(View v) {
}
}
You are closing your anonymous inner classes (new View.OnClickListener() {) wrong. You shouldn't be separating them with a comma but closing them off with a );.
For example,
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
match up your {} and () for all of them. So you will want to do the same thing for button3.setOnClickListener(new View.OnClickListener() { and any others you might have.
You can read more about them in the Java Docs
Try This. You weren't closing your inner classes properly. I imagine you accidentally put commas in place of semi-colons?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
});
}

2 buttons in my android layout, only 1 will work

In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}

button click event doesnt work

I'm trying to switch the views, but when I'm in the second view, the back event click doesnt work.. I don't know what's wrong.
Pls, see my code and help me!
Part1
Part2
public class t extends Activity implements OnClickListener {
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
volta = (Button) findViewById(R.id.button2);
volta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == volta) {
startActivity(new Intent(t.this, MainActivity.class));
}
}
}
You have to override onBackPressed. Change your MainActivity as below
public class MainActivity extends Activity {
private boolean goBack = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
goBack = true;
setContentView(R.layout.janela2);
}
});
}
#Override
public void onBackPressed() {
//If you have switched to R.layout.janela2 then go back
if (goBack){
setContentView(R.layout.activity_main);
goBack = false;
return;
}
//else do default action
super.onBackPressed();
}
}
Just do the following code, I hope it might help you
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, t.class);
startActivity(intent);
}
});
}
}
In t.java
public class t extends Activity{
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
}
#Override
public void onStop() {
super.onStop();
finish();
}
}
If you want two layouts then use viewflipper. If you want two activities (java classes) AND two layouts separately then use:
Intent i = new Intent (this, myClass.class);
startActivity(i);
To start the Activity and NOT setcontentview
So here:
public void onClick(View v) {
startActivity(new Intent (MainActivity.this, t.class));
OR IN THE CASE OF T.CLASS:
startActivity(new Intent (t.this, MainActivity.class));
}
You have to override onBackPressed() method if you want to back button functionality in your application. i.e.
public void onBackPressed() {
Intent start = new Intent(CurrentClass.this,Next_Activity.class);
startActivity(start);
finishActivity(0);
}

Why does my braces shows error?

Hello everyone. Today i started writting a code so i could open second activity while i am pressing on the button. And i am keep getting the "{}" brace errors. I also created manifest "android:name=".activity2">" But i can't find the way how should i fix my braces to stop showing error? I uplauded some pictures so you could see a bit better what's the problem. Maybe there is way easier way to open the second activity with the button? Any tips to fix the problem? Thank you :)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button imeageTextBtn = (Button) findViewById(R.id.imeageTextBtn);
assert imeageTextBtn != null;
imeageTextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), activity2.class);
startActivityForResult(intent, 0);
}
}
}
You're calling the setOnClickListener method. so it needs a closing ) and a semicolon.
imeageTextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), activity2.class);
startActivityForResult(intent, 0);
}
});
After imeageTextBtn.setOnclick...., you need to add a ); after the }
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button imeageTextBtn = (Button) findViewById(R.id.imeageTextBtn);
assert imeageTextBtn != null;
imeageTextBtn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), activity2.class);
startActivityForResult(intent, 0);
}
}
);
}

Categories

Resources