Incompatible Java method - java

I'm trying to learn Java through a tutorial on Aide and have entered what I believed was the correct code:
package com.aide.trainer.myapp;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set main.xml as user interface layout
setContentView(R.layout.main);
final TextView textView = (TextView) findViewById(R.id.mainTextView1);
Button button = (Button) findViewById(R.id.mainButton1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View p1)
{
// TODO: Implement this method
TextView textView = (TextView) findViewById(R.id.mainTextView1);
textView.setText("Clicked");
}
});
button.setOnLongClickListener(new OnLongClickListener()
{
#Override
public void onLongClick(View p1)
{
}
});
}
}
But at public void onLongClick(View p1), there is an error around void that says:
The type of this method is incompatible with the type of the overridden method 'android.view.View.OnLongClickListener.onLongClick(android.view.View)'
I can't figure out what I did wrong. Does anyone know?

In an OnLongClickListener, onLongClick(…) must return boolean, not void.

Related

Overriding parent method in child class. Child method not doing anything

I am having some trouble. I followed every guide online showing how to override a parent method in a child class. I have done everything I was told to do, yet my child function does nothing.
My MainActivity(Parent) class:
package com.example.flashcards;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
DatabseHelper DB = new DatabseHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeText();
changeText2();
};
public void changeText(){}
public void changeText2(){}
String [] columns = new String[] {
DatabseHelper.FLASHCARD_QUESTION,
DatabseHelper.FLASHCARD_ANSWER
};
#Override
public void onClick(View v) {
}
}
My child class (TextC)
package com.example.flashcards;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TextC extends MainActivity {
#Override
public void changeText() {
super.changeText();
final String[] revertText = {"H2O", "What elements does water consist of?"};
final TextView textChange = findViewById(R.id.flashcard1);
Button change = findViewById(R.id.answer1);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
public void changeText2() {
super.changeText2();
final String[] revertText = {"2,200° F", "How hot does lava get?"};
final TextView textChange = findViewById(R.id.flashcard2);
Button change = findViewById(R.id.answer2);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
}
My changeText() function does nothing. I am not getting any errors, so I can not tell what I am doing wrong. Do I need to create an onCreate method for the child class? But I am extending MainActivity which has it.
Any ideas on why my method overriding is not working?
With inheritance and overriding concepts, you need to override onCreate function in your child class. And from that overridden method, you can make a call to super.onCreate or you can do this.chnageText and this.changeText2 from child class.
Otherwise when you call onCreate function, it will call changeText and changeText2 from super class only.
In your child class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.changeText();
this.changeText2();
};
Modify parent class
package com.example.flashcards;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
DatabseHelper DB = new DatabseHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//changeText(); freeze or remove these callings
//changeText2();
};
public void changeText(){}
public void changeText2(){}
}
And add some code to your child class
package com.example.flashcards;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TextC extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
changeText();
changeText2();
};
#Override
public void changeText() {
super.changeText();
final String[] revertText = {"H2O", "What elements does water consist of?"};
final TextView textChange = findViewById(R.id.flashcard1);
Button change = findViewById(R.id.answer1);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
public void changeText2() {
super.changeText2();
final String[] revertText = {"2,200° F", "How hot does lava get?"};
final TextView textChange = findViewById(R.id.flashcard2);
Button change = findViewById(R.id.answer2);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
}
In the above method there is no point declare changeText(); and changeText2(); in parent activity. For the sake of reusability, we can use abstract classes and methods.
Do some changes to your parent activity as you see below.
public abstract class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeText(); // they dont have default implimentation in parent so it will be invoked from child class where these methods implimented
changeText2();
};
public abstract void changeText(); //there is no default implimentation
public abstract void changeText2();
}
And in child activity, you have to implement those methods.
public class TextC extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//changeText(); no need to call these methods bcz its already called in parent onCreate()
//changeText2();
};
#Override
public void changeText() {
super.changeText();
final String[] revertText = {"H2O", "What elements does water consist of?"};
final TextView textChange = findViewById(R.id.flashcard1);
Button change = findViewById(R.id.answer1);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
public void changeText2() {
super.changeText2();
final String[] revertText = {"2,200° F", "How hot does lava get?"};
final TextView textChange = findViewById(R.id.flashcard2);
Button change = findViewById(R.id.answer2);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int random = (int) (Math.random() * revertText.length);
textChange.setText(revertText[random]);
}
});
}
}

Button and click display

I have tried Everything to my knowledge and nothing seems to work. Any help would be appreciated.
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class activity_main extends Activity {
TextView txtCount;
Button btnCount;
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount= (TextView) findViewById(R.id.textView);
txtCount.setText(String.valueOf(count));
btnCount= (Button)findViewById(R.id.button);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
count++;
txtCount.setText(String.valueOf(count));
}
});
}
}
}
No errors appear at any point and the program works without errors; the button clicks and does not crash, but no click is counted numerically in the textView.
you onClick() listener is wrong.
try this one
btnCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
txtCount.setText(String.valueOf(count));
}
});

Dismiss activity in android

I am using the following code to show “Hello” message with ok button when the user clicks a button. In some applications this is working fine. ie while clicking the ok button, the activity is dismissed. But in one application, this is not getting dismissed after clicking the ok button. What to do? Please help.
public class MyClass extends Activity {
private TextView labelTxt;
private Button okBtn;
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.message);
labelTxt = (TextView) findViewById(R.id.txt);
labelTxt.setText("Hello");
okBtn = (Button) findViewById(R.id.okBtn);
okBtn.setOnClickListener(okBtnClickListener);
}
private final OnClickListener okBtnClickListener = new OnClickListener() {
public void onClick(View v) {
finish();
}
};
}
I have tested your code and modified little. Please check below
package test.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
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);
Button btn=(Button) findViewById(R.id.btnOK);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
Try like this....
okBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();// Closing Activity
}
});
In your layout make sure your Button is named:
android:id="#+id/okBtn"
.....
Did you import:
import android.view.View.OnClickListener;
Please look at this tutorial, it should help:
http://martin.cubeactive.com/android-onclicklitener-tutorial/

How to use a flag in event listener callback in Android app

Expected result
A button can act in a way like a toggle, such that,
(1) first time clicking on it, the button moves rightward
(2) second time clicking on it, the button moves leftward
(3) third time clicking on it, the button moves rightward
and so on....
Problem
The flag variable flagToggleButton cannot be used in button's OnClickListener event listener callback function.
Main.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button b = (Button) findViewById(R.id.button1);
boolean flagToggleButton = false;
b.setOnClickListener(new OnClickListener () {
#Override
public void onClick(View arg0) {
if (flagToggleButton == false) {
Animation anim = AnimationUtils.loadAnimation(Main.this,
R.anim.animation_move_right);
b.startAnimation(anim);
flagToggleButton = true;
}
else {
Animation anim = AnimationUtils.loadAnimation(Main.this,
R.anim.animation_move_left);
b.startAnimation(anim);
flagToggleButton = false;
}
}
});
}
}
Make it a member variable of your class. In other words, define it outside onCreate()
public class Main extends Activity {
boolean flagToggleButton = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
// rest of your code
Put globally
boolean flagToggleButton = false;
b.setOnClickListener(new OnClickListener () {
#Override
public void onClick(View arg0) {
if (!(flagToggleButton)) {
// do stuff..
flagToggleButton = true;
}
else {
// do stuff..
flagToggleButton = false;
}
}
});

Android app development error - multiple markers at this line

I have just started android development in eclipse with android and am trying to program a button, this is my code,
package my.Apprentice;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class ApprenticeVoteActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startVotingListener();
}
private void startVotingListener() {
final Button startVoting = (Button) findViewById(R.id.startVoting);
startVoting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
} // Multiple markers at this line error shows here
};)
}
}
The location of my error is commented above. I really have no idea on how to get rid of it, I have tried cleaning my project with no success. Does anyone have any ideas ? Thanks !
The Updated and working code is shown below:
package my.Apprentice;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ApprenticeVoteActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startVotingListener();
}
private void startVotingListener() {
final Button startVoting = (Button) findViewById(R.id.startVoting);
startVoting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
Try });
import android.view.View;
import android.view.View.OnClickListener;
startVoting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
} // Multiple markers at this line error shows here
}); // Sequence is wrong
Did you try to remove the ';' after the listener declaration ? Remove also the View reference :
startVoting.setOnClickListener(new View.OnClickListener() { <-- HERE
#Override
public void onClick(View v) {
} // Multiple markers at this line error shows here
};) // <--- HERE
Final :
startVoting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});

Categories

Resources