Why does my braces shows error? - java

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);
}
}
);
}

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);
}
});
}

Button Onclick does not work

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");

Android OnClickListener, intent and context

I am new to java and android. Here I am trying to set up my onclicklistener so when clicked, it will show another activity, i.e. ActivityB.class. The problem is with Intent i = new Intent(context, ActivityB.class); I am not sure what to put there for context. I tried to use this and context, and both are wrong.
Could you please kindly explain when and why I should use this and when to use other terms for the context?
public class MainActivity extends Activity {
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, ActivityB.class);
startActivity(i);
}
});
}
Change the code to.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, ActivityB.class);
startActivity(i);
}
});
As you need to pass context while using intent.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ActivityB.class));
}
});
Fastest method!
Hope it helps! :D
1) Replace context with getApplicationContext()
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),ActivityB.class);
startActivity(intent);
}
});
2) Replace context with MainActivity.this
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ActivityB.class);
startActivity(intent);
}
});
Hope it will help you!
Try this..
If you declare ActivityB.class in manifest this should work.
Intent i = new Intent(MainActivity.this, ActivityB.class);
startActivity(i);
Intent i = new Intent(this, ActivityB.class);
startActivity(i);

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);
}

Categories

Resources