EditText空指针异常

时间:2014-12-15 19:16:36

标签: android

public class LinkActivity extends Activity {

TextView textView;
String[] text = { "robin", "robin", "pavel", "robin", "pavel", "robin",
        "pavel", "robin", "pavel", "robin", "pavel", "robin", };
private Button button;
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_link);
    Bundle bundle = getIntent().getExtras();
    final int link = bundle.getInt("link");
    Toast.makeText(getBaseContext(), "Right", 200).show();

    button = (Button) findViewById(R.id.button1);
    editText = (EditText) findViewById(R.id.editText1);
    textView = (TextView) findViewById(R.id.textView1);
    textView.setText(text[link]);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String name = editText.getText().toString();

            if ((name.equals(text[link]))) {
                Toast.makeText(getBaseContext(), "Right", 200).show();
            }

        }
    });
  }

}

在此代码中,我收到了另一个Activity的职位。 最后在这个应用程序中,如果名称正确,我会添加一些名称,这将显示正确的Toast。 但当我按下确定按钮时,没有发生任何事情。问题是什么?我不明白。

2.如果这是正确的,那么我想将它保存在sharedPrefarance中,就像当我回到这个位置时,它显示正确。

1 个答案:

答案 0 :(得分:6)

改变这个:

if (name == text[link]) {

是这样的:

if (name.equals(text[link])) {
相关问题