作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Java 类“MainFragment”
package com.example.licenta23;
import android.Manifest;
import android.app.AppComponentFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
import java.util.List;
public class MainFragment extends Fragment {
EditText actv1, edtModel, actv2, actv3, actv4, edtFabricatie, edtPret;
Button btnChoose, btnAdd, btnList;
ImageView imageView3;
final int REQUEST_CODE_GALLERY = 999;
public static SQLiteHelper sqLiteHelper;
private static final String[] CAROSERIE = new String[] {
"Cabrio", "Berlina", "Coupe", "Pick-up", "Hatchback", "Break", "Off-road", "Minibus", "Monovolum", "SUV"
};
private static final String[] COMBUSTIBIL = new String[] {
"Benzina", "Diesel", "GPL", "Electric", "Hibrid"
};
private static final String[] CULOARE = new String[] {
"Alb", "Negru", "Gri", "Argintiu", "Albastru", "Rosu", "Verde", "Auriu", "Galben", "Portocaliu", "Maro", "Bej", "Alta culoare"
};
private List<MarcaItem> marcaList;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
fillMarcaList();
AutoCompleteTextView editText = view.findViewById(R.id.actv1);
AutoCompleteMarcaAdapter adapter = new AutoCompleteMarcaAdapter(getActivity(), marcaList);
editText.setAdapter(adapter);
AutoCompleteTextView editText2 = view.findViewById(R.id.actv2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, CAROSERIE);
editText2.setAdapter(adapter2);
AutoCompleteTextView editText3 = view.findViewById(R.id.actv3);
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, COMBUSTIBIL);
editText3.setAdapter(adapter3);
AutoCompleteTextView editText4 = view.findViewById(R.id.actv4);
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, CULOARE);
editText4.setAdapter(adapter4);
return view;
}
private void fillMarcaList() {
marcaList = new ArrayList<>();
marcaList.add(new MarcaItem("Alfa Romeo", R.drawable.alfa_romeo));
marcaList.add(new MarcaItem("Audi", R.drawable.audi));
marcaList.add(new MarcaItem("Bmw", R.drawable.bmw));
marcaList.add(new MarcaItem("Chevrolet", R.drawable.chevrole));
marcaList.add(new MarcaItem("Citroen", R.drawable.citroen));
marcaList.add(new MarcaItem("Dacia", R.drawable.dacia));
marcaList.add(new MarcaItem("Dodge", R.drawable.dodge));
marcaList.add(new MarcaItem("Fiat", R.drawable.fiat));
marcaList.add(new MarcaItem("Ford", R.drawable.ford));
marcaList.add(new MarcaItem("Honda", R.drawable.honda));
marcaList.add(new MarcaItem("Hyundai", R.drawable.hyundai));
marcaList.add(new MarcaItem("Jaguar", R.drawable.jaguar));
marcaList.add(new MarcaItem("Jeep", R.drawable.jeep));
marcaList.add(new MarcaItem("Kia", R.drawable.kia));
marcaList.add(new MarcaItem("Land Rover", R.drawable.landrover));
marcaList.add(new MarcaItem("Lexus", R.drawable.lexus));
marcaList.add(new MarcaItem("Mazda", R.drawable.mazda));
marcaList.add(new MarcaItem("Mercedes-Benz", R.drawable.mercedes));
marcaList.add(new MarcaItem("Mini Cooper", R.drawable.minicooper));
marcaList.add(new MarcaItem("Mitsubishi", R.drawable.mitsubishi));
marcaList.add(new MarcaItem("Nissan", R.drawable.nissan));
marcaList.add(new MarcaItem("Opel", R.drawable.opel));
marcaList.add(new MarcaItem("Peugeot", R.drawable.peugeot));
marcaList.add(new MarcaItem("Pontiac", R.drawable.pontiac));
marcaList.add(new MarcaItem("Porche", R.drawable.porche));
marcaList.add(new MarcaItem("Renault", R.drawable.renault));
marcaList.add(new MarcaItem("Rover", R.drawable.rover));
marcaList.add(new MarcaItem("Seat", R.drawable.seat));
marcaList.add(new MarcaItem("Skoda", R.drawable.skoda));
marcaList.add(new MarcaItem("Smart", R.drawable.smart));
marcaList.add(new MarcaItem("Subaru", R.drawable.subaru));
marcaList.add(new MarcaItem("Suzuki", R.drawable.suzuki));
marcaList.add(new MarcaItem("Toyota", R.drawable.toyota));
marcaList.add(new MarcaItem("Voltwagen", R.drawable.voltwagen));
marcaList.add(new MarcaItem("Volvo", R.drawable.volvo));
init();
sqLiteHelper = new SQLiteHelper(this, "CarDB.sqlite", null, 1);
sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS CAR (Id INTEGER PRIMARY KEY AUTOINCREMENT,
alegemarca VARCHAR, model VARCHAR, caroserie VARCHAR, combustibil VARCHAR, culoare VARCHAR,
fabricatie VARCHAR, pret VARCHAR image BLOG)");
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(
MainFragment.this,
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_GALLERY
);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
private void init(){
actv1 = (EditText) getView().findViewById(R.id.actv1);
edtModel = (EditText) getView().findViewById(R.id.edtModel);
actv2 = (EditText) getView().findViewById(R.id.actv2);
actv3 = (EditText) getView().findViewById(R.id.actv3);
actv4 = (EditText) getView().findViewById(R.id.actv4);
edtFabricatie = (EditText) getView().findViewById(R.id.edtFabricatie);
edtPret = (EditText) getView().findViewById(R.id.edtPret);
btnChoose = (Button) getView().findViewById(R.id.btnChoose);
btnAdd = (Button) getView().findViewById(R.id.btnAdd);
btnList = (Button) getView().findViewById(R.id.btnList);
imageView3 = (ImageView) getView().findViewById(R.id.imageView3);
}
}
SQL 数据库“SQLiteHelper.java”
package com.example.licenta23;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import androidx.annotation.Nullable;
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(@Nullable MainFragment context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public void queryData(String sql){
SQLiteDatabase database = getWritableDatabase();
database.execSQL(sql);
}
public void insertData(String alegemarca, String model, String caroserie, String combustibil, String culoare, String fabricatie, String pret, byte[] image) {
SQLiteDatabase database = getWritableDatabase();
String sql = "INSERT INTO CAR VALUES (NULL, ?, ?, ?)";
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(1, alegemarca);
statement.bindString(2, model);
statement.bindString(3, caroserie);
statement.bindString(4, combustibil);
statement.bindString(5, culoare);
statement.bindString(6, fabricatie);
statement.bindString(7, pret);
statement.bindBlob(8, image);
statement.executeInsert();
}
public Cursor getData(String sql){
SQLiteDatabase database = getReadableDatabase();
return database.rawQuery(sql, null);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
而且,我的错误是:
C:\Users\tibim\AndroidStudioProjects\licenta23\app\src\main\java\com\example\licenta23\MainFragment.java:130: error: incompatible types: MainFragment cannot be converted to Activity
MainFragment.this,
最佳答案
首先像这样更改构造函数助手(删除 MainFragment 并设置 Context):
public SQLiteHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
接下来在 fragment 中初始化 Activity ,如下所示:
private AppCompatActivity activity;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof AppCompatActivity) {
activity = (AppCompatActivity) context;
}
}
接下来更改您的帮助程序和点击监听器以获得权限:
//...
sqLiteHelper = new SQLiteHelper(activity, "CarDB.sqlite", null, 1);
//...
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(
//this change
activity,
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_GALLERY
);
}
});
关于java - 错误: incompatible types: MainFragment cannot be converted to Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61029763/
最近在谷歌分析中我发现,我的 Android 应用程序因 NullPointerException 而对某些用户崩溃。也许有人可以帮助我确定是什么导致应用程序崩溃...这是崩溃: NullPointe
我正在开发 Android Facebook 登录,但遇到了问题。这是我的 MainFragment 类: public class MainFragment extends Frag
Java 类“MainFragment” package com.example.licenta23; import android.Manifest; import android.app.AppC
您好,我正在开发一个将成为 Android 启动器的应用程序,该启动器中有三个不同的屏幕,第一个屏幕是一个书架,应该导入并显示设备上 SD 卡位置中的所有书籍第二个是计算器(工作得很好!)第三个是家庭
我是一名优秀的程序员,十分优秀!