The code throws compile time error:
Class 'Anonymous class derived from OnLongClickLister' is not abstract and does not override abstract method onLongClick(View) in OnLongClickListener
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button amitsbutton = (Button) findViewById(R.id.amitsbutton);
amitsbutton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("Small click is working");
}
}
);
amitsbutton.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean OnLongClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("long click is also working ");
return true;
}
}
);
You're not overriding the right method. It's onLongClick, not OnLongClick. See below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button amitsbutton = (Button) findViewById(R.id.amitsbutton);
amitsbutton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("Small click is working");
}
}
);
amitsbutton.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("long click is also working ");
return true;
}
}
);
Related
I have researched similar problems but none is a fit for this problem. Am trying to access the arrays of drawables from the method name called nextQuestion(), but i keep getting the error cannot resolve symbol 'ballArray' any help please. I just feel that this should work but don't know why. Here is my code.
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
The reason you can't access ballArray from within nextQuestion() is because it's declared in a separate method. If you want it accessible from within nextQuestion(), you would need to make it visible at that level:
ImageView mBallDisplay;
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
Do like this:-
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
I have a counter program for number. I define 3 buttons for Plus, Minus and Clear.
When I use clear button for clear TextView it's good. But after using Plus and Minus it it is Continuation last counter. that is my code.
please Help me.
public class CounterActivity extends Activity {
private Button btnPlus;
private Button btnMinus;
private Button btnClear;
private TextView txtCounter;
int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.counter_menu);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnClear = (Button) findViewById(R.id.btnClear);
txtCounter = (TextView) findViewById(R.id.txtCounter);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter++;
txtCounter.setText(counter + "");
}
});
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter--;
txtCounter.setText(counter + "");
}
});
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtCounter.setText(0 + "");
}
});
}
}
Change your clear button onClick method as follows:
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
counter = 0;
txtCounter.setText(0 + "");
}
});
public class saeidactivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.saeid);
Button btn=(Button) findViewById(R.id.saeidbtn1);
TextView str=(TextView) findViewById(R.id.saeidtxtv1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
str.setTextColor(0xFF00FF00);
}
});
}
}
Try to add final modifier.
final TextView str=(TextView)
instead of
TextView str=(TextView)
Or, you can make the TextView local.
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
TextView str=(TextView) findViewById(R.id.saeidtxtv1);
if(str != null)
str.setTextColor(0xFF00FF00);
}
});
i have 6 layouts and each layout having two button back and next..without button back and next application runs properly but when i put code in back and next button having name button 7 is used for back and button 8 is used for next layout it gives me a error null exception.
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btn7;
ImageButton btn8;
ImageView view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton7();
//addListenerOnButton8();
addListenerOnButton();
addListenerOnButton2();
addListenerOnButton3();
addListenerOnButton4();
addListenerOnButton5();
addListenerOnButton6();
public void addListenerOnButton7() {
btn7= (ImageButton) findViewById(R.id.btn7);
btn7.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton() {
btn1= (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton2() {
btn2= (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k2);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton3() {
btn3= (ImageButton) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k3);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton4() {
btn4= (ImageButton) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k4);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton5() {
btn5= (ImageButton) findViewById(R.id.btn5);
btn5.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k5);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton6() {
btn6= (ImageButton) findViewById(R.id.btn6);
btn6.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k6);
Runtime.getRuntime().gc();
}
}
);
}
You already use it in there
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Looking at the code, it looks like it comes from inside an activity so you need to call setContentView(R.layout.activity_main); in the onCreate() of your activity before btn7= (ImageButton) findViewById(R.id.btn7); and that should fix the NullPointer.
Why am I getting an java.lang.runtimeexception: Unable to instantiate activityComponentInfo {com.example.carprofile/com.example.carprofile.CarRegistraion}: java.lang.NullPointerExceprion
This is my code:
public class CarRegistration extends Activity{
//ParseXML parseClass;
Button saveCar;
EditText carMake = (EditText) findViewById(R.id.etCarMake);
EditText carModel = (EditText) findViewById(R.id.etCarModel);
String newCar;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.car_registration);
saveCar = (Button) findViewById(R.id.bSaveCar);
saveCar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
newCar = carMake.getText() +" "+ carModel.getText();
//parseClass = new ParseXML(newCar);
//parseClass.editXML();
}
});
}
if I clear the onCreate method, from saveCar = (Button)..., everything works fine.
Can someone tell me what am I doing wrong?
Try this,
public class CarRegistration extends Activity{
Button saveCar;
String newCar;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.car_registration);
EditText carMake = (EditText) findViewById(R.id.etCarMake); //these lines should be after setContentView()
EditText carModel = (EditText) findViewById(R.id.etCarModel);
saveCar = (Button) findViewById(R.id.bSaveCar);
saveCar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
newCar = carMake.getText() +" "+ carModel.getText();
//parseClass = new ParseXML(newCar);
//parseClass.editXML();
}
});
}