onClickListener trouble - java

So, I'm learning and developing a very small app with Android AIDE on my phone, I have code like this:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends Activity {
EditText edit1;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit1 = (EditText) findViewById(R.id.edit1);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
//Problem (this) from AIDE error info: Method
'android.view.View.setOnClickListener(android.view.View.OnClickListener)' in 'android.widget.Button' can not be applied to '(com.mycompany.myapp.MainActivity)'//
}
public void OnClick(View v) {
switch (v.getId()){
case R.id.btn1:
Toast.makeText(getApplicationContext(),edit1.getText().toString(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
What is the problem?

Your activity have to implement View.OnClickListener interface
public class MainActivity extends Activity implements View.OnClickListener {
#Override
public void onClick(View view) {}
}

You know what this refers to, don't you? That's a reference for your activity, but setOnClickListener() takes an argument of type View.OnClickListener, not your activity.
Look at the sample code from the official doc:
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
}
});
When you create the anonymous inner class with new View.OnClickListener(), that anonymous class implements the OnClick interface.
If you just implement View.OnClickListener on your MainActivity, you'd have to change the way you handle events, and I presume you want to minimize the amount of refactoring.

This Code is working (Tested)
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends Activity implements View.OnClickListener{
EditText edit12;
Button btn12;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit12 = (EditText) findViewById(R.id.edit1);
btn12 = (Button) findViewById(R.id.btn1);
btn12.setOnClickListener();
}
public void onClick(View v){
switch (v.getId()){
case R.id.btn12:
Toast.makeText(getApplicationContext(),edit1.getText().toString(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}

Related

Android Studio: App doesn't start with implemented Anonymous Java Class

I caught the error message
"5-14 12:39:13.104 2518-2518/com.example.fdai3744.neueleereapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fdai3744.neueleereapp, PID: 2518 java.lang.RuntimeException: Unable to instantiate activity ..."
and here's my Java Code
package com.example.fdai3744.neueleereapp;
import android.net.wifi.p2p.WifiP2pManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public Button button_1 = (Button) findViewById(R.id.button1); //Button
public TextView text1 = (TextView)findViewById(R.id.text1); // Textview
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_1.setOnClickListener(new View.OnClickListener() { // Here I add the ActionListener for my button
#Override
public void onClick(View v) {
text1.setText("Button 1 wurde geklickt!");
}
});
}
}
If I start my App the emulator throws an error message "App has stopped". How should I prevent this error?
Well, your view hierarchy needs to be alive before your retrieve individual Views from it and the method setContentView() brings it to life(or instantiates it).
How?
setContentView(View) is a method exclusively available for Activity.
Internally it calls the setContentView(View) of Window. This method
sets the activity content to an explicit view. This view is placed
directly into the activity's view hierarchy. Calling this function
"locks in" various characteristics of the window that can not, from
this point forward, be changed. Hence it is called only once.
So, instead of initializing the Views as instance variables, instantiate them inside onCreate() after setContentView().
Also read: Android: setContentView and LayoutInflater
caused by
public Button button_1 = (Button) findViewById(R.id.button1); //Button
public TextView text1 = (TextView)findViewById(R.id.text1); // Textview
never assign view before setContentView() is called
your modified code
package com.example.fdai3744.neueleereapp;
import android.net.wifi.p2p.WifiP2pManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public Button button_1;
public TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_1 = (Button) findViewById(R.id.button1); //Button
text1 = (TextView)findViewById(R.id.text1); // Textview
button_1.setOnClickListener(new View.OnClickListener() { // Here I add the ActionListener for my button
#Override
public void onClick(View v) {
text1.setText("Button 1 wurde geklickt!");
}
});
}
}

Java Variable Scope Global

im trying to understand variable scope with simple example.
I need help with this code
package com.varialescope.examplevariablescope;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button buttonOne;
private Button buttonTwo;
private String mText = "Hello World";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Initialialize UI elements
buttonOne = (Button) findViewById(R.id.button_one);
buttonOne = (Button) findViewById(R.id.button_two);
//Button One click listener
buttonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Set new text
mText = "ONE";
Toast.makeText(MainActivity.this, mText, Toast.LENGTH_SHORT).show();
}
});
//Button Two click listener
buttonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, mText, Toast.LENGTH_SHORT).show();
}
});
}
}
How can i access to mText string from click listener method ?
and how can i set a new string for mText clicking button One and make it accessible globally?
thanks for help
you create anonymous class Object for clicklistener any anonymous class or inner class object has information about the outside class object , then it had the right to access the methods and variables of the outside class object

need some tips on my code, my code doesn't change text on Android

This code to change text when the user presses a button doesn't work. i tried to change it in some ways but i can't figure out why won't it change... please give me a bit help
package com.cookbook.simple_activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class activity extends Activity {
private TextView txt = (TextView) findViewById(R.id.hello_text);
Button startButton = (Button) findViewById(R.id.trigger);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
txt.setText(R.string.pressthisbutton);
}
});
}
}
Change it to
public class activity extends Activity {
private TextView txt;
Button startButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
txt = (TextView) findViewById(R.id.hello_text);
startButton = (Button) findViewById(R.id.trigger);
You need to initialize your Views after inflating your layout with setContentView(). Since your Views exist in your layout, they will return null if you haven't inflated your layout first. You can declare them before your setContentView() but you can't initialize them until after.
Also, since you are trying to access txt inside your listener it must either be final or declared as a member variable as above.
This was a rather easy one to spot but they aren't always. When you post a question try to describe what isn't working and how. Here it would be a NPE I'm guessing when you try to set the listener on your Button so it crashes. When it does crash, please provide the logcat so it is easier for us to spot the problem.
package com.cookbook.simple_activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class activity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
final TextView txt = (TextView) findViewById(R.id.hello_text);
Button startButton = (Button) findViewById(R.id.trigger);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
txt.setText("your text");
}
});
}
}
try this, I moved your button and textview declaring to the body of the activity.
android life cycle always starts with onCreate() method assign the id inside the onCreate
don't use private modifer for the TextView
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class activity extends Activity {
TextView txt;
Button startButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
txt = (TextView) findViewById(R.id.hello_text);
startButton = (Button) findViewById(R.id.trigger);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
txt.setText(R.string.pressthisbutton);
}
});
}
}
Always initialize your textview and other widgets in oncreate method other wise youll get NullPointerException
Try this way
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
txt = (TextView) findViewById(R.id.hello_text);
startButton = (Button) findViewById(R.id.trigger);
txt.setText("It Working Now!!");

java android handler calling

I am new in the android programming. I see many ways to do the event handling, but when I try to do it by calling the handler class it give error on handling class name:
package com.example.test;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//attach an instance of HandleClick to the Button
findViewById(R.id.button1).setOnClickListener(new HandleClick());
}
private class HandleClick implements OnClickListener{
public void onClick(View arg0) {
Button btn = (Button)arg0; //cast view to a button
// get a reference to the TextView
TextView tv = (TextView) findViewById(R.id.textview1);
// update the TextView text
tv.setText("You pressed " + btn.getText());
}
}
}
"HandleClick" error come on this it say class should be abstract type?
I do not understand why it is giving this error can any one help me?
That's the wrong OnClickListener class. You have
import android.content.DialogInterface.OnClickListener;
You need:
import android.view.View.OnClickListener;
For future reference, the error you get is "The type must implement the inherited abstract method...". This is because you need to implement the DialogInterface's onClick, which should have led you to notice that it was the wrong import (since you have onClick(View))
you imported the wrong OnClickListener, you should import the one from android.view.View
Make it simple & use this,
b1 = (Button) findViewById(R.id.button1);
TextView tv = (TextView) findViewById(R.id.textview1);
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
"You have clicked Button 1", Toast.LENGTH_LONG);
msg.show();
tv.setText("You pressed " + btn.getText());
}
});

Need an easier way to set up onClickListener for several variables in Android calculator program

I'm programming an Android calculator in Eclipse. I want to be able to set up the OnClickListener for several variables instead of having to code a listener for each one. Seems like overkill. Is there a way to do this using an array, maybe? Much help would be appreciated.
package rechee.cool;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
int counter=0;
//Just have two buttons so far, I'm going to have like 10 more
Button one;
Button two;
EditText display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Associate the button variable with the xml reference
one= (Button) findViewById(R.id.bOne);
display= (EditText) findViewById(R.id.editText1);
//When button is clicked, display the text. How do I do this for the rest of my variables?
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//String string= Integer.toString(counter);
display.setText("1");
}
});
}
}
The way I'd do this is add the same onClick attribute in XML to every button, like this:
<Button android:onClick="onButtonClick"
... />
and then add the method that you used in that attribute to your activity, differentiating the buttons by id:
public void onButtonClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do something when button 1 is pressed
break;
case R.id.button2:
// do something when button 2 is pressed
break;
// and so on ....
}
}
Alternatively you can use findViewById() to get each button and assign the same listener to all the buttons. Then differentiate by id inside onClick()as shown above. This adds a few useless codelines though, so I think this example here is slightly more clean.
Yes you just need to override the onClick method of the Activity and tell it to implement the OnClickListener:
import com.ewe.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
int counter=0;
//Just have two buttons so far, I'm going to have like 10 more
Button one;
Button two;
EditText display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Associate the button variable with the xml reference
one= (Button) findViewById(R.id.bOne);
display= (EditText) findViewById(R.id.editText1);
//When button is clicked, display the text. How do I do this for the rest of my variables?
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//String string= Integer.toString(counter);
display.setText("1");
}
});
}
#Override
public void onClick(View v){
switch (case v.getId()){
case R.id.bOne:
//Put code for bOne here
break;
case R.id.editText1:
//Put code for editText1 here
break;
}
}
}

Categories