Android app crashes when changing activity [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
New to Android developing, using Android Studio. The first activity of my app is a simple main menu with a "Start" and a "Quit" button. When i press the "Start" button, the app should take me to the second activity named "EncounterScreen". Instead, the app crashes. enter image description here. I am trying to display the current health of the player object, of class Player. Apparently the problem is with "TextView healthText=findViewById(R.id.healthText);". This is the error: "Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference"
public class EncounterScreen extends AppCompatActivity implements View.OnClickListener {
Player player=new Player();
TextView healthText=findViewById(R.id.healthText);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_encounter_screen);
Player player=new Player();
TextView healthText=findViewById(R.id.healthText);
healthText.setText("Health "+player.getCurrentHP());
Button attackButton=findViewById(R.id.attackBtn);
Button drinkPotButton=findViewById(R.id.drinkPotBtn);
Button runButton=findViewById(R.id.runBtn);
attackButton.setOnClickListener(this);
drinkPotButton.setOnClickListener(this);
runButton.setOnClickListener(this);
}
#SuppressLint("SetTextI18n")
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.drinkPotBtn:
player.drinkPotion(player);
healthText.setText("Health "+player.getCurrentHP());
break;
}
}

Make sure the TextView id that you are trying to find has the correct id. You are using "R.id.healthText"
Does the TextView in your activity_ecounter_screen have a different id?

Player player=new Player();
TextView healthText=findViewById(R.id.healthText);
I believe the issue is with the above line. remove =findViewById(R.id.healthText).

Related

Android: App crashes on calling toast from another class [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 2 years ago.
I am a novice Android Developer.
I have created a package-private class which extends Application, and contains the required code for specific functions. I basically want to display if the user-selected button is the correct choice or not, via a toast. Since I have to call this code for many activities, I just created a package-private class for it. However, on clicking the button, the app crashes. Please see the code given below for reference.
I cannot change the onClick method to non-static because if I do that, Android Studio shows an error, and if I change it to static, I am unable to use the method getApplicationContext(), because it is not accessible inside static blocks.
I think that using view.getContext() is causing the crash.
Is there any workaround, or a solution?
Your help would be greatly appreciated. Thanks :)
Here is the code for your reference.
activity.java:
public class activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(functions.select);
functions.makeLayout(expression, buttons);
}
}
Here is the code which crashes the app.
functions.java:
class functions extends Application {
private static int idx;
public static View.OnClickListener select=new View.OnClickListener() {
#Override
public void onClick(View view) {
int selected_index=(int) view.getTag();
if(selected_index==idx)
{
Toast.makeText(view.getContext(), "Correct.", Toast.LENGTH_LONG).show();
((Button) view).setTextColor(Color.GREEN);
}
else
{
Toast.makeText(view.getContext(), "Wrong.", Toast.LENGTH_LONG).show();
((Button) view).setTextColor(Color.RED);
}
}
};
Okay, I figured out that it was not view.getContext() but the line int selected_index=(int) view.getTag(); which was causing the crash. I solved it first making it into a string and then int by using the following code:
String selected_index=view.getTag.toString();
int sidx=Integer.parseInt(selected_index);

Android Project Crashes [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I have a problem with my project on Android Studio. Every time I run the app, it starts up perfectly normal, no build errors at all, however, upon clicking a button on the Main Activity to go to another activity, the app stops. I have checked Logcat for the issue and it states that -
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.opendayapp.openday/com.opendayapp.openday.FAQ}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.opendayapp.openday.FAQ.configureContactButton(FAQ.java:55)
at com.opendayapp.openday.FAQ.onCreate(FAQ.java:20)
Here is some code from that project that Logcat has checked that might have an issue with it
Public Class
public class FAQ extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_faq);
configureHomeButton();
configureContactButton();
webView = (WebView) findViewById(R.id.webViewInformation);
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
//webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("file://asset/information.html");
}
Here's another part of the code that Logcat highlighted to have an issue with
Button contactButton = (Button) findViewById(R.id.btnContact);
contactButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i2 = new Intent(FAQ.this, Contact.class);
startActivity(i2);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_in_left);
}
});
}
Is there a fix for this, as I am stuck for a way to resolve this. Many feedback and criticism will be very helpful for future references
Thanks in advance
It looks like the button for which you are trying to set the onClick listener does not exist in your layout file. Or if it exist, you are using the wrong id in your activity class. Make sure that the id of the button is btnContact in your layout file as you have used in your activity.

Attempt to invoke virtual method void android.widget.ImageView.startAnimation on a null object reference [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
findViewById() returns null for Views in a Dialog
(4 answers)
Closed 4 years ago.
I want when a user clicks on "ans_three_btn_one" button than a dialog appears and in that dialogbox, I want to implement rorate animation on sunburst imageview. The only problem in this code is "sunburst.startAnimation". If I remove this line then code work properly but no animation. With this line of code my app crashing. Appreciate If you help me.
public class GamePlay extends AppCompatActivity implements View.OnClickListener {
Button ans_three_btn_one;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_play);
ans_three_btn_one = findViewById(R.id.ans_three_first_btn);
ans_three_btn_one.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Dialog first_prize_dialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
first_prize_dialog.setContentView(R.layout.activity_first_prize);
first_prize_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
first_prize_dialog.show();
ImageView sunbrust = findViewById(R.id.sunbrust_img);
//----Error area start ---
sunbrust.startAnimation(AnimationUtils.loadAnimation(v.getContext(),R.anim.rotate));
//----Error area end ---
}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.startAnimation(android.view.animation.Animation)' on a null object reference
Application terminated.
You have missed first_prize_dialog before findViewById.
It will be
ImageView sunbrust = first_prize_dialog.findViewById(R.id.sunbrust_img);

android onclick () not responding

I am new to android programming and trying to develop TIC TAC TOE game. I have created gameLogic() method and the problem is this that it is not working as it is expected to do, means on click of ImageView none of the images is getting displayed. Any help will be highly appreciated.
This is my code:
public class MainActivity extends AppCompatActivity {
public void gameLogic(View view) {
ImageView tappedView = (ImageView)view;
tappedView.setTranslationY(-3000f);
tappedView.setImageResource(R.drawable.black);
tappedView.animate().translationYBy(3000f).setDuration(500);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public void onClick (View view) {
ImageView tappedView = (ImageView)view;
tappedView.setTranslationY(-3000f);
tappedView.setImageResource(R.drawable.black);
tappedView.animate().translationYBy(3000f).setDuration(500)
Try changing the class name to onClick and go to XML the page where you designs the button and declare onClick method in the image view button.
All you have to do is let the design page know where the code is by providing a common name to look for which is on click.
Also you have to add an onClick listener which listens for the button to click as the code should know which button it is linked to.
So declare a setonclicklistener method on the button Id and you're set to go.
Also write the code within the override after the setting of the layout.
Hope you understood :)
If you're new try head first Android it'll be really helpful I know how struggling it is during the initial days.

How to make a button unclickable [duplicate]

This question already has answers here:
How to set a Button gray and unclickable? [duplicate]
(3 answers)
Closed 6 years ago.
So right now i am having trouble making the next button unclickable when it is on the last page of the activity. As of right now it goes back to the first screen. How do i make it so that it know when to grey out the button or make it unclickable when the user gets to the last screen.
Here is my code:
public class ReadingActivity extends Activity implements OnClickListener {
private ViewFlipper viewFlipper;
Button btnNext, btnPrev;
private float lastX;
/** Called when the activity is first created */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reading);
viewFlipper=(ViewFlipper)findViewById(R.id.view_flipper);
btnNext=(Button)findViewById(R.id.btnNext);
btnPrev=(Button)findViewById(R.id.btnPre);
btnNext.setOnClickListener(this);
btnPrev.setOnClickListener(this);
btnNext.setEnabled(true);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.btnNext:
viewFlipper.setInAnimation(this, R.anim.in_from_right);
viewFlipper.setOutAnimation(this, R.anim.out_to_left);
viewFlipper.showNext();
break;
case R.id.btnPre:
viewFlipper.setInAnimation(this, R.anim.in_from_left);
viewFlipper.setOutAnimation(this, R.anim.out_to_right);
viewFlipper.showPrevious();
break;
}
}
}
you can set the OnClickListener to null like so
btnNext.setOnClickListener(null);
I think you just want this method
button.setClickable(false);
To make a button grey and UnClickable
put android:enabled="false" in button tag
And using code button.setEnabled(false);

Categories

Resources