我想构建todolist应用程序。我想使用RoomDatabse存储信息。我用房间建立数据库并获取信息,而不是保存到数据库中。但当我点击添加按钮,然后我得到例外。我已经在网上搜索了一个合适的解决方案,但没有找到任何有用的东西。请帮帮我.
Caused by: java.lang.ClassNotFoundException: Didn't find class
"androidx.core.app.ActivityManagerCompat" while store data using Room.
compileSdkVersion 27
buildToolsVersion '28.0.3'
minSdkVersion 15
targetSdkVersion 27
@Database(entities = {TaskEntry.class},version = 1,exportSchema = false)
private static final String LOG_TAG=AppDatabase.class.getSimpleName();
private static final Object LOCK=new Object();
private static final String DATABASE_NAME="todolist";
private static AppDatabase mInstance;
public static AppDatabase getInstance(Context context){
if(mInstance==null){
synchronized (LOCK){
Log.d(LOG_TAG,"Creating new database instance");
mInstance= Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class,AppDatabase.DATABASE_NAME)
.allowMainThreadQueries()
.build();
}
}
Log.d(LOG_TAG,"getting the database instance");
return mInstance;
}
public abstract TaskDao taskDao();
}
public class AddTaskActivity extends AppCompatActivity {
// Extra for the task ID to be received in the intent
public static final String EXTRA_TASK_ID = "extraTaskId";
// Extra for the task ID to be received after rotation
public static final String INSTANCE_TASK_ID = "instanceTaskId";
// Constants for priority
public static final int PRIORITY_HIGH = 1;
public static final int PRIORITY_MEDIUM = 2;
public static final int PRIORITY_LOW = 3;
// Constant for default task id to be used when not in update mode
private static final int DEFAULT_TASK_ID = -1;
// Constant for logging
private static final String TAG = AddTaskActivity.class.getSimpleName();
// Fields for views
EditText mEditText;
RadioGroup mRadioGroup;
Button mButton;
private int mTaskId = DEFAULT_TASK_ID;
private AppDatabase mDb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_task);
mDb=AppDatabase.getInstance(getApplicationContext());
initViews();
if (savedInstanceState != null && savedInstanceState.containsKey(INSTANCE_TASK_ID)) {
mTaskId = savedInstanceState.getInt(INSTANCE_TASK_ID, DEFAULT_TASK_ID);
}
Intent intent = getIntent();
if (intent != null && intent.hasExtra(EXTRA_TASK_ID)) {
mButton.setText(R.string.update_button);
if (mTaskId == DEFAULT_TASK_ID) {
// populate the UI
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(INSTANCE_TASK_ID, mTaskId);
super.onSaveInstanceState(outState);
}
/**
* initViews is called from onCreate to init the member variable views
*/
private void initViews() {
mEditText = findViewById(R.id.editTextTaskDescription);
mRadioGroup = findViewById(R.id.radioGroup);
mButton = findViewById(R.id.saveButton);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onSaveButtonClicked();
}
});
}
/**
* populateUI would be called to populate the UI when in update mode
*
* @param task the taskEntry to populate the UI
*/
private void populateUI(TaskEntry task) {
}
/**
* onSaveButtonClicked is called when the "save" button is clicked.
* It retrieves user input and inserts that new task data into the underlying database.
*/
public void onSaveButtonClicked() {
// Not yet implemented
String description=mEditText.getText().toString();
int priority=getPriorityFromViews();
Date date=new Date();
TaskEntry taskEntry=new TaskEntry(description,priority,date);
mDb.taskDao().insertTask(taskEntry);
finish();
}
/**
* getPriority is called whenever the selected priority needs to be retrieved
*/
public int getPriorityFromViews() {
int priority = 1;
int checkedId = ((RadioGroup) findViewById(R.id.radioGroup)).getCheckedRadioButtonId();
switch (checkedId) {
case R.id.radButton1:
priority = PRIORITY_HIGH;
break;
case R.id.radButton2:
priority = PRIORITY_MEDIUM;
break;
case R.id.radButton3:
priority = PRIORITY_LOW;
}
return priority;
}
/**
* setPriority is called when we receive a task from MainActivity
*
* @param priority the priority value
*/
public void setPriorityInViews(int priority) {
switch (priority) {
case PRIORITY_HIGH:
((RadioGroup) findViewById(R.id.radioGroup)).check(R.id.radButton1);
break;
case PRIORITY_MEDIUM:
((RadioGroup) findViewById(R.id.radioGroup)).check(R.id.radButton2);
break;
case PRIORITY_LOW:
((RadioGroup) findViewById(R.id.radioGroup)).check(R.id.radButton3);
}
}
}
在你的梯度依赖中,你使用这个吗?
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
如果是,请将其改为:
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
所以我强迫Android Studio在停止Gradle构建的过程中停下来(我知道,我有多白痴),因为它花了很长时间,而且一直没有响应,当我打开它备份时,它还可以,我可以创建一个新的布局文件,但是当我创建一个新的java类时,它决定不喜欢在我的任何java类中导入android.support.v7.app.AppCompat活动(但是如果我不打开它们来查看它们,就没有红色弯弯曲曲的下划线),它说
我试着用android studio安装一个react-native应用程序,安装apk后应用程序就会停止。 以下是带有错误的logcat: 我尝试了很多事情:清洁,重建,...库的版本似乎在任何需要的地方都是一样的,multidex似乎是正确设置的。 你能帮帮我吗? 谢谢!
新安装的Android studio 3.1.3在制作新项目和第一次编译时出现了奇怪的依赖错误。 一个无助于解决问题的类似问题。 这个问题在我身上发生过几次,强制https或http也不能解决它
我正在尝试在我的应用程序中使用Firebase。 向 Gradle 添加三行后,我收到此错误: 错误:(27,13)无法解析:com.google.firebase:firebase-core:9.8.0 以下是我的代码的一些图片: 为什么会出现此错误?
尝试添加时遇到此错误 为我的移动应用程序建立.分级。下面是整个build.gradle(移动)应用插件:“com.android.application” 这是我的第一个android应用程序,所以,我可能刚刚犯了一个初学者的错误。下面是我所采取的解决问题的步骤: styles.xml styles.xml(v21)