gpt4 book ai didi

android - onCreateLoader 返回的对象不能是非静态内部成员类 : DumbLoader

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:17 27 4
gpt4 key购买 nike

我正在尝试使用数据库(Sqlite 本地数据库)实现 Cursor LoaderCustom Cursor Adapter我的安卓项目。实际上,我想借助 Cursor Loader 和 Adapter` 将本地数据库中的数据异步提供给 ListView 。下面是我的类、子类和方法

        // Home Activity class
public class HomeActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
DotCursorAdapter mAdapter;
private ListView lv;
private final int LOADER_ID = 1932;

DatabaseHandler dbHelper = new DatabaseHandler(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
lv = (ListView) findViewById(R.id.lists);


mAdapter = new DotCursorAdapter(this, null,0);
lv.setAdapter(mAdapter);

getSupportLoaderManager().initLoader(LOADER_ID, null, this);
}





@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new DumbLoader(this);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
mAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
mAdapter.swapCursor(null);
}



/**
* DumbLoader sub class
*/
public class DumbLoader extends CursorLoader {
private static final String TAG = "DumbLoader";
public DumbLoader(Context context) {
super(context);
}
@Override
public Cursor loadInBackground() {
Cursor c = dbHelper.fetchAllCountries();

return c;

}
}






/**
* DotCursor Adapter sub class
*/
public final class DotCursorAdapter extends CursorAdapter {

public DotCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}



@Override
public void bindView(View view, Context context, Cursor cursor) {

// Find fields to populate in inflated template
TextView tvBody = (TextView) view.findViewById(R.id.header_text);

String body = cursor.getString(cursor.getColumnIndexOrThrow("fname"));
// Populate fields with extracted properties
tvBody.setText(body);


}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_cardview_activity, parent, false);
}
}
}

下面也是我的数据库处理程序方法,它从数据库中获取数据。

public  Cursor fetchAllCountries() {
SQLiteDatabase db = this.getReadableDatabase();

Cursor mCursor = db.query(TABLE_LOGIN, new String[] {KEY_ID,KEY_FIRSTNAME},
null, null, null, null, null);

if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

问题在执行上面的代码时,它会从日志猫中给出一条消息错误并使应用程序崩溃。

主要错误

Object returned from onCreateLoader must not be a non-static inner member class

下面也是我的日志猫的详细信息

09-21 16:13:25.563: D/AbsListView(26541): Get MotionRecognitionManager 09-21 16:13:25.568: D/AndroidRuntime(26541): Shutting down VM 09-21 16:13:25.573: W/dalvikvm(26541): threadid=1: thread exiting with uncaught exception (group=0x411372a0) 09-21 16:13:25.573: E/AndroidRuntime(26541): FATAL EXCEPTION: main 09-21 16:13:25.573: E/AndroidRuntime(26541): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alliswell.alliswell/com.alliswell.alliswell.HomeActivity}: java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be a non-static inner member class: DumbLoader{418dc668 id=0} 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.app.ActivityThread.access$700(ActivityThread.java:140) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.os.Handler.dispatchMessage(Handler.java:99) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.os.Looper.loop(Looper.java:137) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.app.ActivityThread.main(ActivityThread.java:4921) 09-21 16:13:25.573: E/AndroidRuntime(26541): at java.lang.reflect.Method.invokeNative(Native Method) 09-21 16:13:25.573: E/AndroidRuntime(26541): at java.lang.reflect.Method.invoke(Method.java:511) 09-21 16:13:25.573: E/AndroidRuntime(26541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 09-21 16:13:25.573: E/AndroidRuntime(26541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 09-21 16:13:25.573: E/AndroidRuntime(26541): at dalvik.system.NativeStart.main(Native Method) 09-21 16:13:25.573: E/AndroidRuntime(26541): Caused by: java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be a non-static inner member class: DumbLoader{418dc668 id=0} 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.support.v4.app.LoaderManagerImpl$LoaderInfo.start(LoaderManager.java:257) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.support.v4.app.LoaderManagerImpl.doStart(LoaderManager.java:714) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.app.Activity.performStart(Activity.java:5198) 09-21 16:13:25.573: E/AndroidRuntime(26541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083) 09-21 16:13:25.573: E/AndroidRuntime(26541): ... 11 more

我认为我的代码有问题,如果有人能帮助我解决这个问题,我将不胜感激。谢谢。

最佳答案

你有两种方法来解决这个问题:

1.- 创建 DumbLoader.java 文件并设置如下:

public class DumbLoader extends CursorLoader {

private DatabaseHandler mDBHelper;

public DumbLoader(Context context, DatabaseHandler dbHelper) {
super(context);
mDBHelper = dbHelper;
}

@Override
public Cursor loadInBackground() {
return mDBHelper.fetchAllCountries();
}
}

2.- 对 Activity 的 DumbLoader 嵌套类使用 static 修饰符,使其如下所示:

public class HomeActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {

//onCreate(), etc...

//Make DumbLoader nested class STATIC
public static class DumbLoader extends CursorLoader {

private DatabaseHandler mDBHelper;

public DumbLoader(Context context, DatabaseHandler dbHelper) {
super(context);
mDBHelper = dbHelper;
}

@Override
public Cursor loadInBackground() {
return mDBHelper.fetchAllCountries();
}
}

关于android - onCreateLoader 返回的对象不能是非静态内部成员类 : DumbLoader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32700372/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com