Two buttons, it mix between the links - java

Two buttons, it mix between the links
I have a page with two buttons, when I had the first button, it took me to the link, when I had the second button, the first stopped and the second took me to the second link, help!
PageOne.jave
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
setContentView(R.layout.weoff);
}
}
MainActivity.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.abus);
button = (Button) findViewById(R.id.weoff);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
}
}

Here
button = (Button) findViewById(R.id.abus);
button = (Button) findViewById(R.id.weoff);
It replaces the old reference to R.id.weoff so you will add the listener to weoff, two times which replaces the old listener and you set it with a listener which points to PageOne.class.
You should use two variables
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
and
button1.setOnClickListener(new OnClickListener() {
button2.setOnClickListener(new OnClickListener() {
Since the two listeners point to the same Intent, you could create the listener only one time and put the same?

Related

'OnClickListener' is abstract; cannot be instantiated and Cannot resolve symbol 'v'

package com.example.application;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener());
#Override
public void onClick(View v)
Intent intent = new Intent(this, ClaimApplication.class);
startActivity(intent);
}
}
I have error such as 'OnClickListener' is abstract; cannot be instantiated and Cannot resolve symbol 'v'
I am a new programmer so please someone help!
You cannot instantiate abstract classes, so the new View.OnClickListener() gives the error.
As said in the Android Doc (https://developer.android.com/reference/android/widget/Button), try this
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here
}
});
and for keeping the code as simple as possible I suggest to call a method inside the onClick(),
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myMethod();
}
});
}
public void myMethod(){
//codehere
}

Toast doesn't show up when clicking button in other activity than Main

I am learning Android and I have build a simple Android app that shows Toasts for starters.
In the main activity I have a button that when I press it it shows a Toast and another button that takes you to another activity. This is the code:
package ro.serbab.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.addnotexml);
}
});
Button button2 = (Button) findViewById(R.id.button5);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Serban Android",
Toast.LENGTH_SHORT).show();
}
});
}
}
This is the second activity:
package ro.serbab.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ro.serbab.notes.R;
public class addnote extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnotexml);
Button button3 = (Button) findViewById(R.id.button2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Serban
Android",Toast.LENGTH_SHORT).show();
}
});
}
}
The button from the second activity doesn't show any toast. I keep pressing it and it doesn't work.
remove setContentView(R.layout.addnotexml); this line from onClick and add
when you want to go 2nd activity onClick the button this code will help you out
Intent intent = new Intent(MainActivity.this, 2ndActivity.class);
startActivity(intent);

I am new in android development. I have a problem

From the main page, i click the image and it will open the question 1. Now, I trying to open a new activity which is question 2 using image view from the question 1. But it has an error: question 1 is not an enclosing class.
Here is the code for Main Activity.
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//button video to video page
b = (ImageView) findViewById(R.id.btnPlayVideo);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, VideoPage.class);
startActivity(i);
}
});
//button start test to question1
b = (ImageView) findViewById(R.id.btnStartTest);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Question1.class);
startActivity(i);
}
});
//button question 1 ke question 2
b = (ImageView) findViewById(R.id.btn1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Question1.this, Question2.class);
startActivity(i);
}
});}}
This is question 1.java. it said that it is not an enclosing class.
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Question1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
}
This is question 2.java
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Question2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
}
}
Intent i = new Intent(Question1.this, Question2.class);
startActivity(i);
You are trying to start Question2 from the context of Question1 within MainActivity. The first parameter of new Intent() should be the context of the enclosing class that will start the next activity. Since you are defining this in MainActivity, the context should be MainActivity.this. There is a great introduction to this topic here.
Either change the first parameter of new Intent() to "MainActivity.this" or move the entire second OnClickListener to a view that is within Question1.

Button one should aim to page one

I have a page that has two buttons to "two" other pages, the thing is, button 1 and button 2 lead to a second page, thought button 1 should lead to the first page and button 2 should lead to the second page, here is the java file, I don't know how to put it!
ActiveMain.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button1;
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTwo.class);
// you made a mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
}
}
PageOne.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
}
}
PageTwo
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageTwo extends Activity {
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weoff);
}
}
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTWO.class); // you made a
mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});

button can not be resolved

This is my starting activity. I'm trying to get this button to work but it's been giving me this error.
Line 15 button can not be resolved.
package com.synamegames.giveaway;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GiveawayActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button register = (Button) findViewById(R.id.register);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
setContentView(R.layout.main);
}
}
Please try this..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
}
The problem is You have defined the Button instances as register in the line final Button register = (Button) findViewById(R.id.register); But you are setting onclick listener to the button instance which is not defined. You should have
register.setOnClickListener(new OnClickListener() {
instead of
button.setOnClickListener(new OnClickListener() {
U can use android:onClick from xml and pass a the view into the .java file
eg:
android:onClick="bactToList"
in java:
public void bactToList(View view){
}

Categories