4

I am attempting to change the text displayed on a button from my java file, and am not having much luck. After searching for awhile on various forums, and doing google searches, I have learned to create a new button and then use button.setText. However, after my application would suddenly end, displaying a message about being forced to quit, I decided to do some debugging. I found the problem to be in this Java class.

package com.KenanDeHart.thebasics;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;



public class Showrole extends Activity {
    public static int players;
    public static int idx;
    public static int[] playArray;
    public static Button nextplayerButton =null;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.show_role);

            Bundle extras = getIntent().getExtras();
            Showrole.players = extras.getInt("PLAYERS");


            for (idx = 0; idx < players ; ++idx){

                display(idx);
                ++idx;
            }


        }








     public void display(int idx){
        Bundle extras = getIntent().getExtras();
        playArray = extras.getIntArray("PLAYARRAY");
        nextplayerButton = (Button) findViewById(R.id.nextplayer);
        nextplayerButton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
        nextplayerButton.setText(playArray[Showrole.idx]);
        }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
    }




    public void next(){

        Bundle extras = getIntent().getExtras();
        Button  button = (Button)findViewById(R.id.nextplayer);
        button.setText("Next Player");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }



    }
}

After using breakpoints, I think I have located the problematic source code.

 public void display(int idx){
    Bundle extras = getIntent().getExtras();
    playArray = extras.getIntArray("PLAYARRAY");
    nextplayerButton = (Button) findViewById(R.id.nextplayer);
    nextplayerButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
    nextplayerButton.setText(playArray[Showrole.idx]);
    }
    });
}

More specifically, this line

nextplayerButton.setText(playArray[Showrole.idx]);

When I press continue, a string of things happen.

  1. A file opens called

    ZygoteInit$MethodAndArgsCaller.run()

  2. My phone vibrates once
  3. About 3 seconds pass
  4. My phone vibrates three times quickly
  5. The program closes
Here is my logcat log

11-30 12:47:28.770: E/AndroidRuntime(9770): FATAL EXCEPTION: main
11-30 12:47:28.770: E/AndroidRuntime(9770): java.lang.NullPointerException
11-30 12:47:28.770: E/AndroidRuntime(9770):     at com.KenanDeHart.thebasics.Showrole$1.onClick(Showrole.java:51)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at android.view.View.performClick(View.java:2461)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at android.view.View$PerformClick.run(View.java:8888)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at android.os.Handler.handleCallback(Handler.java:587)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at android.os.Looper.loop(Looper.java:123)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at java.lang.reflect.Method.invoke(Method.java:521)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-30 12:47:28.770: E/AndroidRuntime(9770):     at dalvik.system.NativeStart.main(Native Method)

Pay attention to this error

11-30 12:47:28.770: E/AndroidRuntime(9770):     at com.KenanDeHart.thebasics.Showrole$1.onClick(Showrole.java:51)

I really am unsure what is happening here. This is my first time using Java, and I have only been using it for two days. I hope this question is explanatory enough, and I assure you I have done my research but to no avail. I really appreciate any help someone can give me. Thank you.

2
  • 1
    Can you post your logcat messages here? Commented Nov 30, 2012 at 6:23
  • I really didn't understand the use of passing idx variable to display method and though you passed it, you used ShowRole.idx instead of idx that was passed. Why is that? Try to debug it and check the value you are getting at the line nextplayerButton.setText(playArray[Showrole.idx]); which is line number 51. Commented Dec 1, 2012 at 4:26

4 Answers 4

1

If its just changing the text displayed on button.

add this as instance variable (outside all the methods)

Button nextplayerButton =null;

get reference of the button in onCreate() method.

nextplayerButton = (Button) findViewById(R.id.nextplayer);
nextplayerButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
nextplayerButton.setText(""+i);
}
});
Sign up to request clarification or add additional context in comments.

2 Comments

When I try to add the instance variable using the code you provided, I get an error saying "Syntax error on tokens, delete these tokens".
Hey Kumar, I found why I was receiving that error, I was adding the instance variable outside of the class instead of outside all methods. Woops :p
1

According to the LogCat, you have a NullpointerException in your display() method at line 42.

Caused by: java.lang.NullPointerException 11-30 06:44:37.147: E/AndroidRuntime(6601): at com.KenanDeHart.thebasics.Showrole.display(Showrole.java:42)

Just check if there's something that's not initialized there.

1 Comment

Thanks for the help. I was able to find the specific line where the problem seems to be happening, and am looking into how I can declare / initialize all variables in that line correctly.
0

Normally, this should work:

setContentView(R.layout.activity_mail_sender);
        int i = 2;
        btn = (Button) findViewById(R.id.autoSendBtn);
        btn.setText(""+i);

You don't have to create button in java code if its already there in xml file. There must be some other issue. Please post your logcat error

2 Comments

he want's to change in java file. see the whole content about question.
Yes, @chintankhetiya, but he doesn't want to create a button in java file, Still he tried that. "I have learned to create a new button and then use button.setText." So I just suggested that you can set text dynamically. Its not necessary to create it dynamically unless he wants to
0

Alright, I figured out what was happening.

The array "playArray" was not correctly being passed from my other intent. I looked into the variable tree and found it was actually equal to null. I solved this by sending the size of the area (which was a variable determined in the previous class) with the intent, and creating the array after arrival.

Thank you everyone for your help. I am impressed with the support and helpfulness found on Stack Overflow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.