我正在使用包含三个片段的Activity
经理标签布局。我在SQLiteHelper
中调用了类Db扩展TabtOneFragment
,但它无效,并在Db文件中的Line处显示此错误。
SQLiteDatabase db = getWritableDatabase();
我使用Db db = new Db(getActivity());
获取上下文,但它也无法正常工作。任何帮助都会非常有帮助,拜托!
这是Db类:
package com.example.hismartversearch;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Db extends SQLiteOpenHelper {
public Db(Context context) {
super(context, "Db.sqlite", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
public Cursor getdata(String sql) {
// error at this line below
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.rawQuery(sql, null);
return c;
}
public void querydata(String sql) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL(sql);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void delete_table(String table_name) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL("delete from " + table_name);
}
public void delete_row(String tenbang, String tencot, String giatri) {
SQLiteDatabase db = getWritableDatabase();
db.delete(tenbang, tencot + "=" + giatri, null);
}
}
这是TabOneFragment:
public class TabOneFragment extends Fragment implements SearchView.OnQueryTextListener {
Db db = new Db(getActivity());
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_one_fragment, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
final RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(5), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
return view;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
// error at line in logcat when I create table.
public void LoadDtaWeb() {
db.querydata("Create table if not exists tbl_mon_app (_ID integer primary key, IDMon integer not null, IDCH integer not null, TenMon text not null, Gia text not null, ImgUrl text not null, ImgLocal text)");
}
}
//some text hidden to save space display
这是Logcat:
12-15 13:15:30.763 29533-29533 / com.example.hieul.hismartversearch E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.example.hieul.hismartversearch,PID:29533 java.lang.NullPointerException:尝试调用虚方法' android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String,int,android.database.sqlite.SQLiteDatabase $ CursorFactory,android。 database.DatabaseErrorHandler)'在null对象引用上 在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223) 在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) 在com.example.hieul.hismartversearch.Db.querydata(Db.java:33) 在com.example.hieul.hismartversearch.TabOneFragment.LoadDtaWeb(TabOneFragment.java:309) 在com.example.hieul.hismartversearch.TabOneFragment.onCreateView(TabOneFragment.java:131) 在android.support.v4.app.Fragment.performCreateView(Fragment.java:2261) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) 在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819) 在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797) 在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590) 在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377) 在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332) 在android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2209) 在android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649) 在android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1238) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1086) 在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1616) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:719) 在android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:91) 在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1361) 在android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:789) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:747) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:629) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:747) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:629) 在android.view.View.measure(View.java:20151) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:3143) 在android.view.View.measure(View.java:20151) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594) 在android.vi
答案 0 :(得分:3)
FRAGMENT
部分。onCreate(SQLiteDatabase db)
。
onCreate(SQLiteDatabase db)
这是我们编写create table语句的地方。这是 在创建数据库时调用。
代码
public class TabOneFragment extends Fragment implements SearchView.OnQueryTextListener {
public Db db ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_one_fragment, container, false);
db = new Db(getActivity());
并且
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_STATEMENT);
}
答案 1 :(得分:0)
public static final String TABLE_NAME = "table_name";
String CREATE_TABLE_SAMPLE = "create table " + TABLE_NAME + " (" + COLUMN_XXX_ID + " UNIQUE,"
+ COLUMN_XXX_NAME + "," + COLUMN_XXXX +
"," + COLUMN_XXXXX + ");";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_SAMPLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}