Java 通讯簿应用程序-@SuppressWarnings
我正在使用EclipseADT构建一个地址簿应用程序,其中一些代码有问题。我收到有关SimpleCursorAdapter和deactivate的@SuppressWarnings 将@SuppressWarnings“depresiation”添加到onCreate 将@SuppressWarnings“depresiation”添加到桌面 我该如何解决这个问题Java 通讯簿应用程序-@SuppressWarnings,java,adt,addressbook,Java,Adt,Addressbook,我正在使用EclipseADT构建一个地址簿应用程序,其中一些代码有问题。我收到有关SimpleCursorAdapter和deactivate的@SuppressWarnings 将@SuppressWarnings“depresiation”添加到onCreate 将@SuppressWarnings“depresiation”添加到桌面 我该如何解决这个问题 // AddressBook.java // Main activity for the Address Book app. pac
// 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
请参阅eclipse提供的所有警告列表
回答你的问题。您必须在该方法之前添加@SuppressWarnings
和是Java语言规范所要求的,因此应该对所有编译器都有效。弃用警告意味着库的作者已将您使用的类、方法或变量标记为不适合将来使用,您应该避免使用有问题的项。文档通常指定它的问题是什么以及使用什么来代替。例如:
此构造函数在API级别11中被弃用。不鼓励使用此选项,因为它会导致在应用程序的UI线程上执行光标查询,从而导致响应性差,甚至应用程序没有响应错误。或者,将LoaderManager与游标Loader一起使用
如果您必须使用不推荐使用的功能,您可以简单地在带有警告的行中添加@SuppressWarningDeprecation,但如果可能,您应该避免这样做。请阅读问题