0

So I have an android app (java in Eclipse) and I want to change the text of some buttons on a keyboard through a shift method. I implemented the same code as I did to change a textview text, the same as everyone says to on similar questions, but for some reason it WILL NOT work. For some reason, after testing other button functions, I've determined that there's something it doesn't like about me changing ANY property of the button. Tried cleaning the project, didn't help. Keep getting an invocation exception. Here is relevant code:

public class MainActivity extends Activity {

    boolean shift = true;
    static Vector<String> answer = new Vector<String>(1, 1);
    static int ansLength = 0;
    private TextView answerbox;
    private Button a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializeButtons();
        setContentView(R.layout.activity_main);
        answerbox = (TextView) findViewById(R.id.answerbox);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void initializeButtons() {
        a = (Button) findViewById(R.id.a);

    }

    public void typeKey(View sender) {
        Button pressed = (Button) sender;
        answer.add(ansLength, (String) pressed.getText());
        //answerbox.setText("test string");
        ansLength++;
        StringBuilder stringBuilder = new StringBuilder();
        for (String string : answer) {
            stringBuilder.append(string);
        }

        answerbox.setText(stringBuilder.toString());

    }

    public void backSpace(View sender) {

        answer.remove(ansLength - 1);
        ansLength--;
        StringBuilder stringBuilder = new StringBuilder();
        for (String string : answer) {
            stringBuilder.append(string);
        }
        answerbox.setText(stringBuilder.toString());
    }

    public void shift(View sender) {
        if (shift == true) {
            shift = false;
            a.setText("l");
        }
    }
} 

XML below:

<Button
            android:id="@+id/a"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="A" 
            android:onClick="typeKey"/>
<Button
            android:id="@+id/shift1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="^"
            android:textSize="24sp"
            android:onClick="shift" />
Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
Gil Guday
  • 7
  • 4
  • have you invalidate view after change properties? – Saeed-rz Aug 07 '13 at 06:16
  • Consider using AndroidAnnotations http://androidannotations.org/ it's a great tool for reducing boiler plate code and make initializations less error prone. It will even tell you if something is missing or messed up. – allprog Aug 07 '13 at 06:45

1 Answers1

1

First, findViewById() in initializeButtons() should be called after setContentView() since there is no layout data in Activity object before setContentView()

Therefore, move the statement as following:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initializeButtons(); // Move this.
    answerbox = (TextView) findViewById(R.id.answerbox);

Second, I'd like to advise you that do not use Vector in Java except in a special purpose. Use ArrayList instead. Vector in Java is slow, and almost deprecated. It is just available because of compatibility issues.

static Vector<String> answer = new Vector<String>(1, 1);

should be replaced to

static ArrayList<String> answer = new ArrayList<String>(1, 1);

If you have sync issues, (I don't think you have this issue now) then use Collections.synchronizedList() method: Why is Java Vector class considered obsolete or deprecated?

Community
  • 1
  • 1
Naetmul
  • 14,544
  • 8
  • 57
  • 81