I am currently making a very very simple Android app, following this tutorial: http://www.mkyong.com/android/android-textbox-example/
In my fragment_main.xml
, I have this:
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
In my MainActivity.java
, I have the following. It's simply an app that responds to key presses.
private EditText edittext;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
addKeyListener();
}
public void addKeyListener()
{
// get edittext component
edittext = (EditText) findViewById(R.id.editText);
// add a keylistener to keep track of user input
edittext.setOnKeyListener(new View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
// display a floating message
Toast.makeText(MainActivity.this, edittext.getText(), Toast.LENGTH_LONG).show();
return true;
}
else if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_9))
{
// display a floating message
Toast.makeText(MainActivity.this, "Number 9 is pressed!", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
}
The app basically responds to key presses.
Everything I have up top are the only things I have changed from the stock application. The target was 4.3 on my Note 3.
My app runs fine if it is a Hello World app (the stock app you create with Eclipse), however, as soon as I add additional components/methods to it, it would simple get me unfortunately, myapp has stopped.
I have tried creating a new project just for this new example, but the same errors of the application crashing.
I was wondering does anyone know why this is happening. Compilation does not tell me any warnings/errors.