2017-11-08 16 views
0

getLoaderManager().initLoader() 메서드를 사용하여 LoaderManager을 호출 할 때마다 잘못된 제 3 인수 유형에 대해 오류가 발생합니다. 세 번째 인수로 this을 전달합니다. 아무도 왜이 오류가 발생하는지 말할 수 있습니까? 내 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.app) 및 안드로이드 지원 라이브러리 (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