android development View.OnClickListener error - java

I'm starting learning android development but im facing a an error which says :
Multiple markers at this line
- view cannot be resolved to a type
- The method setOnClickListener(View.OnClickListener) in the type View is not applicable
for the arguments (new
OnClickListener(){})
my program is :
package com.sc.uploader;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add;
Button sub;
TextView disply;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add =(Button) findViewById(R.id.badd);
sub=(Button) findViewById(R.id.bsub);
disply= (TextView) findViewById(R.id.tvdisplay);
add.setOnClickListener(new view.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#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;
}
}
so please if you could help me resolving the error i will be grateful
thanks

Change this line
add.setOnClickListener(new view.OnClickListener() {
to
add.setOnClickListener(new View.OnClickListener() {
capital "V". It is looking for a variable view when it should be set on the View Class.
OnClickListener Docs

Related

android app, Java - Overflow Menu Error and it's not displaying in the action bar

I did not expect an error to come up but the 3-dot ActionBar menu is not displaying and I am getting an unexpected error. I am not sure where I went wrong in my code.
Please help,
thanks in advance!
MainActivity.java
package com.example.it5.foothillers;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button;
Button button2;
Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Display app icon in the ActionBar
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.ic_launcher);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
}
private void buttonClick() {
startActivity(new Intent("it5.foothillers.news"));
}
private void button2Click() {
startActivity(new Intent("it5.foothillers.sports"));
}
private void button3Click() {
startActivity(new Intent("it5.foothillers.events"));
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
buttonClick();
break;
case R.id.button2:
button2Click();
break;
case R.id.button3:
button3Click();
break;
}
}
#Override
public void onPause() {
super.onPause();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(MenuItem item) {
int id = item.getItemId();
}
}
You have 3 onCreateOptionsMenu methods. Remove the last 2, you only need the first one with inflater. Also, Override the onOptionsItemSelected method to control the actions.

How to display my Inputted Text from the other class

This is my Input Edit Text class
package com.example.websocketclient;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText name;
Button enter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
name = (EditText) findViewById(R.id.editTextdname);
String dname = name.getText().toString();
sendname(dname);
}
});
}
public void sendname(String dname) {
Intent i = new Intent(this,Chatroom.class);
Bundle bundle = new Bundle();
bundle.putString("myname", dname);
i.putExtras(bundle);
startActivity(i);
}
#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;
}
}
This is my display the EditText to TextView class
package com.example.websocketclient;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class Chatroom extends Activity {
TextView uname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatroom);
uname = (TextView) findViewById(R.id.textViewmynam);
//kuha
Bundle bundle = getIntent().getExtras();
String urname = bundle.getString("myname");
uname.setText(urname);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chatroom, menu);
return true;
}
}
When i run it.. it says Unfortunately, then it will close..
I would like to pass a data from -> EditText to my other class to my display class -> TextView.
Along with ρяσѕρєя K answer, for passing the value you, since you are passing only one String, you can just use:
intent.putExtra("value","key");
and while getting,
String value= getIntent.getStringExtra("key","default_value");
it says Unfortunately, then it will close..
Because you are not initializing enter Button object before calling setOnClickListener. do it as:
setContentView(R.layout.activity_main);
// initilize Button here
enter= (Button) findViewById(R.id.BUTTON_ID_IN_XML);

Problems to use Views on Android

I'm trying to declare buttons and EditTexts in the OnCreate() function, calling the function findViewById(). But when I try to use the button in the emulator the application fails. When I use the findViewById() in the function of the treatment of the event (I used android:onClick because the treatment in java wasn't working either) the aplication worked normally. Why I can't only use the findViewById() one time, inside the onCreate?
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity implements OnClickListener{
String[] names;
EditText status;
EditText par1;
EditText par2;
EditText par3;
Button b1;
Button b2;
Button b3;
Button b4;
Spinner spin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (EditText) findViewById(R.id.text);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
status.setText("HA");
}
});
Button b2 = (Button) findViewById(R.id.b2);
b2.setActivated(false);
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Display any data about the persons
}
});
Button b3 = (Button) findViewById(R.id.b3);
b3.setActivated(false);
b3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Start the sensors lecture
}
});
Button b4 = (Button) findViewById(R.id.b4);
b4.setActivated(false);
b4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Execute the music
}
});
}
public void treatment_button(View v){
status = (EditText) findViewById(R.id.text);
status.setText("Connect");
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Inside oncreate() you have again define Button b1 like:
Button b1 = (Button) findViewById(R.id.b1);
Change it to:
b1 = (Button) findViewById(R.id.b1);
As you are creating the Button variable inside onCreate(),it will not work outside that method.So use the global reference variable of the Button that you have created.
try this hope it's worked:
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
String[] names;
EditText status;
EditText par1;
EditText par2;
EditText par3;
Button b1;
Button b2;
Button b3;
Button b4;
Spinner spin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (EditText) findViewById(R.id.text);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status.setText("HA");
}
});
b2 = (Button) findViewById(R.id.b2);
b2.setActivated(false);
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Display any data about the persons
}
});
b3 = (Button) findViewById(R.id.b3);
b3.setActivated(false);
b3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Start the sensors lecture
}
});
b4 = (Button) findViewById(R.id.b4);
b4.setActivated(false);
b4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Execute the music
}
});
}
public void treatment_button(View v){
status = (EditText) findViewById(R.id.text);
status.setText("Connect");
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}

GetExtra PutExtra

Ok So i want to pass values from One activity to another using getextrand putextra method.
In Second Activity in which I want to receive data is full of contents like Buttons and Text view. and i want to set that certain value which i have received from MainActivity to a particular text box.
setContenView(R.id.intent)
is the easiest one method to show a string but what if I want to Set this value to one or more textview. My code is here
MainActivity
package com.prashant.cookbook;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
static String Message_send="Prashant";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et=(EditText)findViewById(R.id.editText1);
Button send=(Button)findViewById(R.id.send);
final Intent msg_send= new Intent(this,Second.class);
String MSG= et.getText().toString();
msg_send.putExtra(Message_send, MSG);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(msg_send);
}
});
}
#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;
}
}
SecondAvtivity
package com.prashant.cookbook;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class Second extends Activity {
private TextView tv;
private Intent rcv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv = (TextView) findViewById(R.id.msg_show);
rcv = getIntent();
String Show_msg;
Show_msg=rcv.getStringExtra(MainActivity.Message_send);
tv.setText(Show_msg);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
but when I run this code I got nothing but a blank second Activity Not even a default text
get Value entered by user in EditText on Button click and then use msg_send.putExtra for placing value in Intent as:
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String MSG= et.getText().toString(); //<< get value from EditText here
msg_send.putExtra(Message_send, MSG);
startActivity(msg_send);
}
});

OnClickListener in Android Studio

I'm attempting to develop and app using the new Android Studio, but I keep receiving major errors on my OnClickListeners. Mainly it is telling me that it cannot resolve symbol "setOnClickListener" and it also cannot resolve "View v"
package com.sigmachi.derbydays;
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;
public class MainActivity extends Activity {
#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;
}
Button button= (Button) findViewById(R.id.standingsButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
}
});
That is the code in the class
Information:Compilation completed with 11 errors and 0 warnings in 4 sec
Information:11 errors
Information:0 warnings
/Users/angelo/AndroidStudioProjects/SigmaChiDerbyDaysProject/SigmaChiDerbyDays/src/main/java/com/sigmachi/derbydays/MainActivity.java
Error:Error:line (28)Gradle: <identifier> expected
Error:Error:line (28)Gradle: illegal start of type
Error:Error:line (28)Gradle: ')' expected
Error:Error:line (28)Gradle: ';' expected
Error:Error:line (28)Gradle: invalid method declaration; return type required
Error:Error:line (30)Gradle: illegal start of type
Error:Error:line (30)Gradle: ';' expected
Error:Error:line (30)Gradle: ')' expected
Error:Error:line (30)Gradle: not a statement
Error:Error:line (30)Gradle: ';' expected
Error:Error:line (33)Gradle: illegal start of type
Those are the errors I am receiving which makes absolutely no sense. Line 28 starts at when I do button.setOnClickListener
EDIT: Now I receive a force close when I press the button
This is the class it should open, a bare class with the only change being the layout to open
package com.sigmachi.derbydays;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class StandingsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.standings_layout);
}
#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;
}
}
Button button= (Button) findViewById(R.id.standingsButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
}
});
This code is not in any method. If you want to use it, it must be within a method like OnCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button= (Button) findViewById(R.id.standingsButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
}
});
}
you will need to button initilzation inside method instead of trying to initlzing View's at class level do it as:
Button button; //<< declare here..
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.standingsButton); //<< initialize here
// set OnClickListener for Button here
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
}
});
}
This worked for me:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newarea);
btnSave = (Button)findViewById(R.id.btnSave);
OnClickListener btnListener = new OnClickListener() {
#Override
public void onClick(android.view.View view) {
finish();
}
};
btnSave.setOnClickListener(btnListener);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
titolorecuperato = (TextView) findViewById(R.id.textView);
String stitolo = titolorecuperato.getText().toString();
Button btnHome = (Button) findViewById(R.id.button);
btnHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
same thing as Nic007 said before.
You do need to write code inside "onCreate" method. Sorry me too for the indent... (first comment here)
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.standingsButton) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}

Categories