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