【问题标题】:Does the CursorLoader makes the Intents stop working?CursorLoader 是否使 Intent 停止工作?
【发布时间】:2017-04-24 12:05:14
【问题描述】:

onClickListenerFAB 中的 Intent 在实现 LoaderManager.LoaderCallbacks<Cursor> 及其方法后会导致应用崩溃,但在实现这些方法之前应用运行良好。 为什么onCreateLoaderonLoadFinishedonLoaderReset 方法会使FAB 停止工作?

注意:目前并非应用的所有功能都可以使用,即:删除 我还在研究更新方法。

//The intent in the CatalogActivity "The main activity"    
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent newIntent = new Intent(CatalogActivity.this, EditorActivity.class);
                startActivity(newIntent);
            }



//After implementing these methods the EditorActivity, the FAB in the first activity makes the app crash
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = {
                PetEntry._ID,
                PetEntry.COLUMN_PET_NAME,
                PetEntry.COLUMN_PET_BREED,
                PetEntry.COLUMN_PET_GENDER,
                PetEntry.COLUMN_PET_WEIGHT
        };

        return new CursorLoader(this, mCurrentPetUri, projection, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

        if (data == null || data.getCount() < 1) {
            return;
        }

        if (data.moveToNext()) {
            String name = data.getString(data.getColumnIndex(PetEntry.COLUMN_PET_NAME));
            mNameEditText.setText(name);

            String breed = data.getString(data.getColumnIndex(PetEntry.COLUMN_PET_BREED));
            mBreedEditText.setText(breed);

            int weight = data.getInt(data.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT));
            mWeightEditText.setText(Integer.toString(weight));

            int gender = data.getInt(data.getColumnIndex(PetEntry.COLUMN_PET_GENDER));

            if (gender == PetEntry.GENDER_MALE){
                mGenderSpinner.setSelection(1);
            } else if (gender == PetEntry.GENDER_FEMALE) {
                mGenderSpinner.setSelection(2);
            } else {
                mGenderSpinner.setSelection(0);
            }

        }

    }



    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mNameEditText.setText("");
        mBreedEditText.setText("");
        mWeightEditText.setText("");
        mGenderSpinner.setSelection(0);
    }

【问题讨论】:

  • 请不要只在场外链接到您的代码。您需要在问题本身中包含minimal reproducible example,以及崩溃的堆栈跟踪。
  • 将编辑问题

标签: java android android-intent android-contentprovider loader


【解决方案1】:

问题是当URI 为空时我初始化了loader

//The old code
Intent intent = getIntent();

 mCurrentPetUri = intent.getData();

        if (mCurrentPetUri == null) {
            setTitle(getString(R.string.editor_activity_title_new_pet));

        } else {
            setTitle(getString(R.string.editor_activity_title_edit_pet));
        }
getLoaderManager().initLoader(1, null, this);

但我应该初始化 loader 只有 URI 不为空

//The new code
Intent intent = getIntent();

        mCurrentPetUri = intent.getData();

        if (mCurrentPetUri == null) {
            setTitle(getString(R.string.editor_activity_title_new_pet));

        } else {
            setTitle(getString(R.string.editor_activity_title_edit_pet));
            getLoaderManager().initLoader(1, null, this);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多