我有一个带有很多textEdits的片段,当我按下按钮时,哪些字符串应该放在表格列中

问题描述 投票:0回答:1

我正在使用具有两个片段的视图分页器制作一个应用程序;其中一个具有许多不同的editTextes,如下图所示:ui of the fragment

[当用户单击Aggiungi(顶部的那个)按钮时,我想将每个字符串放在称为Puntostrada的表的列中]

[当您按下按钮时应用崩溃时

这里是创建数据库的地方:

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE PUNTOSTRADA ("
            + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + "MYSELF_PAST TEXT, "
            + "MYSELF_PRESENT TEXT , "
            + "MYSELF_FUTURE TEXT , "
            + "OTHERS_PAST TEXT , "
            + "OTHERS_PRESENT TEXT ,"
            + "OTHERS_FUTURE TEXT ,"
            + "WORLD_PAST TEXT ,"
            + "WORLD_PRESENT TEXT ,"
            + "WORLD_FUTURE TEXT ,"
            + "GOD_PAST TEXT ,"
            + "GOD_PRESENT TEXT ,"
            + "GOD_FUTURE TEXT);");
}

这里是片段的onCreate:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.fragment_punto, container, false);
     View button = view.findViewById(R.id.add_button);
      button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                    public void onClick(View v) {

                    String myselfh,myselfp,myselff,othersS,othersP,othersF,godS,godP,godF,worldS,worldP,worldF;
                    EditText myselfStoria = Objects.requireNonNull(getView()).findViewById(R.id.myself_story);
                    myselfh=myselfStoria.getText().toString();
                    EditText myselfPresent = Objects.requireNonNull(getView()).findViewById(R.id.myself_present);
                    myselfp=myselfPresent.getText().toString();
                    EditText myselfFuture =  Objects.requireNonNull(getView()).findViewById(R.id.myself_future);
                    myselff=myselfFuture.getText().toString();
                    EditText othersPast =  Objects.requireNonNull(getView()).findViewById(R.id.others_story);
                    othersS=othersPast.getText().toString();
                    EditText othersPresent =  Objects.requireNonNull(getView()).findViewById(R.id.others_present);
                    othersP=othersPresent.getText().toString();
                    EditText othersFuture =  Objects.requireNonNull(getView()).findViewById(R.id.others_future);
                    othersF=othersFuture.getText().toString();
                    EditText godPast =  Objects.requireNonNull(getView()).findViewById(R.id.god_story);
                    godS=godPast.getText().toString();
                    EditText godPresent =  Objects.requireNonNull(getView()).findViewById(R.id.god_present);
                    godP=godPresent.getText().toString();
                    EditText godFuture =  Objects.requireNonNull(getView()).findViewById(R.id.god_future);
                    godF=godFuture.getText().toString();
                    EditText WorldPast =  Objects.requireNonNull(getView()).findViewById(R.id.world_past);
                    worldS=WorldPast.getText().toString();
                    EditText worldPresent =  Objects.requireNonNull(getView()).findViewById(R.id.world_present);
                    worldP= worldPresent.getText().toString();
                    EditText WorldFuture =  Objects.requireNonNull(getView()).findViewById(R.id.world_future);
                    worldF=WorldFuture.getText().toString();
                    puntoValues.put("MYSELF_PAST",myselfh);
                    puntoValues.put("MYSELF_PRESENT",myselfp);
                    puntoValues.put("MYSELF_FUTURE",myselff);
                    puntoValues.put("OTHERS_PAST",othersS);
                    puntoValues.put("OTHERS_PRESENT",othersP);
                    puntoValues.put("OTHERS_FUTURE",othersF);
                    puntoValues.put("GOD_PAST",godS);
                    puntoValues.put("GOD_PRESENT",godP);
                    puntoValues.put("GOD_FUTURE",godF);
                    puntoValues.put("WORLD_PAST",worldS);
                    puntoValues.put("WORLD_PRESENT",worldP);
                    puntoValues.put("WORLD_FUTURE",worldF);
                    SQLiteDatabase db = helper.getWritableDatabase();
                    db.insert("PUNTOSTRADA", null, puntoValues);
                    db.close();
                  Toast.makeText(getActivity(),"RECORD AGGIUNTO",Toast.LENGTH_SHORT).show();
                }
            }
    );

    return view;
}

对不起,这是我的第一个真正有用的程序(我是意大利人,所以...)如果您只允许Toast通知进入,则eventHandler可以工作,所以我认为仅此代码会产生问题

这是应用程序崩溃时android studio给出的堆栈跟踪:

2019-12-26 21:50:41.838 24285-24285/com.penta.angri3app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.penta.angri3app, PID: 24285
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:352)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
    at com.penta.angri3app.PuntoFragment$1.onClick(PuntoFragment.java:82)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
android sqlite android-fragments
1个回答
0
投票

由于尝试获取可写数据库时会抛出空指针,因此问题很可能是在行SQLiteDatabase db = helper.getWritableDatabase();遇到空指针的地方。

最可能的原因是helper为null,因此尚未实例化。

如果要在行上添加一个断点然后以调试模式运行(即您应该做的事情),那么您很可能会在调试窗口中看到helper为空。

解决问题

没有足够的代码来建立确切的克服方法,因为未显示什么类helper打算作为其实例。

假设类是DatabaseHelper可能不存在,并且其签名仅需要参数1,即Context,然后使用:-

helper = new DatabaseHelper(v.getContext);
SQLiteDatabase db = helper.getWritableDatabase();
......

将解决该问题。

© www.soinside.com 2019 - 2024. All rights reserved.