2017-11-08 6 views
0

LoaderManagerメソッドを使用してgetLoaderManager().initLoader()メソッドを呼び出すと、誤った第3引数型のエラーが発生します。私はthisを第3引数として渡します。誰も私にこのエラーが出ている理由を教えてもらえますか?私はonCreate()メソッドの最後にLoaderManagerを呼び出しました。ここでは以下の私のコードは次のとおりです。ローダーマネージャエラー

public class CatalogActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { 

    private static final int PET_LOADER = 0; 

    PetCursorAdapter mCursorAdapter; 

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

     // Setup FAB to open EditorActivity 
     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(CatalogActivity.this, EditorActivity.class); 
       startActivity(intent); 
      } 
     }); 

     // Find the ListView which will be populated with the pet data 
     ListView petListView = (ListView) findViewById(R.id.list); 

     // Find and set empty view on the ListView, so that it only shows when the list has 0 items. 
     View emptyView = findViewById(R.id.empty_view); 
     petListView.setEmptyView(emptyView); 

     mCursorAdapter = new PetCursorAdapter(this, null); 
     petListView.setAdapter(mCursorAdapter); 

     petListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
       Intent intent = new Intent(CatalogActivity.this, EditorActivity.class); 

       Uri currentPetUri = ContentUris.withAppendedId(PetEntry.CONTENT_URI, id); 

       intent.setData(currentPetUri); 

       startActivity(intent); 
      } 
     }); 

     getLoaderManager().initLoader(PET_LOADER, null, this); 
    } 

    private void insertPet() { 

     ContentValues values = new ContentValues(); 
     values.put(PetEntry.COLUMN_NAME, "Toto"); 
     values.put(PetEntry.COLUMN_BREED, "Terrier"); 
     values.put(PetEntry.COLUMN_GENDER, PetEntry.GENDER_MALE); 
     values.put(PetEntry.COLUMN_WEIGHT, 7); 

     Uri newUri = getContentResolver().insert(PetEntry.CONTENT_URI, values); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu options from the res/menu/menu_catalog.xml file. 
     // This adds menu items to the app bar. 
     getMenuInflater().inflate(R.menu.menu_catalog, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // User clicked on a menu option in the app bar overflow menu 
     switch (item.getItemId()) { 
      // Respond to a click on the "Insert dummy data" menu option 
      case R.id.action_insert_dummy_data: 
       insertPet(); 
       return true; 
      // Respond to a click on the "Delete all entries" menu option 
      case R.id.action_delete_all_entries: 
       // Do nothing for now 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public Loader<Cursor> onCreateLoader(int id, Bundle args) { 

     String[] projection = {PetEntry._ID, 
       PetEntry.COLUMN_NAME, 
       PetEntry.COLUMN_BREED}; 

     return new CursorLoader(this, 
       PetEntry.CONTENT_URI, 
       projection, 
       null, 
       null, 
       null); 
    } 

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

     mCursorAdapter.swapCursor(data); 
    } 

    @Override 
    public void onLoaderReset(Loader<Cursor> loader) { 

     mCursorAdapter.swapCursor(null); 
    } 
} 
+0

どのLoaderManager.LoaderCallbacksが実装していますか? 'android.app'、または' android.support.v4.app'からのものですか? – clownba0t

+0

うん、 'android.support.v4.app.LoaderManager' – mani

答えて

0

ローダーは、Androidのフレームワーク(android.app)とAndroidのサポートライブラリ(android.support.v4.app)の両方で提供されています。これらは互換性がないため、使用するコンポーネントを選択し、特定の場所(アクティビティなど)で使用しているすべてのコンポーネントが同じパッケージに含まれていることを確認する必要があります。 getSupportLoaderManager()戻るサポートライブラリコンポーネント(android.support.v4.app.LoaderManager)に対し、フレームワークコンポーネント(android.app.LoaderManager)を返しgetLoaderManager() - AppCompatActivity

両方を使用する能力を提供します。

コメントに記載されているとおり、LoaderManager.LoaderCallbacksコンポーネントをサポートライブラリからインポートしています。あなたが持っていると思う問題は、フレームワークのLoaderManagerコンポーネント(getLoaderManager()経由)でそれを使用しようとしているということです。

getLoaderManager()getSupportLoaderManager()に変更してみてください。また、何らかの理由でサポートライブラリを使用したくない場合は、代わりにandroid.app.LoaderManagerをインポートする必要があります。

+0

ありがとうございました。 – mani