0

This is my first post on this platform, if I have done wrong, please forgive me. These codes can run in eclipse without error, when the app run on the phone and clicked the button, the app exited with “Unfortunately,*** has stopped”. Firstly, I thought my phone is not compatible with the program, so I used the emulator and got the same result.

This is the code:

public class MainActivity extends Activity implements OnClickListener{

  private Button button;
  private EditText editText;

  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text_layout);

    button = (Button) findViewById(R.id.test_button);
    editText =(EditText) findViewById(R.id.edit_text);
    button.setOnClickListener(this);

  }

  @Override
  public void onClick(View v){
      switch (v.getId()){
      case R.id.test_button:
            String inputText = editText.getText().toString();
            Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
          break;
      default:
          break;
      }
  } 

}

String inputText = editText.getText().toString() replaced by String inputText = hello , that would be run on the phone successfully with "hello" when clicked hte button. Thank you for helping me solve this problem

<Button
    android:id="@+id/test_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"/>

<EditText
    android:name="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Type something here"
    android:maxLines="2"
    />

0 Answers0