我正在创建一个地址簿应用程序,我想从联系人地址簿数据库中获取联系人图像。我一直得到这个Uri错误。似乎错误是在行:97是@ thumb.setImageURI(uri);
。请帮我做点什么。感谢。
这是我的AddressBook.java类
package com.rufflez.viewpagerexample;
import android.support.v4.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentUris;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
public class AddressBook extends ListFragment implements
LoaderCallbacks<Cursor>{
private CursorAdapter mAdapter;
public ListView listView;
public Cursor cursor;
public static final String[] FROM = { ContactsContract.Contacts.DISPLAY_NAME_PRIMARY, ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
private static final int[] TO = { R.id.text_name, R.id.contact_thumbnail };
int layout = R.layout.contact_list_item;
// Name should be displayed in the text1 TextView in item layout
private Context context;
public View view;
public static Uri uri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// delete list if already there (old version)
if (!(listView == null)){
listView.setAdapter(null);
}
// create adapter once
context = getActivity();
int layout = android.R.layout.simple_list_item_1;
Cursor c = null; // there is no cursor yet
int flags = 0; // no auto-requery! Loader requeries.
// put List in adapter
mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);
} // end onCreate
// columns requested from the database
private static final String[] PROJECTION = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts.IN_VISIBLE_GROUP
};
// this goes in the CursorLoader parameter list, it filters
// out only those contacts who have a phone number
private static final String SELECTION =
ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";
// Empty public constructor, required by the system
public AddressBook() {}
// A UI Fragment must inflate its View (all fragments must override onCreateView)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the fragment layout
View view = inflater.inflate(R.layout.contact_list_view,
container, false);
listView = (ListView) view.findViewById(R.id.list);
// set Contact photo to ImageView
AddressBook frag = new AddressBook();
ImageView thumb = (ImageView)view.findViewById(R.id.contact_thumbnail);
frag.getPhotoUriFromID(ContactsContract.Contacts._ID);
thumb.setImageURI(uri);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// and tell loader manager to start loading
// getLoaderManager().initLoader(0, null, this);
listView.setAdapter(mAdapter);
listView.setFastScrollEnabled(true);
}
// a CursorLoader does a query in the background
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// load from the "Contacts table"
Uri contentUri = Contacts.CONTENT_URI;
// no sub-selection, no sort order, simply every row
// projection says we want just the _id and the name column
return new CursorLoader(getActivity(),
contentUri,
PROJECTION,
SELECTION,
null,
ContactsContract.Contacts.DISPLAY_NAME
);
}
private Uri getPhotoUriFromID(String id) {
try {
Cursor cur = context.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ id
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cur != null) {
if (!cur.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
uri = Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
return uri;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Once cursor is loaded, give it to adapter
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// Delete the reference to the existing Cursor,
// so it can recycle it
mAdapter.swapCursor(null);
}
}
这是contact_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
这是我的contact_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/contact_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"/>
</LinearLayout>
这是我的Logcat错误
09-03 20:08:15.241: E/AndroidRuntime(5257): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
09-03 20:08:15.241: E/AndroidRuntime(5257): at com.rufflez.viewpagerexample.AddressBook.onCreateView(AddressBook.java:97)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.view.View.measure(View.java:18580)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.widget.LinearLayout.measureVertical(LinearLayout.java:874)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.view.View.measure(View.java:18580)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.view.View.measure(View.java:18580)
09-03 20:08:15.241: E/AndroidRuntime(5257): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
09-03 20:08:15.241: E/AndroidRuntime(5257): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
答案 0 :(得分:1)
你试图设置一个不存在的uri&#34; ... thumb.setImageURI(uri);将永远为空。
缩略图我假设它在你的列表视图中,所以...你需要设置:
thumb.setImageURI(URI);在自定义适配器内。
你现在这样做的方式(代码中的错误位置的详细信息),一旦应用程序加载,即使在设置适配器,获取数据等之前也试图获取uri ... uri总是为null
答案 1 :(得分:0)
您的片段使用contact_list_view.xml
布局,其中只包含一个ListView
。当该列表首次膨胀时,它不包含子项,因此(ImageView)view.findViewById(R.id.contact_thumbnail);
将返回null,因为您只拥有一个ListView
。
核心问题是适配器的工作是为ListView
充气和填充子视图。您不应该尝试从片段中引用contact_list_item.xml
中的任何视图。这些视图不仅完全有可能不存在,这样做也会限制您只使用列表的一行,而不是用每个行填充各自的数据。
答案 2 :(得分:0)
public static Uri uri;
未初始化(总是null
)。
答案 3 :(得分:0)
我和Uri有问题。重新创建活动时,它将变量Uri的值设置为null。这个事实使我的应用程序崩溃了,因为它用它保存了实例。怎么预防这个?不知何故保存了Uri变量的值。要sqlite,sharedpreferences或bundle或任何其他选择。并且当重新创建时检查它的值是否为空。如果是真的,从内存中获取保存的值。正如我提醒调用Uri.parseString或smth类似。我想你有这样的问题。 祝你好运!