2017-04-24 14 views
0

FABonClickListenerにあるIntentは、LoaderManager.LoaderCallbacks<Cursor>とそのメソッドを実装した後にアプリがクラッシュすることがありますが、これらのメソッドを実装する前にアプリは正常に動作していました。 なぜonCreateLoaderonLoadFinished、およびonLoaderResetの方法でFABの停止が機能するのですか?CursorLoaderでインテントが機能しなくなりますか?

注:アプリのすべての機能が現在動作しているわけではありません。削除: 私はまだ更新方法を検討中です。

//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); 
    } 
+0

ないでくださいコードをオフサイトにリンクするだけです。質問自体には[mcve]と、クラッシュからのスタックトレースを含める必要があります。 –

+0

は質問を編集します –

答えて

0

問題はURI

//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を初期化するということでした。しかし、私はloaderのみURIがnullでなく、初期化している必要があります

//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); 
     } 
関連する問題