Cannot resolve symbol view - java

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.

Related

images Intent does not open randomly. If I press the images in order intents are working

I have six images. If I press the first image intent is working. But if I press the third image firstly it is not working. it is working in order.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.indoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent);
ImageView imageView = (ImageView) findViewById(R.id.outdoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent);
You are setting other view click listener inside R.id.indoor_activities
In above code, you are saying that assign click listener every time to outdoor_activities if user click on indoor_activities first.
To fix this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.indoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent);
});// click listener for indoor activities
// click listener for outdoor activities when onCreated is called
ImageView imageView = (ImageView) findViewById(R.id.outdoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent);
});
I think you should do like this.This will certainly works as your requirement.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1 = (ImageView) findViewById(R.id.indoor_activities);
imageView1.bringToFront();
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent1 = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent1);
}
});
ImageView imageView2 = (ImageView) findViewById(R.id.outdoor_activities);
imageView2.bringToFront();
imageView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent2 = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent2); }
});
Hope this helps you.

onClick change imageButton in MainActivity

When i click onClick button change imageButton in MainActivity.class
This is Activity with onClick button:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
This is code in MainActivity with imageButton:
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton2);
imageButton.setImageResource(R.drawable.psik);
if I understand well, you should pass some indicator to change image resource to the MainActivity and set it in onCreate(). Try to change your code like this:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
myIntent.putExtra("shouldChangeButton", true);
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
and then in MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean defaultValue = false; //or true as you need
if(getIntent().getBooleanExtra("shouldChangeButton", defaultValue)) {
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
ib.setImageResource(R.drawable.img);
}
}
or in case you want permanent change, you should keep it in SharedPreferences instead of passing it through intent:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
prefs.edit().putBoolean("shouldChangeButton", true).apply();
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
and
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean defaultValue = false; //or true as you need
SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
boolean shouldChangeButton = prefs.getBoolean("shouldChangeButton", defaultValue);
if(shouldChangeButton) {
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
ib.setImageResource(R.drawable.img);
}
}
You need to add a onClickListener to the view which would trigger onClick. And then you need override the onClick where you finish the operations to change imageButton.
Just Look at this:
Button btn=new Button(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//to change you imageButton here.
imageButton.setBackgroundResource(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");

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