I have a UI screen (CardViewActivity) with a bunch of EditText lines for data input for the user. When the user is done they click the "Save" button on the UI to save the string data input to a CardView that is then added to a RecyclerView list. When I tried to add the Save button reference (R.id.saveButtonRV) in the Recycler activity (ListContactsActivity), the app crashed due to the Button click.
ListContactsActivity
...
public class ListContactsActivity extends AppCompatActivity {
private ListContactsAdapter mContactsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RecyclerView mRecyclerView;
...
Button saveButton = (Button) findViewById(R.id.saveButtonRV);
// the below line caused the app to crash with NPE
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Contact contact = new Contact("", "");
mContactsAdapter.addItem(contact);
mRecyclerView.scrollToPosition(0);
}
});
CardViewActivity (for user input)
...
public class CardViewActivity extends AppCompatActivity {
private ListenerEditText cListenerEditText;
...
public void onClickSave(View v) {
int stringToDo = cListenerEditText.getText().toString().replace(" ", "").length();
else if (stringToDo > 0 && stringNotes1 == 0 && stringNotes2 == 0 &&
stringDueDate == 0 && stringDueTime ==0) {
cListenerEditText.requestFocus();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(cListenerEditText.getWindowToken(), 0);
cListenerEditText.clearFocus();
Button saveButton = (Button)findViewById(R.id.saveButtonRV);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Can I reference Contact, the adapter and the RecyclerView
// here to addItem (the CardView with string data) to the
// ReyclerView list?
}
});
}
}
xml referencing the saveButtonRV
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardviewTwobuttons"
android:orientation="horizontal" >
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="14" />
<LinearLayout
android:id="@+id/LinearLayout4"
android:layout_marginTop="1dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="52"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/rect_forbuttons">
<Button
android:id="@+id/clearButton"
android:text="Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:textColor="#FFFFFF"
android:textStyle="bold"
style="?android:attr/borderlessButtonStyle"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/clearbutton_rounded"
android:drawableStart="@drawable/ic_clear_white_24dp"
android:drawableLeft="@drawable/ic_clear_white_24dp"
android:paddingStart="2dp"
android:paddingLeft="2dp"
android:paddingEnd="2dp"
android:paddingRight="2dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:onClick="onClickClear"
android:nextFocusRight="@+id/saveButtonRV" />
<Button
android:id="@+id/saveButtonRV"
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:textColor="#FFFFFF"
android:textStyle="bold"
style="?android:attr/borderlessButtonStyle"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/savebutton_rounded"
android:drawableStart="@drawable/ic_save_white_24dp"
android:drawableLeft="@drawable/ic_save_white_24dp"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:onClick="onClickSave" />
</LinearLayout>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="14" />
logcat from ListContactsActivity:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jdw.seventhscreen/com.example.jdw.seventhscreen.ListContactsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.jdw.seventhscreen.ListContactsActivity.onCreate(ListContactsActivity.java:58)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
02-02 23:47:10.529 15871-15871/com.example.jdw.seventhscreen I/Process: Sending signal.