Android:如何在 onResume 中的 CardView 上设置启用(真)?
Android: how do I setEnabled(true) on a CardView in onResume?
我有一个带有 CardView 的 RecyclerView 列表。我在下面添加了以下代码以启动允许用户编辑 CardView 的 activity (ActActivity)。如果用户在 CardView 上连续快速单击多次,setEnabled(false)
代码用于防止 activity 的多个实例打开。我只想一次打开 activity 的一个实例,以便用户只编辑他们单击的单个 CardView。
我的问题是,当我添加 onResume()
部分以将 setEnabled()
重新设置为 "true" 时,应用程序崩溃了。当我删除 onResume() 部分时,setEnabled(false) 代码通过不允许 activity 的多个实例打开而正常工作,但问题是 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);
}
xml RecyclerView 文件:
<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>
xml CardView 文件:
<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.access0(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.access0(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 的答案相比如何?
您可以在 activity 的 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);
将 return 为空。
已编辑:
我认为 RecyclerView 项目的视图在 activity 完全可见之前不会被初始化,这会导致 findViewById(R.id.singlecard_view1)
return 为空。
但是正如您所说,您只希望编辑 activity 只启动一次。
您可以通过
-> 在清单文件中为 "ActActivity" 添加 android:launchMode="singleTask"
来实现。
或
-> 将 FLAG_ACTIVITY_CLEAR_TASK 添加到 Intent。
问题来了.....
CardView
在 recycle view
中,您正在尝试从 main layout
而不是 item layout
....
here is your solution for your problem....
创建 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
中的第一个 运行 试试这个...
并将条件放在Resume ....
Override
protected void onResume() {
super.onResume();
if(cardview!=null){
cardview.setEnabled(true);
}
以上问题将解决您的第一个运行问题....
我有一个带有 CardView 的 RecyclerView 列表。我在下面添加了以下代码以启动允许用户编辑 CardView 的 activity (ActActivity)。如果用户在 CardView 上连续快速单击多次,setEnabled(false)
代码用于防止 activity 的多个实例打开。我只想一次打开 activity 的一个实例,以便用户只编辑他们单击的单个 CardView。
我的问题是,当我添加 onResume()
部分以将 setEnabled()
重新设置为 "true" 时,应用程序崩溃了。当我删除 onResume() 部分时,setEnabled(false) 代码通过不允许 activity 的多个实例打开而正常工作,但问题是 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);
}
xml RecyclerView 文件:
<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>
xml CardView 文件:
<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.access0(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.access0(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 的答案相比如何?
您可以在 activity 的 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);
将 return 为空。
已编辑:
我认为 RecyclerView 项目的视图在 activity 完全可见之前不会被初始化,这会导致 findViewById(R.id.singlecard_view1)
return 为空。
但是正如您所说,您只希望编辑 activity 只启动一次。
您可以通过
-> 在清单文件中为 "ActActivity" 添加 android:launchMode="singleTask"
来实现。
或
-> 将 FLAG_ACTIVITY_CLEAR_TASK 添加到 Intent。
问题来了.....
CardView
在 recycle view
中,您正在尝试从 main layout
而不是 item layout
....
here is your solution for your problem....
创建 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
中的第一个 运行 试试这个...
并将条件放在Resume ....
Override
protected void onResume() {
super.onResume();
if(cardview!=null){
cardview.setEnabled(true);
}
以上问题将解决您的第一个运行问题....