0

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.

twlkyao
  • 14,302
  • 7
  • 27
  • 44
theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169

2 Answers2

2

The error is :

edittext = (EditText) findViewById(R.id.editText);

because editText component is in fragment_main.xml , not the current activity's contentview activity_main.xml, so the value of variable edittext must be null, then the following code will cause error.

please move the method addKeyListener() into PlaceholderFragment class, then it will be ok.

lxh2002
  • 36
  • 2
0

I guess the solution is simple.Your EditText resides in fragment_main.xml and you are using activity_main with setContentView() in your java file.Hence there will be a nullpointer exception.The solution is :change setContentView(R.layout.activity_main); to setContentView(R.layout.fragment_main);

kgandroid
  • 5,507
  • 5
  • 39
  • 69
  • Hi @kgandroid. Seems like your method is simpler than what the others suggested, that is putting stuff into `PlaceholderFragment`. I'll test it out on my phone soon. – theGreenCabbage May 28 '14 at 18:18