android java onClick Could not execute method of the activity android java onClick Could not execute method of the activity xml xml

android java onClick Could not execute method of the activity


The key is in your error message:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference            at com.sapps.app.testapp2.MainActivity.addItem(MainActivity.java:53)

A good tip for learning from your error output is to look at each "Caused by" statement, and scan through the log until you find one of your own files referenced. Then that part of the error log will even tell you what line your code was failing on ( in this case it is line 53 on MainActivity.java).

A Null Pointer Exception in Java is when you attempt to call a method on some object 'A', but that object 'A' is currently null.

So this error message means:"On line 53 of MainActivity.java, you tried to call a method on some object which doesn't exist yet, so I crashed."

The method that is failing is EditText mText = (EditText) findViewById(R.id.textEt);

Usually this type of failure means that you aren't finding the right ID from your layout. Double check that textEt is the correct ID for this layout element.

EDIT:

Still not sure why your views aren't getting populated, but I did notice an error with your adapter. You are redefining mAdapter, so you have 2 copies, one in local scope and one as a member to MainActivity. This will definitely mess things up.

Right here:

// Setting the adapter.CustomRecyclerAdapter mAdapter = new CustomRecyclerAdapter();recyclerView.setAdapter(mAdapter)

You are redefining mAdapter locally. Do this instead:

// Setting the adapter.mAdapter = new CustomRecyclerAdapter();recyclerView.setAdapter(mAdapter)


You are getting a null pointer on the getText() call

It means the following line:

EditText mText = (EditText) findViewById(R.id.textEt);

returns null, solution is to check and correct the layout so that textEt is on it.

Edit:

If you are sure that it's in the layout remove EditText declaration.

Declare as private EditText mText; on the class scope

setContentView(R.layout.name_of_layout_here);mText = (EditText) findViewById(R.id.textEt);


The view in addItem(View v) is referring to the button only.The EditText object that you want is not in the button, it is in the button's parent view.If you try to access the object from the button view it will be null, because the button does not have it.Instead you need to access the object from the button's parent view.

// Solution:public void addItem(View v) {    View parentView = (View) v.getParent();    EditText mText = (EditText) parentView.findViewById(R.id.textEt);    Log.d("LOG", mText.getText().toString()));}

I know that technically this does not solve the code in the question. But the code in the question has been changed from what produced the error so this would solve the original buggy code that actually produced the question.I am guessing that the original buggy code looked like this:

// My guess this was the original buggy code// MainActivity.javapublic void addItem(View v) {    EditText mText = (EditText) v.findViewById(R.id.textEt);    Data dataToAdd = new Data(mText.getText().toString());    mData.add(dataToAdd);}// MainActivity.xml<LinearLayout>    <EditText        android:id="@+id/textEt"    />    <Button        android:text="Add"        android:onClick="addItem"/></LinearLayout>