【发布时间】:2016-05-12 00:37:31
【问题描述】:
我有一个 UI 屏幕 (CardViewActivity),其中包含一组 EditText 行,用于为用户输入数据。用户完成后,他们单击 UI 上的“保存”按钮,将输入的字符串数据保存到 CardView,然后将其添加到 RecyclerView 列表中。当我尝试在 Recycler 活动 (ListContactsActivity) 中添加保存按钮引用 (R.id.saveButtonRV) 时,应用程序由于单击按钮而崩溃。
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.
【问题讨论】:
-
是的,在 onCreate() 中调用了 setContentView()。我首先将 saveButton 代码放在 ListContacts Activity 中,然后应用程序崩溃了,然后才意识到我真的想运行点击是在 CardViewActivity 中。请参阅上面的补充内容。
-
您是要修复 ListContactsActivity 中的 NPE 还是让 CardViewActivity saveButton 工作?选择一个并专注于那个......
-
我的偏好是让 CardViewActivity saveButton 工作。我可以删除 ListContactsActivity 中的 Button 和 setOnClickListener 代码,只要我能找到一种方法将 CardView 项从 CardViewActivity 添加到 RecycerView 列表。
-
你在哪里使用了那个布局? CardViewActivity 还是另一个?
-
CardViewActivity 使用 cardviewinput.xml 文件。该文件使用 viewflipper 加载具有 saveButtonRv 按钮的 Viewflipper 布局。
标签: android string button android-recyclerview