活动意外停止,没有崩溃

时间:2018-08-18 10:54:28

标签: android android-activity

我有一个CardViews的RecyclerView。 每当单击RecyclerView的一项时,我都会尝试打开一个新的Activity。 然后我看到用户是否快速两次单击了卡片,活动打开了两次,所以我添加了一个布尔值,称为response,以忽略第二次触摸。

但是现在我遇到了另一个错误。当我快速单击两次卡片时,应用程序关闭,我看到了手机背景,当我再次单击启动器图标时,我看到了我将要看到的第二项活动。

这是我快速单击两次卡片时的Logcat。

logcat:

onPause: MainActivity
onCreate: ProductGroupDetailActivity
onStart: ProductGroupDetailActivity
onResume: ProductGroupDetailActivity
onPause: ProductGroupDetailActivity
onStop: MainActivity
onStop: ProductGroupDetailActivity

AppActivity(我已经从此类扩展了所有活动):

package project.activities;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import project.G;

public abstract class AppActivity extends AppCompatActivity {

public static final int LOADING_DELAY = 500;

private static boolean responding = true;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(G.LOG_TAG, "onCreate: " + this.getClass().getSimpleName());
}

@Override
protected void onStart() {
    super.onStart();
    Log.i(G.LOG_TAG, "onStart: " + this.getClass().getSimpleName());
}

@Override
protected void onResume() {
    super.onResume();
    Log.i(G.LOG_TAG, "onResume: " + this.getClass().getSimpleName());
}

@Override
protected void onPause() {
    super.onPause();
    Log.i(G.LOG_TAG, "onPause: " + this.getClass().getSimpleName());
}

@Override
protected void onStop() {
    super.onStop();
    Log.i(G.LOG_TAG, "onStop: " + this.getClass().getSimpleName());
}

@Override
protected void onRestart() {
    super.onRestart();
    Log.i(G.LOG_TAG, "onRestart: " + this.getClass().getSimpleName());
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.i(G.LOG_TAG, "onDestroy: " + this.getClass().getSimpleName());
}

public static boolean isResponding() {
    return responding;
}

public static void disableResponding() {
    responding = false;
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            responding = true;
        }
    }, LOADING_DELAY);
 }
}

这是我的RecyclerView适配器:

package project.adapters;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.company.app.R;

import java.util.ArrayList;

import project.helpers.FileHelper;
import project.structure.data.ProductsGroup;

public class GroupProductsRecyclerAdapter extends RecyclerView.Adapter<GroupProductsRecyclerAdapter.Holder> {

    private ArrayList<ProductsGroup> productsGroups;
    private View.OnClickListener onClickListener;

    public GroupProductsRecyclerAdapter(ArrayList<ProductsGroup> productsGroups, View.OnClickListener onClickListener) {
        this.productsGroups = productsGroups;
        this.onClickListener = onClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item, parent, false);
        return new Holder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int position) {
        if (productsGroups.get(position).getImage() != null && productsGroups.get(position).getImage().length() > 0) {
            holder.imgItem.setImageBitmap(FileHelper.getImage(productsGroups.get(position).getImage()));
        }
        holder.txtItem.setText(productsGroups.get(position).getName());
        holder.root.setTag(R.id.tag_productGroupNumber, productsGroups.get(position).getNumber());
        holder.root.setOnClickListener(onClickListener);
    }

    @Override
    public int getItemCount() {
        return productsGroups.size();
    }

    static class Holder extends RecyclerView.ViewHolder {

        ViewGroup root;
        TextView txtItem;
        ImageView imgItem;

        Holder(@NonNull View itemView) {
            super(itemView);
            txtItem = itemView.findViewById(R.id.txtItem);
            imgItem = itemView.findViewById(R.id.imgItem);
            root = (ViewGroup) itemView;
        }
    }
}

最后是适配器初始化:

GroupProductsRecyclerAdapter gpAdapter = new GroupProductsRecyclerAdapter(G.data.getDefaultLanguage().getProductsGroups(), new View.OnClickListener() {
                    @Override
                    public void onClick(final View view) {
                        if (AppActivity.isResponding()) {
                            AppActivity.disableResponding();
                            Intent intent = new Intent(MainActivity.this, ProductGroupDetailActivity.class);
                            intent.putExtra(ProductGroupDetailActivity.EXTRA_PRODUCT_GROUP_NUMBER, (Integer) view.getTag(R.id.tag_productGroupNumber));
                            MainActivity.this.startActivity(intent);
                        }
                    }
                });
                recycler.setAdapter(gpAdapter);

这是Logcat

    08-18 15:41:35.703 14262-14262/? E/adbd: recv: OPEN 000000f7 00000000 0015:73 68 65 6C 6C 3A 6C 6F 67 63 61 74 20 2D 76 20 6C 6F 6E 67 00 
    service_to_fd: shell:logcat -v long

    --------- beginning of system
08-18 15:41:36.315 24460-24460/? W/Notification: Use of stream types is deprecated for operations other than volume control
    See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
08-18 15:41:36.363 7191-7191/? E/CatchNotificationsService: Invalid notification data
08-18 15:41:37.325 24460-24460/? W/Notification: Use of stream types is deprecated for operations other than volume control
    See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
08-18 15:41:37.333 14644-14644/? W/ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:956 android.content.ContextWrapper.sendBroadcast:442 com.samsung.android.sm.widgetapp.SMWidgetService.a:66 com.samsung.android.sm.widgetapp.SMWidgetService.b:17 com.samsung.android.sm.widgetapp.d.handleMessage:31 
08-18 15:41:37.368 30894-30894/? W/SearchServiceClient: Starting with NO_SESSION handoverId is deprecated. Just don't. You will break.
08-18 15:41:37.385 7191-7191/? E/CatchNotificationsService: Invalid notification data
08-18 15:41:37.394 30894-30894/? W/SessionLifecycleManager: Handover failed. Creating new session controller.
08-18 15:41:37.404 30894-30894/? E/PBSessionCacheImpl: sessionId[135874783492680391] not persisted.
08-18 15:41:37.445 6335-6765/? E/ApplicationPackageManager: checkSettingsForIconTray value : 0
    checkSettingsForIconTray value : 0
08-18 15:41:37.459 5024-7390/? W/StorageManager: getStorageLowBytes lowPercent : 5, lowBytes : 1327564390, maxLowBytes : 524288000
08-18 15:41:37.478 10416-10416/? W/DeviceHAL: Device 0xf20ca500 open_input_stream: Invalid argument
08-18 15:41:37.500 10416-16755/? W/audio_hw_primary: start_input_stream echoReference effect is not supported.
08-18 15:41:37.593 30894-15920/? W/SpeechLevelGenerator: Really low audio levels detected. The audio input may have issues.
08-18 15:41:37.628 6407-6407/? E/SchedPolicy: add_tid_to_cgroup failed; fd=-1
08-18 15:41:37.634 16756-16756/? E//system/bin/webview_zygote32: failed to make and chown /acct/uid_99306: Permission denied
08-18 15:41:37.635 16756-16756/? E/Zygote: createProcessGroup(99306, 0) failed: Permission denied
08-18 15:41:37.635 16756-16756/? E/libpersona: Couldn't open the File - /data/system/users/0.xml - Permission denied
08-18 15:41:37.635 16756-16756/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:webview_zygote:s0 RAM:SEPF_SM-G935F_8.0.0_0011, [-1 -1 -1 -1 0 1]
08-18 15:41:37.722 16770-16770/? E/asset: setgid: Operation not permitted
08-18 15:41:37.803 10416-10416/? E/audio_hw_primary: adev_close_input_stream, set jack_in to null
08-18 15:41:37.889 16756-16756/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num3L.ttf
08-18 15:41:37.889 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.889 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num3L:null
    Error mapping font file /system/fonts/SamsungSans-Num3T.ttf
08-18 15:41:37.889 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.889 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num3T:null
    Error mapping font file /system/fonts/SamsungSans-Num4L.ttf
08-18 15:41:37.889 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.889 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num4L:null
    Error mapping font file /system/fonts/SamsungSans-Num4T.ttf
08-18 15:41:37.889 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.889 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num4T:null
    Error mapping font file /system/fonts/SamsungSans-Num35.ttf
08-18 15:41:37.889 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.889 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num35:null
08-18 15:41:37.890 16756-16756/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num45.ttf
08-18 15:41:37.890 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.890 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num45:null
    Error mapping font file /system/fonts/SamsungSans-Num3Lv.ttf
08-18 15:41:37.890 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.890 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num3LV:null
    Error mapping font file /system/fonts/SamsungSans-Num3R.ttf
08-18 15:41:37.890 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.890 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num3R:null
    Error mapping font file /system/fonts/SamsungSans-Num4Tv.ttf
08-18 15:41:37.890 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.890 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num4Tv:null
    Error mapping font file /system/fonts/SamsungSans-Num4Lv.ttf
08-18 15:41:37.890 16756-16756/? E/Minikin: Could not get cmap table size!
08-18 15:41:37.890 16756-16756/? E/Typeface: Unable to load Family: samsung-sans-num4Lv:null
08-18 15:41:37.912 30894-30894/? W/SearchServiceCore: Abort, client detached.
08-18 15:41:38.332 24460-24460/? W/Notification: Use of stream types is deprecated for operations other than volume control See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
08-18 15:41:38.368 7191-7191/? E/CatchNotificationsService: Invalid notification data
08-18 15:41:38.462 5024-5038/? E/memtrack: Couldn't load memtrack module
08-18 15:41:38.462 5024-5038/? W/android.os.Debug: failed to get memory consumption info: -1
08-18 15:41:38.470 30894-30894/? W/SearchServiceCore: Abort, client detached.
08-18 15:41:38.490 5024-5038/? E/memtrack: Couldn't load memtrack module

我真的被卡住了,不知道为什么活动会关闭?

谢谢大家阅读。

3 个答案:

答案 0 :(得分:0)

经过大量尝试和研究,我发现了问题

当我以应用程序样式注释此行代码时,我的问题就解决了。

<item name="android:windowIsTranslucent">true</item>

但是我不知道为什么吗?

答案 1 :(得分:0)

如果有人在使用gms(谷歌播放服务)库的android 6.x上存在此问题,则我在Google here

上创建了一个问题

答案 2 :(得分:0)

将以下属性添加到您在 AndroidManifest.xml 中的活动

android:launchMode = "singleTop"

示例

 <activity
            android:name=".MainActivity"
            android:launchMode = "singleTop"/>