Can't Instantiate the type - java

Have this code in my mainactivity.java file:
public class MainActivity extends Activity {
Button btnSendSMS;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener());
}
ADT says that : btnSendSMS.setOnClickListener(new View.OnClickListener());
cannot be instantiated.

You cannot intantiate a View.OnClickListener with an empty constructor. You need to instantiate an anonymous class for that.
Example:
btnSendSMS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO your code
};
});

If you want to add anonymous click listener do it that way
btnSendSMS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
// your code
}
});

btnSendSMS.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
1)That's the implementation you need!
2)Try to rebuild your project anyway!

ADT says that : btnSendSMS.setOnClickListener(new
View.OnClickListener()); cannot be instantiated.
I believe View.OnClickListener() is either an abstract class or an interface. In java You cannot instantiate an interface or an Abstract class, thus you get an error. I believe you need an anonymous inner class in place of instantiation.
btnSendSMS.setOnClickListener(new ViewOnClickListener() {
//your implemneting method from ViewOnClickListener
});

Related

setOnClickListener has not been written well, it gives an error

I'm trying to rewrite my code from android:onClick9" to setOnClickListener`
since that crashes, bu I'm getting an error. Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button9=findViewById(R.id.button9);
button9.setOnClickListener(
new Button.OnClickListener() {
public void onClick9(View view) {
button9.setText("OK");
}
}
);
}
**ERROR**
error: <anonymous com.example.tablelayout6.MainActivity$1> is not
abstract and does not override abstract method onClick(View)
in OnClickListener new Button.OnClickListener() {
EDIT
EDIT 2 I have tried button9 but still the label of button9 doesn't change and new Button.OnClickListener() is shadowed.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button9=findViewById(R.id.button9);
button9.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View view) {
TextView status=findViewById(R.id.button9);
button9.setText("OK");
}
}
);
}
When you pass an anonymous class to method, you have to implements all of its abstract/interface inherited method.
In your code, you wrote new method called onClick9, and didn't override the abstract method onClick as needed, so it gives new an error.
You should rename your method or override on click separately.

startactivity creates two instances

for my android app, I use a button to go to the next Activity.
the problem is when I touch the button on the screen one instance of the activity is created but when
I use performClick() method to click the button programmatically, it creates two instances of the activity. ( performClick() is called from a callback method).
I used the CLEAR_TOP FLAG but it seems to break the back button.
Any idea how to solve this problem ?
this is what my code looks like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(Activity1.this, Activity2.class));
}
});
}
private void A_callback_method(){
if (some_condition_to_launch_activity){
btn.performClick();
}
}
Just a simple trick. create function
private void function_name(){
startActivity(new Intent(Activity1.this, Activity2.class));
}
Then on the button onClickListener
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
function_name();
}
});
and also inside A_callback_method
private void A_callback_method(){
if (some_condition_to_launch_activity){
function_name();
}
}
hope this will solve your problems.
I succesfully did a work around to solve this problem by adding a boolean variable intialise it to false in the onResume() method and then did the following:
private boolean clicked_btn;
private void A_callback_method(){
if ((some_condition_to_launch_activity)&&!clicked_btn){
clicked_btn=true;
btn.performClick();
}
}

Activity cannot be transfered into a onclicklistener

I made one of the simplest programs that creates a login page, however, I cannot add an OnClickListener to my button, and I do not know why. I'm really new to Android Studio and have no idea what to do. When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #Override does not override what's above.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
regist1 = (Button)findViewById(R.id.btnregister1);
regist1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent teachtoregist = new Intent(TeacherInJ.this, TeacherRegisterInJ.class);
startActivity(teachtoregist);
}
});
etUsername = (EditText) findViewById(R.id.editText2);
etPassword = (EditText) findViewById(R.id.editText3);
bLogin = (Button) findViewById(R.id.btnlogin);
bLogin.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #override does not override what's above.
Both the above problems are because you didn't add the implements keyword to your Activity. You need to add it so the Activity can be regarded as OnClickListener interface by the button.
You need to do something like this (See the comments inside the code):
// see below the implements View.OnClickListener line that
// need to be added so the Activitiy can be regarded as the listener.
public class TeacherInJ extends AppCompatActivity implements View.OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
...
// now you can use this as the listener. It's because you have
// set the current Activity class as the View.OnClickListener
// this is refer to current Activity object.
bLogin.setOnClickListener(this);
}
// Now you can add the #Override to the onClick method from
// the View.OnClickListener.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
}
}
}

How to reference button for onClickListener?

I am trying to create an Android app, and i want to create a on click listener, here is what I have so far.
public void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}
As you see, i am in the very early stages, but where I first referenced amazonButton (before the = sign) button, it turns into red text and it says Cannot resolve symbol 'amazonButton'. Also, I have referenced this method in the onCreate method
This is how you'd go about creating a button and setting a click listener to it.
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
}
}
You can also put the initializations in a single method and call that method, like you've tried :
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
}
private void initializeViews() {
//Make sure you've declared the buttons before you initialize them.
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
// Add more Views initialization here ...
....
}
}
You need to give the type of the variable when you declare it.
Button amazonButton = (Button) findViewById(R.id.amazonButton);
The alternative is to declare it (but not initialize it) outside of any method and then initialize it later.
Button amazonButton;
/* ... */
private void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}

The different OnClickListener implementation ways

What is the difference between:
public class MainActivity extends Activity {
public void onCreate (Bundle savedInstanceState) {
button1 = (Button) findViewById(R.id.btn1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Click code
}
)};
}
}
And:
public class MainActivity extends Activity implements OnClickListener {
public void onCreate (Bundle savedInstanceState) {
button1 = (Button) findViewById(R.id.btn1);
button1.setOnClickListener(this);
}
public void onClick(View arg0) {
switch(arg0.getId()) {
case R.id.button1:
// Click code
break;
}
}
}
They have both the exact same functionality and results.
The first method uses an anonymous inner class that implements the interface method. By using this approach, you receive events only for that particular View.
In the second method, you entire Activity class implements the OnClickListener interface. You can set the OnClickListener of every View to this, and receive all the click events in one method, where you can then filter them and act upon them.
The first method translates to:
Button.OnClickListener anonymous_listener = new Button.OnClickListener() { ... };
button.setOnClickListener(anonymous_listener);
Which is to say that it dynamically creates and stores a new OnClickListener instance when you use it.
In the second method, your entire class uses one single instance of the OnClickListener, that is passed to all the Views you want to listen for clicks on.

Categories