[英]Android: how do I setEnabled(true) on a CardView in onResume?
我有一个CardViews的RecyclerView列表。 我在下面添加了以下代码以启动活动(ActActivity),该活动允许用户编辑CardView。 如果用户在CardView上快速连续单击多次,则setEnabled(false)
代码用于阻止打开活动的多个实例。 我只希望一次打开一个活动实例,以便用户仅编辑他们单击的单个CardView。
我的问题是,当我添加onResume()
部分以将setEnabled()
为“ true”时,应用程序崩溃。 当我删除onResume()部分时,setEnabled(false)代码通过不允许打开活动的多个实例来正常工作,但是问题是,对CardView的任何双击都会禁用将来的单击以正确启动ActActivity。
我在这里想念什么?
MainActivity.java
public class MainActivity extends AppCompatActivity implements
RecyclerItemClickListener {
lvContact = (RecyclerView) findViewById(R.id.lvContact);
assert lvContact != null;
lvContact.setHasFixedSize(true);
contactListAdapter = new ContactListAdapter(this);
contactListAdapter.setOnItemClickListener(this);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
lvContact.setLayoutManager(layoutManager);
lvContact.setAdapter(contactListAdapter);
...
@Override
public void onItemClick(int position, View view) {
CardView c = (CardView) view;
c.setEnabled(false);
ActActivity.start(this, contactListAdapter.getItem(position));
}
...
Override
protected void onResume() {
super.onResume();
CardView cardView1 = (CardView) findViewById(R.id.singlecard_view1);
cardView1.setEnabled(true);
}
RecyclerView的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.v050.MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" >
</include>
<LinearLayout
android:id="@+id/todoListLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_above="@+id/s4"
android:background="@color/background4main"
android:layout_marginTop="6dp"
android:layout_marginStart="6dp"
android:layout_marginLeft="6dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="6dp"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="@+id/lvContact"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
<TextView
android:id="@+id/s4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:background="@color/colorPrimary"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:clickable="true" />
</RelativeLayout>
CardView的xml文件:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/singlecard_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="4dp"
android:foreground="?android:attr/selectableItemBackground"
android:longClickable="true" >
Logcat输出不喜欢onResume()部分中的“ cardView1.setEnabled(true)”行:
11-01 23:22:54.814 1399-1399/com.example.v50 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {com.example.v50/com.v050.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
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.v050.MainActivity.onResume(MainActivity.java:279)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
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)
一个可行的建议答案是:
...
@Override
public void onItemClick(int position, final View view) {
view.setEnabled(false);
ActActivity.start(this, contactListAdapter.getItem(position));
view.post(new Runnable() {
@Override
public void run() {
view.setEnabled(true);
}
});
}
这与使用onResume的答案相比如何?
这是问题.....
CardView
在recycle view
,您正在尝试从main layout
而不是item layout
....
这是您解决问题的方法。
创建cardview
变量为global
.......
private CardView cardview;
当item clicked
时使用...
@Override
public void onItemClick(int position, View view) {
cardview = (CardView) view;
cardview.setEnabled(false);
ActActivity.start(this, contactListAdapter.getItem(position));
}
注意:-始终尝试声明已在许多方法中使用过的全局变量 。
编辑:-尝试在onResume
首次运行...。
并把条件放在简历上....
Override
protected void onResume() {
super.onResume();
if(cardview!=null){
cardview.setEnabled(true);
}
上述问题将解决您的首次运行问题....
您可以在活动的onCreate而不是onResume中初始化名片视图。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CardView cardView1 = (CardView)findViewById(R.id.singlecard_view1);
}
Override
protected void onResume() {
super.onResume();
cardView1.setEnabled(true);
}
在以下代码之前,您是否已初始化RecyclerView联系人列表:
CardView cardView1 =(CardView)findViewById(R.id.singlecard_view1);
如果尚未创建,则将不会创建名片视图,并且不会创建findViewById(R.id.singlecard_view1);
将返回null。
编辑:
我认为在完全可见活动之前,RecyclerView的项目视图不会初始化,这会导致findViewById(R.id.singlecard_view1)
返回null。
但是正如您所说的,您只希望编辑活动仅启动一次。 您可以通过实现
->在清单文件中为“ ActActivity”添加android:launchMode="singleTask"
。
要么
->将FLAG_ACTIVITY_CLEAR_TASK添加到Intent。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.