OnClickListener throws NullPointerException - java

I'm trying to set onClick listener to a button from a ViewPager's page but when I try to run the app I get NullPointerException
....
public class MainActivity extends Activity {
private ViewPager vPager;
private MyPagerAdapter pagerAdapter;
private TextView tv;
private MyViewPagerListener pagerListener;
private Button deleteAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.page_state);
pagerListener = new MyViewPagerListener();
pagerAdapter = new MyPagerAdapter();
vPager = (ViewPager) findViewById(R.id.vPager);
vPager.setAdapter(pagerAdapter);
vPager.setCurrentItem(0);
vPager.setOnPageChangeListener(pagerListener);
//Here is the problem
ClickListener cl = new ClickListener();
deleteAll = (Button) vPager.findViewById(R.id.btnDelete);
deleteAll.setOnClickListener(cl);
}
.....
}

You should register a new OnClickListener object instead of a ClickListener. Also, you have to provide an implementation for the onClick method like this:
new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};

Related

java.lang.NullPointerException: Attempt to invoke virtual method, causing app to crash

I'm particularly new to this field. I'm trying to create a login page using fragments and it crashed when I tried to run it. I checked the logcat, turns out its a NullPointerException. It highlighted the error here:
register = (Button) findViewById(R.id.RegisterUser);
register.setOnClickListener(this);
Here is my code
this is mainactivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView register;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
register = (Button) findViewById(R.id.RegisterUser);
register.setOnClickListener(this);
ViewPager viewPager = findViewById(R.id.viewPager);
AuthenticationPagerAdapter pagerAdapter = new
AuthenticationPagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragmet(new LoginFragment());
pagerAdapter.addFragmet(new RegisterFragment());
viewPager.setAdapter(pagerAdapter);
}
public void onClick(View v){
switch (v.getId()){
case R.id.RegisterUser:
startActivity(new Intent(this, RegisterUser.class));
break;
}
}
class AuthenticationPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragmentList = new ArrayList<>();
public AuthenticationPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
#Override
public int getCount() {
return fragmentList.size();
}
void addFragmet(Fragment fragment) {
fragmentList.add(fragment);
}
}
}
It seems you define the global field private TextView register; but you want to cast it to Button register = (Button) findViewById(R.id.RegisterUser);
Please check your XML file and make sure what's your element TextView or Button.
can you please check your activity_main.xml file
check into that is a Button or Textview

Java Android: the button does not work after returning to the first layout

when I press two buttons the picture changes but when I move with the 3 button to the 2nd layout and I go back to the first picture it doesn't change anymore
public class MainActivity extends AppCompatActivity {
private Button button1;
private Button button2;
private Button changeLayout;
private Button exit;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
imageView = findViewById(R.id.imageView);
changeLayout = findViewById(R.id.changeLayoutBt);
exit = findViewById(R.id.exitBtn);
}
public void changeImage(View view) {
switch (view.getId()){
case R.id.button1:
imageView.setImageResource(R.drawable.orange);
break;
case R.id.button2:
imageView.setImageResource(R.drawable.java);
break;
}
}
public void changeLayout(View view) {
setContentView(R.layout.second_layout);
}
public void exit(View view) {
setContentView(R.layout.activity_main);
}
}
`
In order to create new layout got to File>New>Activity>Empty Activity
Change your changeLayout code to this one
public void changeLayout() {
startActivity(new Intent(MainActivity.this, Main2Activity.class);
}
Main2Activity.class is your newly created java class. It would be Main2Activity.java or what ever you named it.
Inside your onCreate() method. Just below
changeLayout = findViewById(R.id.changeLayoutBt);
Paste this code
changeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeLayout();
}
});

Why the button added dynamically does not receive the input?

I was developing an application that It is a simple game of cards and I added a button, in a dynamic way at the view, which represents a card of the game, but this button doesn't receive any input when It is pressed.
The question is: Why the button does not receive any input?
I've already tried to use the Log to print the message but nothing is printed.
public class MainActivity extends AppCompatActivity {
private Banco banco;
private Giocatore giocatore1;
private Giocatore giocatore2;
private Giocatore giocatoreAttuale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
giocatore1 = new GiocatoreUmano();
giocatore2 = new GiocatoreUmano();
banco = new Banco(giocatore1,giocatore2);
giocatoreAttuale=giocatore1;
DaiCarteAiGiocatori();
DaiCarteAlPlayerOne();
}
private void DaiCarteAlPlayerOne(){
Button primaCarta = new Button(this); //BUTTON OF THE CARD
primaCarta.setText(giocatore1.getCartaByIndex(0).getSeme().toString()+":"+giocatore1.getCartaByIndex(0).getValore());
ConstraintLayout layout = findViewById(R.id.baseLayout);
layout.addView(primaCarta);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.move_cards); // THE ANIMATION
primaCarta.startAnimation(animation);
//when the button is pressed the log should print the message of the card
primaCarta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
banco.giocaCarte(giocatore1,giocatore1.getCartaByIndex(0));
Log.i("CARTA","Carta Premuta");
}
});
}
private void DaiCarteAiGiocatori(){
banco.pescaCarte();
}
}
Thanks for the help.
see my changes in three places commented by //!!!
public class MainActivity extends AppCompatActivity implements View.OnClickListener { //!!!
private Banco banco;
private Giocatore giocatore1;
private Giocatore giocatore2;
private Giocatore giocatoreAttuale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
giocatore1 = new GiocatoreUmano();
giocatore2 = new GiocatoreUmano();
banco = new Banco(giocatore1,giocatore2);
giocatoreAttuale=giocatore1;
DaiCarteAiGiocatori();
DaiCarteAlPlayerOne();
}
private void DaiCarteAlPlayerOne(){
Button primaCarta = new Button(this); //BUTTON OF THE CARD
primaCarta.setOnClickListener(MainActivity.this); //!!!
primaCarta.setText(giocatore1.getCartaByIndex(0).getSeme().toString()+":"+giocatore1.getCartaByIndex(0).getValore());
ConstraintLayout layout = findViewById(R.id.baseLayout);
layout.addView(primaCarta);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.move_cards); // THE ANIMATION
primaCarta.startAnimation(animation);
}
//!!!
#Override
public void onClick(View v) {
banco.giocaCarte(giocatore1,giocatore1.getCartaByIndex(0));
Log.i("CARTA","Carta Premuta");
}
private void DaiCarteAiGiocatori(){
banco.pescaCarte();
}

Trouble with Intent in Android Studio

I have been trying to get a string to go from one java class to another for a project of mine. The code I have been experimenting with is not working. When I press the button, I know it opens the other Java class because it creates the other layout, but it doesn't show the string. Please help me.
First Java Class:
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra("Number", editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString("number");
}
textView.setText(number);
}
}
you are putting "Number" as key but, in your second activity you are trying to retrieve "number"
so change number to Number and it will work.
Its case Sensitive.
Don't make your keys hard-coded in that way.
Just declare public static variable in MainActivity and use it from SecondScreenJ
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
public static String NUMBER_KEY = "Number";
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra(NUMBER_KEY , editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString(MainActivity.NUMBER_KEY);
}
textView.setText(number);
}
}
Beware when using the key for putting and getting the extras. They're case sensitive. Replace "number" with "Number" in your second activity.

Button that Plays Sound in Android Studio not Playing

I am trying to make a simple button that plays a sound, but I am getting an error on (this, R.raw.shotgun). I have the raw folder and the sound file. I think the problem is with the this but I don't know why. Thanks.
public class MainActivity extends AppCompatActivity {
private Button button;
#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) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.shotgun);
mp.start();
}
});
}
}
The first parameter of MediaPlayer.create is a Context. The usage of MediaPlayer.create(this, R.raw.shotgun) works when you are working in your Activitys scope, because Activity extends Context, therefore this in this case ends up being a Context as well.
However, you're working within View.OnClickListener scope, and this assumes this class' value, and not Context as required. To fix that, just set the Context variable to this while still in your Activity scope.
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(context, R.raw.shotgun);
mp.start();
}
});

Categories