![](/img/trans.png)
[英]How to add item to string array in activity 'a' by click on the button in activity 'b' and save the items after exit from app?
[英]Android: how do I use Save button click in one Activity to add a CardView item in Recyclerview Activity?
我有一个UI屏幕(CardViewActivity),其中有一堆EditText行,用于为用户输入数据。 当用户完成他们点击界面中的“保存”按钮,输入字符串数据保存到CardView,然后将其添加到列表RecyclerView。 当我尝试在Recycler活动(ListContactsActivity)中添加“保存”按钮引用(R.id.saveButtonRV)时,由于单击Button而使应用程序崩溃。
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" />
来自ListContactsActivity的 logcat:
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.
检查ListContactsActivity.java的第58行。 这就是您的错误的根源。
看来您使用id R.id.saveButtonRV
创建了一个按钮。 你说,你张贴的XML使用由cardviewactivity。 这意味着您不能在其他活动中使用R.id.saveButtonRV
。 这就是为什么ListContactsActivity
的saveButton
值为null
。 因此,听者的onclick设置为null
值将导致null pointer exception
如果要将数据从一个活动传递到另一个活动,则应使用startActivityForResult()
。 检查此线程和此文档/教程 。
基本上,你用startActivityForResult()
而不是startActivity()
然后在你的CardViewActivity您需要创建另一个意图然后把字符串意图对象中,像这样做:
Intent returnIntent = new Intent();
returnIntent.putExtra(someTag, userInputString);
setResult(Activity.RESULT_OK, returnIntent);
然后在您的ListContactsActivity
,重写onActivityResult
以从意图中获取数据。
由于您没有发布有关recyclerview的足够数据,因此我将假定您为recyclerview的行使用单独的布局,并且在recyclerview适配器中具有List<String>
。
在获得用户输入并将其传递回包含recyclerview的活动之后,只需要将数据添加到recyclerview适配器中,很可能是List<String>
。 然后检查该如何将数据添加到recyclerview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.