当我在模拟器上运行我的Android应用程序时崩溃(不幸的是,CursoLoader已经停止)。我在logcat中遇到以下错误:
01-05 14:46:45.812: D/dalvikvm(1505): Not late-enabling CheckJNI (already on)
01-05 14:46:45.964: E/Trace(1505): error opening trace file: No such file or directory (2)
01-05 14:46:47.071: D/gralloc_goldfish(1505): Emulator without GPU emulation detected.
01-05 14:46:49.841: D/AndroidRuntime(1505): Shutting down VM
01-05 14:46:49.841: W/dalvikvm(1505): threadid=1: thread exiting with uncaught exception (group=0xb2f93288)
01-05 14:46:49.854: E/AndroidRuntime(1505): FATAL EXCEPTION: main
01-05 14:46:49.854: E/AndroidRuntime(1505): java.lang.NullPointerException
01-05 14:46:49.854: E/AndroidRuntime(1505): at be.howest.mad.lab16.oef1.CursorLoaderActivity.onLoadFinished(CursorLoaderActivity.java:50)
01-05 14:46:49.854: E/AndroidRuntime(1505): at be.howest.mad.lab16.oef1.CursorLoaderActivity.onLoadFinished(CursorLoaderActivity.java:1)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:482)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:450)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.content.Loader.deliverResult(Loader.java:143)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.content.CursorLoader.deliverResult(CursorLoader.java:113)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.content.CursorLoader.deliverResult(CursorLoader.java:43)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:254)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:91)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.os.Looper.loop(Looper.java:137)
01-05 14:46:49.854: E/AndroidRuntime(1505): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-05 14:46:49.854: E/AndroidRuntime(1505): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 14:46:49.854: E/AndroidRuntime(1505): at java.lang.reflect.Method.invoke(Method.java:511)
01-05 14:46:49.854: E/AndroidRuntime(1505): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-05 14:46:49.854: E/AndroidRuntime(1505): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-05 14:46:49.854: E/AndroidRuntime(1505): at dalvik.system.NativeStart.main(Native Method)
CursorLoaderActivity.java:
package be.howest.mad.lab16.oef1;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.widget.SimpleCursorAdapter;
import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
public class CursorLoaderActivity extends ListActivity implements LoaderCallbacks<Cursor> {
private static final int CONTACTS_LOADER = 0;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] { Contacts.DISPLAY_NAME };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to, 0);
setListAdapter(adapter);
getLoaderManager().initLoader(CONTACTS_LOADER, null, this);
}
private static String[] projection = new String[]{
Contacts._ID,
Contacts.DISPLAY_NAME,
};
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if(id == CONTACTS_LOADER) {
CursorLoader cursorLoader = new CursorLoader(this, Contacts.CONTENT_URI,projection, null, null, null);
return cursorLoader;
}
return null;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
adapter.swapCursor(arg1);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
adapter.swapCursor(null);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="be.howest.mad.lab16.oef1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="be.howest.mad.lab16.oef1.CursorLoaderActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
有谁知道如何解决这个问题?谢谢!
答案 0 :(得分:0)
修正:onCreate方法存在问题。
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
null, from, to, 0);