Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 碎片上的可点击按钮……”;没有零参数构造函数“;?我尝试了一切';我在stackoverflow上看过_Java_Android - Fatal编程技术网

Java 碎片上的可点击按钮……”;没有零参数构造函数“;?我尝试了一切';我在stackoverflow上看过

Java 碎片上的可点击按钮……”;没有零参数构造函数“;?我尝试了一切';我在stackoverflow上看过,java,android,Java,Android,似乎找不到任何方法来解决这个问题。它在android studio上,我试图在片段中添加一个可单击的按钮,每次单击按钮时它都会崩溃。非常感谢您的帮助 public class DashboardFragment extends Fragment { private DashboardViewModel dashboardViewModel; public View onCreateView(@NonNull LayoutInflater inflater,

似乎找不到任何方法来解决这个问题。它在android studio上,我试图在片段中添加一个可单击的按钮,每次单击按钮时它都会崩溃。非常感谢您的帮助

public class DashboardFragment extends Fragment {

private DashboardViewModel dashboardViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    dashboardViewModel =
            ViewModelProviders.of(this).get(DashboardViewModel.class);
    View v = inflater.inflate(R.layout.fragment_dashboard, container, false);

    Button button = (Button) v.findViewById(R.id.createLogBTN);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent intent = new Intent(getActivity(), divelogcreate.class);
           startActivity(intent);
        }
    });
    return v;

}

在未扩展活动的类上不能使用startActivity。如果要创建divelogcreate,只需使用默认构造函数,如:

package com.example.divelogapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class divelogcreate extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "divelogStartOver2.db";
    public static final String TABLE_NAME = "divelog_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "DIVE_NUM";
    public static final String COL_3 = "DATE";
    public static final String COL_4 = "LOCATION";
    public static final String COL_5 = "SITE";
    public static final String COL_6 = "TEMPERATURE";
    public static final String COL_7 = "VISIBILITY";
    public static final String COL_8 = "MAX_DEPTH";
    public static final String COL_9 = "BOTTOM_TIME";
    public static final String COL_10 = "SAFTEY_CONDUCT_TIME";
    public static final String COL_11= "TOTAL_TIME";
    public static final String COL_12= "WEIGHT";
    public static final String COL_13 = "SUIT_TYPE";
    public static final String COL_14 = "THICKNESS";
    public static final String COL_15 = "AIR_TYPE";
    public static final String COL_16 = "COMMENTS";
    public static final String COL_17 = "SIG_FIRSTNAME";
    public static final String COL_18 = "SIG_LASTNAME";
    public static final String COL_19 = "DATE_SIGNED";
    public static final String COL_20 = "SIGN_DIVE_NUM";
    public static final String COL_21 = "COMPANY";

    public divelogcreate(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    public divelogcreate(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "DIVE_NUM TEXT, DATE TEXT, LOCATION TEXT, SITE TEXT, TEMPERATURE INTEGER, VISIBILITY TEXT," +
                "MAX_DEPTH INTEGER, BOTTOM_TIME INTEGER, SAFTEY_CONDUCT_TIME INTEGER, TOTAL_TIME INTEGER," +
                "WEIGHT INTEGER, SUIT_TYPE TEXT, THICKNESS INTEGER, AIR_TYPE TEXT, COMMENTS TEXT," +
                "SIG_FIRSTNAME TEXT, SIG_LASTNAME TEXT, DATE_SIGNED TEXT, SIGN_DIVE_NUM TEXT, COMPANY TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String dive_num, String date, String location, String site, String temperature,
                              String visibility, String max_depth, String bottom_time, String saftey,
                              String total_time, String weight, String suit_type, String thickness,
                              String air_type, String comments, String sign_first, String sign_last,
                              String date_sign, String sign_dive_num, String company){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(COL_2,dive_num);
        contentValues.put(COL_3,date);
        contentValues.put(COL_4,location);
        contentValues.put(COL_5,site);
        contentValues.put(COL_6,temperature);
        contentValues.put(COL_7,visibility);
        contentValues.put(COL_8,max_depth);
        contentValues.put(COL_9,bottom_time);
        contentValues.put(COL_10,saftey);
        contentValues.put(COL_11,total_time);
        contentValues.put(COL_12,weight);
        contentValues.put(COL_13,suit_type);
        contentValues.put(COL_14,thickness);
        contentValues.put(COL_15,air_type);
        contentValues.put(COL_16,comments);
        contentValues.put(COL_17,sign_first);
        contentValues.put(COL_18,sign_last);
        contentValues.put(COL_19,date_sign);
        contentValues.put(COL_20,sign_dive_num);
        contentValues.put(COL_21,company);

        long results = db.insert(TABLE_NAME,null,contentValues);
        if (results == -1)
            return false;
        else
            return true;

    }

    public Cursor getAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
        return res;
    }

    public boolean updateData(String id, String dive_num, String date, String location, String site, String temperature,
                              String visibility, String max_depth, String bottom_time, String saftey,
                              String total_time, String weight, String suit_type, String thickness,
                              String air_type, String comments, String sign_first, String sign_last,
                              String date_sign, String sign_dive_num, String company){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(COL_1,id);
        contentValues.put(COL_2,dive_num);
        contentValues.put(COL_3,date);
        contentValues.put(COL_4,location);
        contentValues.put(COL_5,site);
        contentValues.put(COL_6,temperature);
        contentValues.put(COL_7,visibility);
        contentValues.put(COL_8,max_depth);
        contentValues.put(COL_9,bottom_time);
        contentValues.put(COL_10,saftey);
        contentValues.put(COL_11,total_time);
        contentValues.put(COL_12,weight);
        contentValues.put(COL_13,suit_type);
        contentValues.put(COL_14,thickness);
        contentValues.put(COL_15,air_type);
        contentValues.put(COL_16,comments);
        contentValues.put(COL_17,sign_first);
        contentValues.put(COL_18,sign_last);
        contentValues.put(COL_19,date_sign);
        contentValues.put(COL_20,sign_dive_num);
        contentValues.put(COL_21,company);

        db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id});

        return true;
    }

    public Integer deleteData (String id){
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME, "ID = ?", new String[] {id});
    }
}
您需要学习正确的Java命名。使用CamelCase命名您的类(例如):


我建议您学习Android体系结构组件中的“房间数据库”

如果您想连接sqllitedb,最好的解决方案是使您的数据库实例在整个应用程序生命周期中成为一个单实例:

public class DiveLogCreateDBHelper extends SQLiteOpenHelper
每当您想要将上下文传递给db并使用singleton方法时

private static divelogcreate instance;

public static synchronized divelogcreate getInstance(Context context) {

if (instance == null) {
  instance = new divelogcreate(context.getApplicationContext());
}
return instance;
}

public divelogcreate(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

什么是“divelogcreate”?请sharedivelogcreate是项目中的一个类activity中缺少什么元素会导致崩溃?这就是您要求共享activity的原因
public class DiveLogCreateDBHelper extends SQLiteOpenHelper
private static divelogcreate instance;

public static synchronized divelogcreate getInstance(Context context) {

if (instance == null) {
  instance = new divelogcreate(context.getApplicationContext());
}
return instance;
}

public divelogcreate(Context context) {
    super(context, DATABASE_NAME, null, 1);
}
public void onClick(View v) {
     divelogcreate dbHelper = divelogcreate.getInstance(getActivity());
     dbHelper.deleteData("your_id")
}