0

我会尽快解决这个问题。我正在开发一个需要从按钮文本中得到认可的应用程序。让我用代码的方式解释一下。如果我的按钮有“此文本”,那么就这样做。在过去的两天里,我一直在尝试几种不同的东西,而且我处于死胡同,所以我在问。这是我的代码:

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" />

爪哇:

 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();

让我更好地说明我的问题。每当我尝试执行“String b1L = Button1L.getText().toString();”时,我在 logcat 中收到一个错误,指出指针为 Null。应该如何解决?

4

2 回答 2

1

不要使用 == 比较字符串的内容。

b1L == "Not Used"

应该

b1L.equals("Not Used")

甚至更好

"Not Used".equals(b1L)

如何比较 Java 中的字符串?

于 2013-10-02T17:47:44.833 回答
0

邹邹是对的。但在你的情况下,更好的是:

b1L.equals(getResources().getString(R.string.NotUsed));
于 2013-10-02T18:12:39.143 回答