2014-04-02 66 views
0

我正在使用Elipse ADT來構建地址簿應用程序,並且遇到一些代碼問題。我收到@SupressWarningsSimpleCursorAdapterdeactivate地址簿應用程序 - @SupressWarnings

添加@SupressWarnings 'depresiation' 到onCreate()

添加@SupressWarnings 'depresiation' 到onStop()

我該如何解決這個問題?

// AddressBook.java 
// Main activity for the Address Book app. 
package com.deitel.addressbook; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.CursorAdapter; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class AddressBook extends ListActivity 
{ 
    public static final String ROW_ID = "row_id"; // Intent extra key 
    private ListView contactListView; // the ListActivity's ListView 
    private CursorAdapter contactAdapter; // adapter for ListView 

    // called when the activity is first created 
    @Override 
    public void onCreate(Bundle savedInstanceState) > 
    { 
     super.onCreate(savedInstanceState); // call super's onCreate 
     contactListView = getListView(); // get the built-in ListView 
     contactListView.setOnItemClickListener(viewContactListener);  

     // map each contact's name to a TextView in the ListView layout 
     String[] from = new String[] { "name" }; 
     int[] to = new int[] { R.id.contactTextView }; 
     contactAdapter = new SimpleCursorAdapter(AddressBook.this, R.layout.contact_list_item, null, from, to); 
     setListAdapter(contactAdapter); // set contactView's adapter 
    } // end method onCreate 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); // call super's onResume method 

     // create new GetContactsTask and execute it 
     new GetContactsTask().execute((Object[]) null); 
    } // end method onResume 

    @Override 
    protected void onStop() 
    { 
     Cursor cursor = contactAdapter.getCursor(); // get current Cursor 

     if (cursor != null) 
     cursor.deactivate(); // deactivate it 

     contactAdapter.changeCursor(null); // adapted now has no Cursor 
     super.onStop(); 
    } // end method onStop 

    // performs database query outside GUI thread 
    private class GetContactsTask extends AsyncTask<Object, Object, Cursor> 
    { 
     DatabaseConnector databaseConnector = 
     new DatabaseConnector(AddressBook.this); 

     // perform the database access 
     @Override 
     protected Cursor doInBackground(Object... params) 
     { 
     databaseConnector.open(); 

     // get a cursor containing call contacts 
     return databaseConnector.getAllContacts(); 
     } // end method doInBackground 

     // use the Cursor returned from the doInBackground method 
     @Override 
     protected void onPostExecute(Cursor result) 
     { 
     contactAdapter.changeCursor(result); // set the adapter's Cursor 
     databaseConnector.close(); 
     } // end method onPostExecute 
    } // end class GetContactsTask 

    // create the Activity's menu from a menu resource XML file 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     super.onCreateOptionsMenu(menu); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.addressbook_menu, menu); 
     return true; 
    } // end method onCreateOptionsMenu 

    // handle choice from options menu 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // create a new Intent to launch the AddEditContact Activity 
     Intent addNewContact = 
     new Intent(AddressBook.this, AddEditContact.class); 
     startActivity(addNewContact); // start the AddEditContact Activity 
     return super.onOptionsItemSelected(item); // call super's method 
    } // end method onOptionsItemSelected 

    // event listener that responds to the user touching a contact's name 
    // in the ListView 
    OnItemClickListener viewContactListener = new OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) 
     { 
     // create an Intent to launch the ViewContact Activity 
     Intent viewContact = 
      new Intent(AddressBook.this, ViewContact.class); 

     // pass the selected contact's row ID as an extra with the Intent 
     viewContact.putExtra(ROW_ID, arg3); 
     startActivity(viewContact); // start the ViewContact Activity 
     } // end method onItemClick 
    }; // end viewContactListener 
} // end class AddressBook 
+0

請閱讀[如何編寫適當的格式](http://stackoverflow.com/editing-help)問題 – Baby

回答

0

請參考this SO questions查看所有提供的日蝕列表。

並回答你的問題。您必須在該方法之前添加@SupressWarnings

'unchecked''deprecation'是Java語言規範所要求的,所以對所有的編譯器都應該是有效的。

0

deprecation警告意味着您使用的類,方法或變量已被庫的作者標記爲不適合將來使用,並且您應該避免使用有問題的項目。文檔通常指定它的問題是什麼以及如何使用。例如,the doc for SimpleCursorAdapter says

這個構造函數是在API級別11推薦使用此選項氣餒,因爲它會導致應用程序的UI線程上執行光標查詢,從而可能會導致反應不佳,甚至應用程序無響應錯誤。作爲替代,使用帶有CursorLoader的LoaderManager。

如果有一個情況下,你絕對必須使用棄用的功能,你可以簡單地添加@SuppressWarnings("deprecation")與預警線,但你應該避免這樣做,如果在所有可能的。