Ignasi Sánchez
Ignasi Sánchez

Reputation: 75

Android Buttons: getting a text back

I'll go over this as quick as posible. I'm developing an app that needs a comprobation from the text of a button. Let me explain it in a code-way. If my button has "This text", then do that. I've been trying for the past two days several diferent things, and I'm on a dead end, so I'm asking. Here is my code:

XML:

<Button
                android:id="@+id/bHiddenL1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="invisible"
                android:layout_margin="18dp"
                android:text="@string/NotUsed"
                android:maxLength="10" />

Java:

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.newnotebookbutton);
            initializeNotebookNewSubject();


        }


            public void initializeNotebookNewSubject() {

                NewTextInput = (EditText) findViewById(R.id.etNewNotebookButtonCreateSubjectButton);
                OKButton = (Button) findViewById(R.id.bOkButton);
                Button1L = (Button) findViewById(R.id.bHiddenL1);           
                Button2L = (Button) findViewById(R.id.bHiddenL2);           
                Button3L = (Button) findViewById(R.id.bHiddenL3);           
                Button4L = (Button) findViewById(R.id.bHiddenL4);           
                Button5L = (Button) findViewById(R.id.bHiddenL5);           
                Button1R = (Button) findViewById(R.id.bHiddenR1);           
                Button2R = (Button) findViewById(R.id.bHiddenR2);           
                Button3R = (Button) findViewById(R.id.bHiddenR3);           
                Button4R = (Button) findViewById(R.id.bHiddenR4);           
                Button5R = (Button) findViewById(R.id.bHiddenR5);           

                OKButton.setOnClickListener(this);

            }


            @Override
            public void onClick(View v) {

                String b1L = Button1L.getText().toString();
                String b2L = Button2L.getText().toString();
                String b3L = Button3L.getText().toString();
                String b4L = Button4L.getText().toString();
                String b5L = Button5L.getText().toString();
                String b1R = Button1R.getText().toString();
                String b2R = Button2R.getText().toString();
                String b3R = Button3R.getText().toString();
                String b4R = Button4R.getText().toString();
                String b5R = Button5R.getText().toString();

                if(b1L == "Not Used" && NewTextInput.getText().toString() != null){

                    NewSubjectBundle.putString("title1L", NewTextInput.getText().toString());
                    NewSubjectBundle.putInt("int", 1);
                     mIntent.putExtras(NewSubjectBundle);
                     setResult(RESULT_OK, mIntent);
                     finish();

Let me specify better my problem. Whenever I'm trying to do the "String b1L = Button1L.getText().toString();", I get an error in the logcat saying that the pointer is Null. How should that be fixed?

Upvotes: 0

Views: 83

Answers (2)

Manuel Allenspach
Manuel Allenspach

Reputation: 12745

ZouZou is right. But in your case, even better is:

b1L.equals(getResources().getString(R.string.NotUsed));

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 93872

Don't compare content of Strings using ==.

b1L == "Not Used"

should be

b1L.equals("Not Used")

or even better

"Not Used".equals(b1L)

How do I compare strings in Java ?

Upvotes: 1

Related Questions