调用结果的活动开始时未激活活动

时间:2015-10-04 11:37:34

标签: android android-activity click listener

我有一个活动A,我从中开始一个活动结果B.在活动中,有一个包含回收者视图的片段。当活动B启动时,它使用相同的回收器视图适配器并单击正在使用的监听器活动A中包含的片段。问题是活动B似乎没有被破坏,这导致点击监听器出现问题并导致应用程序崩溃。我想知道为什么没有调用onPause或onDestroy活动b 。 我编辑了可能不重要的代码。

活动B

*

当我注释掉这行

public class Add_Custom extends AppCompatActivity implements RecyclerViewAdapter.ClickListener, com.wdullaer.materialdatetimepicker.date.DatePickerDialog.OnDateSetListener {

    private static final int PICK_IMAGE_REQUEST = 1;
    private FloatingActionButton floatingActionButton;
    ArrayList<Person> persons = new ArrayList<>();
    private RecyclerViewAdapter rcv;
    private ArrayList<Person> personRecyclerList = new ArrayList<>();
    private LocalDate date;
    private String name;
    private Snackbar snackbar;
    private boolean date_flag = false;
    private boolean user_flag = false;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private ImageView imageView;
    private String picturePath;
    private int image_color;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_custom);

        android.os.Handler handler = new android.os.Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                getName();
            }
        };
        handler.post(runnable);

        setUpRecyclerView();
        imageView = (ImageView) findViewById(R.id.image_toolbar);
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapseToolbar);
        floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
        final Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        floatingActionButton.setEnabled(false);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(true);
            actionBar.setTitle("Custom Event");
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        final String[] stringArray = genList(getApplicationContext());
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MaterialDialog.Builder(Add_Custom.this)
                        .title("Select Contact")
                        .autoDismiss(true)
                        .items(stringArray)
                        .widgetColor(image_color != 0 ? image_color : ContextCompat.getColor(Add_Custom.this, R.color.accentColor))
                        .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
                            @Override
                            public boolean onSelection(MaterialDialog materialDialog, Integer[] integers, CharSequence[] charSequences) {
                                for (int i = 0; i < integers.length; i++) {
                                    if (!personRecyclerList.contains(persons.get(integers[i]))) {
                                        personRecyclerList.add(persons.get(integers[i]));
                                        rcv.notifyItemInserted(personRecyclerList.size());
                                    }
                                }
                                return true;
                            }
                        })
                        .negativeColor(image_color != 0 ? image_color : ContextCompat.getColor(Add_Custom.this, R.color.accentColor))
                        .negativeText("Cancel")
                        .positiveColor(image_color != 0 ? image_color : ContextCompat.getColor(Add_Custom.this, R.color.accentColor))
                        .positiveText("Add")
                        .show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add__custom, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.done) {
            checkConstraints();
        }
        return super.onOptionsItemSelected(item);
    }

    public String[] genList(final Context context) {
        final DataBaseHelper db = new DataBaseHelper(context);
        final ArrayList<String> arrayList = new ArrayList<>();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                persons = db.getPeople();
                for (int i = 0; i < persons.size(); i++)
                    arrayList.add(persons.get(i).getName());
            }
        };
        runnable.run();
        floatingActionButton.setEnabled(true);
        return arrayList.toArray(new String[arrayList.size()]);
    }

    public void setUpRecyclerView() {
        final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
        rcv = new RecyclerViewAdapter(getApplicationContext());
        rcv.setClickListener(this);
        final LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
        rcv.setArrayList(personRecyclerList);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(llm);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(rcv);
    }

    @Override
    public void itemClick(View v, final int position) {
        final MaterialDialog materialDialog = new MaterialDialog.Builder(Add_Custom.this)
                .title("Remove Person")
                .positiveColor(image_color != 0 ? image_color : ContextCompat.getColor(Add_Custom.this, R.color.accentColor))
                .positiveText("Remove")
                .negativeColor(image_color != 0 ? image_color : ContextCompat.getColor(Add_Custom.this, R.color.accentColor))
                .negativeText("Cancel")
                .show();

        View positive = materialDialog.getActionButton(DialogAction.POSITIVE);
        View negative = materialDialog.getActionButton(DialogAction.NEGATIVE);

        positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                personRecyclerList.remove(position);
                rcv.notifyItemRemoved(position);
                if (materialDialog.isShowing())
                    materialDialog.dismiss();
            }
        });

        negative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (materialDialog.isShowing())
                    materialDialog.dismiss();
            }
        });
    }

    void checkConstraints() {
        if (personRecyclerList.size() > 0) {
            if (date_flag && user_flag) {
                DataBaseHelper db = new DataBaseHelper(getApplicationContext());
                Custom custom = new Custom(date, name);
                custom.setPersons(personRecyclerList);
                custom.setImage_path(picturePath);
                custom.setImage_color(image_color);
                long result = db.AddEvent(custom, DataBaseHelper.TYPE_CUSTOM);
                Intent intent = new Intent();
                if (result >= 0)
                    setResult(RESULT_OK, intent);
                else
                    setResult(RESULT_CANCELED, intent);
                finish();
            } else {
                snackbar = Snackbar.make(findViewById(R.id.cor_layout), "Incomplete Details", Snackbar.LENGTH_LONG);
                snackbar.setAction("Dismiss", clickListener);
                snackbar.show();
            }
        } else {
            snackbar = Snackbar.make(findViewById(R.id.cor_layout), "Select A Person", Snackbar.LENGTH_LONG);
            snackbar.setAction("Dismiss", clickListener);
            snackbar.show();
        }
    }

    private final View.OnClickListener clickListener = new View.OnClickListener() {
        public void onClick(View v) {
            snackbar.dismiss();
        }
    };

    public void getName() {
        final MaterialDialog dialog = new MaterialDialog.Builder(Add_Custom.this)
                .title("Name")
                .alwaysCallInputCallback()
                .inputType(InputType.TYPE_CLASS_TEXT)
                .cancelable(false)
                .negativeText("Cancel")
                .positiveText("Set")
                .input("Event Name", null, false, new MaterialDialog.InputCallback() {
                    @Override
                    public void onInput(MaterialDialog dialog, CharSequence input) {
                        name = String.valueOf(input);
                    }
                }).show();

        View positive = dialog.getActionButton(DialogAction.POSITIVE);
        View negative = dialog.getActionButton(DialogAction.NEGATIVE);

        negative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog.isShowing())
                    dialog.dismiss();
                Intent intent = new Intent();
                setResult(RESULT_CANCELED, intent);
                finish();
            }
        });

        positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                user_flag = true;
                if (name != null)
                    collapsingToolbarLayout.setTitle(name);
                if (dialog.isShowing())
                    dialog.dismiss();
            }
        });
    }

    @Override
    public void onDateSet(com.wdullaer.materialdatetimepicker.date.DatePickerDialog datePickerDialog, int year, int monthOfYear, int dayOfMonth) {
        try {
            date = new LocalDate(year, monthOfYear + 1, dayOfMonth);
            date_flag = true;
        } catch (Exception e) {
            date_flag = false;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("pause", "");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("destroyed", "");
    }
}

应用程序不再崩溃,一切正常。

rcv.setClickListener(this);

Recycler View Adapter

    10-04 09:08:02.683 7508-7508/atsystems.cal D/AndroidRuntime: Shutting down VM
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime: FATAL EXCEPTION: main
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime: Process: atsystems.cal, PID: 7508
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime: com.afollestad.materialdialogs.MaterialDialog$DialogException: Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at com.afollestad.materialdialogs.MaterialDialog.show(MaterialDialog.java:1253)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at com.afollestad.materialdialogs.MaterialDialog$Builder.show(MaterialDialog.java:1242)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at atsystems.cal.Add_Custom.itemClick(Add_Custom.java:273)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at atsystems.cal.RecyclerViewAdapters.RecyclerViewAdapter$ViewHolderCheese.onClick(RecyclerViewAdapter.java:94)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at android.view.View.performClick(View.java:4780)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19866)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-04 09:08:02.684 7508-7508/atsystems.cal E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

0 个答案:

没有答案