我想将所有联系人保存到我的MySql数据库中,我是从Contactcontract表中检索的,当我打算将联系人保存到数据库时,它给我一个空指针异常的错误,我在oncreate函数中调用它,将contacts属性传递给其他类以进行进一步处理。
以下是我的源代码的一部分:
public class ContactsListFragment extends ListFragment implements
AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
// Defines a tag for identifying log entries
private static final String TAG = "ContactsListFragment";
// Bundle key for saving previously selected search result item
private static final String STATE_PREVIOUSLY_SELECTED_KEY =
"com.example.android.contactslist.ui.SELECTED_ITEM";
private static final Cursor Cursor = null;
public DBHandler db;
private ContactsAdapter mAdapter; // The main query adapter
private ImageLoader mImageLoader; // Handles loading the contact image in a background thread
private String mSearchTerm; // Stores the current search query term
// Contact selected listener that allows the activity holding this fragment to be notified of
// a contact being selected
private OnContactsInteractionListener mOnContactSelectedListener;
// Stores the previously selected search item so that on a configuration change the same item
// can be reselected again
private int mPreviouslySelectedSearchItem = 0;
// Whether or not the search query has changed since the last time the loader was refreshed
private boolean mSearchQueryChanged;
// Whether or not this fragment is showing in a two-pane layout
private boolean mIsTwoPaneLayout;
// Whether or not this is a search result view of this fragment, only used on pre-honeycomb
// OS versions as search results are shown in-line via Action Bar search from honeycomb onward
private boolean mIsSearchResultView = false;
/**
* Fragments require an empty constructor.
*/
public ContactsListFragment() {}
*
* @param query The contacts search query.
*/
public void setSearchQuery(String query) {
if (TextUtils.isEmpty(query)) {
mIsSearchResultView = false;
} else {
mSearchTerm = query;
mIsSearchResultView = true;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// corresponding resource size-qualified directory.
mIsTwoPaneLayout = getResources().getBoolean(R.bool.has_two_panes);
// Let this fragment contribute menu items
setHasOptionsMenu(true);
Log.d("Hope","Hope 3");
// Create the main contacts adapter
mAdapter = new ContactsAdapter(getActivity());
if (savedInstanceState != null) {
// If we're restoring state after this fragment was recreated then
// retrieve previous search term and previously selected search
// result.
mSearchTerm = savedInstanceState.getString(SearchManager.QUERY);
mPreviouslySelectedSearchItem =
savedInstanceState.getInt(STATE_PREVIOUSLY_SELECTED_KEY, 0);
}
mImageLoader = new ImageLoader(getActivity(), getListPreferredItemHeight()) {
@Override
protected Bitmap processBitmap(Object data) {
// This gets called in a background thread and passed the data from
// ImageLoader.loadImage().
return loadContactPhotoThumbnail((String) data, getImageSize());
}
};
HERE我正在尝试将所有联系人保存到mysqldatabase
**SavingContacts savingcontacts=new SavingContacts();
final String ContactID = Cursor.getString(ContactsQuery.ID);
final String Lookup = Cursor.getString(ContactsQuery.LOOKUP_KEY);
final String displayName = Cursor.getString(ContactsQuery.DISPLAY_NAME);
final String photoUri = Cursor.getString(ContactsQuery.PHOTO_THUMBNAIL_DATA);
savingcontacts.savingcontact(ContactID, displayName, photoUri, Lookup);**
// Set a placeholder loading image for the image loader
mImageLoader.setLoadingImage(R.drawable.ic_contact_picture_holo_light);
// Add a cache to the image loader
mImageLoader.addImageCache(getActivity().getSupportFragmentManager(), 0.1f);
}
这是我的logcat:
02-18 23:35:11.212: E/AndroidRuntime(21479): FATAL EXCEPTION: main
02-18 23:35:11.212: E/AndroidRuntime(21479): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.contactslist/com.example.android.contactslist.ui.ContactsListActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class fragment
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.os.Looper.loop(Looper.java:138)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.app.ActivityThread.main(ActivityThread.java:3701)
02-18 23:35:11.212: E/AndroidRuntime(21479): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 23:35:11.212: E/AndroidRuntime(21479): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 23:35:11.212: E/AndroidRuntime(21479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-18 23:35:11.212: E/AndroidRuntime(21479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-18 23:35:11.212: E/AndroidRuntime(21479): at dalvik.system.NativeStart.main(Native Method)
02-18 23:35:11.212: E/AndroidRuntime(21479): Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class fragment
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-18 23:35:11.212: E/AndroidRuntime(21479): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.app.Activity.setContentView(Activity.java:1657)
02-18 23:35:11.212: E/AndroidRuntime(21479): at com.example.android.contactslist.ui.ContactsListActivity.onCreate(ContactsListActivity.java:65)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
02-18 23:35:11.212: E/AndroidRuntime(21479): ... 11 more
02-18 23:35:11.212: E/AndroidRuntime(21479): Caused by: java.lang.NullPointerException
02-18 23:35:11.212: E/AndroidRuntime(21479): at com.example.android.contactslist.ui.ContactsListFragment.onCreate(ContactsListFragment.java:198)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
02-18 23:35:11.212: E/AndroidRuntime(21479): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
02-18 23:35:11.212: E/AndroidRuntime(21479): ... 19 more