gpt4 book ai didi

java - 在sqlite Android中向容器类添加数据

转载 作者:行者123 更新时间:2023-12-01 09:22:53 24 4
gpt4 key购买 nike

我有一个容器类ServiceMeasureCollection:

import java.util.ArrayList;

public class ServiceMeasureCollection {
public String service_id="";
public String service_type="";
public String service_measure="";

public static ArrayList<ServiceMeasureCollection> data_arr=null;
public ServiceMeasureCollection(String service_id,String service_type, String service_measure){

this.service_id=service_id;
this.service_type=service_type;
this.service_measure=service_measure;
}
}

我想从 ArrayList 调用此类并用 sqlite 数据填充它,如下所示:

public ArrayList<ServiceMeasureCollection> getServiceProviderServices() {
ServiceMeasureCollection.data_arr = new ArrayList<ServiceMeasureCollection>();

Log.d("test", "testing if getServProv Services works " );
String selectQuery = "SELECT * FROM " + TABLE_SERVICEPROVSERVICE;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Log.d(TAG, "testing if getServProv Services works " );
// Move to first row
cursor.moveToFirst();

if (cursor.getCount() > 0) {
do {
ServiceMeasureCollection.data_arr.add(new ServiceMeasureCollection(cursor.getString(0),
cursor.getString(8),cursor.getString(1)));

} while (cursor.moveToNext());
}

cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " +ServiceMeasureCollection.data_arr.toString());

return ServiceMeasureCollection.data_arr;
}

每次我尝试通过 db.getServiceProviderServices() 在 Main 中调用它时,我都会收到空指针异常。我猜这是因为我初始化了 data_arr=Null。有谁知道如何解决这个问题或真正的问题是什么?

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.monobono.monobonoapp, PID: 4458
java.lang.NullPointerException
at de.monobono.monobonoapp.activity.ProviderServiceListFragment.onViewCreated(ProviderServiceListFragment.java:78)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5147)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)

编辑:

public class ProviderServiceListFragment extends Fragment {

View myView;
public String stampid;
private ListView listView;
private ListView listView2;
private ListView listView3;
public TextView dr_name;
private ProgressDialog pDialog;
private SQLiteHandler connection;
private SQLiteHandler db;
private ServiceMeasureAdapter list_adapter;
private ListView lv_android;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.service_measure_list_section, container,
false);
return myView;

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
stampid = getArguments().getString("stampID");
super.onViewCreated(view, savedInstanceState);

db.getServiceProviderServices();

} }

最佳答案

在调用 onViewCreated 时,您的成员变量 private SQLiteHandler db 为 null。这会导致您的 NullPointerException。您必须在 Fragment 的 onCreateView 函数中添加类似的内容

db = new SQLiteHandler(/*Required parameters*/);

关于java - 在sqlite Android中向容器类添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40042026/

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