0

我第一次尝试在 Android 中使用加载器概念。

这是我的测试代码:

package at.powersoftware.hello.loader;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

public class HelloLoaderActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    static final int LOADER_CONTACTS = 1;   // ID of the loader

    ListView list;
    SimpleCursorAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loaderactivity);

        list = (ListView) findViewById(R.id.listView);

        getSupportLoaderManager().initLoader(LOADER_CONTACTS, null, this);

        String[] from = {Contacts.DISPLAY_NAME, Contacts.TIMES_CONTACTED};
        int[] to = {R.id.tvDisplayName, R.id.tvTimesContacted};

  adapter = new SimpleCursorAdapter(this, R.layout.contactlist3, null, from, to, 0);

        list.setAdapter(adapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.loaderactivity, menu);
        return true;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

        String[] projection = {Contacts._ID, Contacts.DISPLAY_NAME, Contacts.TIMES_CONTACTED};
        String selection = null; //"1=1" or even "" works;  
    String[] selectionArgs = null;
        String sortOrder = Contacts.TIMES_CONTACTED + " desc, " + Contacts.DISPLAY_NAME; 

        CursorLoader cl = new CursorLoader(this, Contacts.CONTENT_URI, projection, selection, selectionArgs, sortOrder);

        return cl;

    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

        adapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {

        adapter.swapCursor(null);
    }

}

我想选择内容提供者的所有记录,因此我为“选择”参数指定了 null。但这给了我下面的错误。如果我提供像“1 = 1”甚至“”这样的选择标准,它就可以完美地工作。任何想法可能是什么问题?- 提前致谢!

08-01 13:28:06.092: E/AndroidRuntime(1528): FATAL EXCEPTION: main
08-01 13:28:06.092: E/AndroidRuntime(1528): java.lang.RuntimeException: Unable to start activity ComponentInfo{at.powersoftware.hello.loader/at.powersoftware.hello.loader.HelloLoaderActivity}: java.lang.NullPointerException: println needs a message
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.os.Looper.loop(Looper.java:123)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at java.lang.reflect.Method.invoke(Method.java:507)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at dalvik.system.NativeStart.main(Native Method)
08-01 13:28:06.092: E/AndroidRuntime(1528): Caused by: java.lang.NullPointerException: println needs a message
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.util.Log.println_native(Native Method)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.util.Log.i(Log.java:158)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at at.powersoftware.hello.loader.HelloLoaderActivity.onCreateLoader(HelloLoaderActivity.java:73)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.support.v4.app.LoaderManagerImpl.createLoader(LoaderManager.java:487)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.support.v4.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:496)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:550)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at at.powersoftware.hello.loader.HelloLoaderActivity.onCreate(HelloLoaderActivity.java:30)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-01 13:28:06.092: E/AndroidRuntime(1528):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-01 13:28:06.092: E/AndroidRuntime(1528):     ... 11 more
4

1 回答 1

1

@Jens:你说得对。在我的代码中,我有一行:

Log.i("HLA", cl.getSelection());

(为了更好的可读性,我删除了所有的 Log stmts)

当然,如果 selection 为 null,cl.getSelection() 也返回 null。

我到处找,但那里没有……

非常感谢你们!

@StenaviN:我们看到发布整个代码似乎是个好主意!

于 2012-08-02T10:08:27.963 回答