Please consider I am new to developing apps in Android Studio.
I tried to add a fragment to an existing application responsible for creating, updating, and deleting tasks. After making a few changes, I wanted to try to test the code, but I could not open the fragment anymore in the emulator. I can still open the other fragments while running the application.
I tried to fix the problem myself, but I do not know where the issue is.
package com.example.connectedfamily.ui.taskManagement;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.connectedfamily.R;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.text.DateFormat;
import java.util.Date;
public class TaskManagementFragment extends Fragment {
TaskManagementViewModel mViewModel;
TaskManager taskManager;
EditText newTaskEditText;
ImageView sendButton;
FirebaseDatabase firebaseDatabase;
DatabaseReference reference;
public static TaskManagementFragment newInstance() { return new TaskManagementFragment(); }
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_task_management, container, false);
newTaskEditText = newTaskEditText.findViewById(R.id.enterTask);
sendButton = sendButton.findViewById(R.id.sendButton);
initClickListeners();
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = new ViewModelProvider(this).get(TaskManagementViewModel.class);
}
/**
* Initializes the click listeners
*/
protected void initClickListeners()
{
sendButton.setOnClickListener(view -> onSendButtonClick());
}
/**
* defines behaviour after clicking on the send button
*/
protected void onSendButtonClick()
{
firebaseDatabase = FirebaseDatabase.getInstance();
String newTask = newTaskEditText.getText().toString();
reference = FirebaseDatabase.getInstance().getReference().child("tasks");
//check if user entered empty string
if (newTask != null && !newTask.isEmpty())
{
mViewModel.setName(newTask);
mViewModel.setId(reference.push().getKey());
mViewModel.setDate(DateFormat.getDateInstance().format(new Date()));
taskManager.addTask(mViewModel);
newTaskEditText.getText().clear();
reference.child(mViewModel.getId()).setValue(mViewModel).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
{
Toast.makeText(getContext().getApplicationContext(), "Task was processed successfully", Toast.LENGTH_SHORT).show();
}
else
{
String error = task.getException().toString();
Toast.makeText(getContext().getApplicationContext(), "Update failed "+error, Toast.LENGTH_SHORT).show();
}
}
});
}
else
{
Toast.makeText(getContext().getApplicationContext(), "Input is invalid", Toast.LENGTH_SHORT).show();
}
}
}
package com.example.connectedfamily.ui.taskManagement;
import androidx.lifecycle.ViewModel;
public class TaskManagementViewModel extends ViewModel {
String name;
String date;
String id;
/**
* gets the name of the task
* @return the name
*/
public String getName()
{
return name;
}
/**
* sets the name
* @param argName the name to be set
*/
public void setName(String argName)
{
this.name = argName;
}
/**
* get the date of the task
* @return the date
*/
public String getDate()
{
return date;
}
/**
* set the date of the task
* @param argDate the argDate
*/
public void setDate(String argDate)
{
this.date = argDate;
}
/**
* gets the id of the task
* @return the id
*/
public String getId()
{
return id;
}
/**
* sets the id of the new task
* @param argId the id
*/
public void setId(String argId)
{
this.id = argId;
}
}
package com.example.connectedfamily.ui.taskManagement;
import java.util.ArrayList;
public class TaskManager {
ArrayList<TaskManagementViewModel> tasks;
/**
* adds a new task to the list
* @param task to add
*/
public void addTask(TaskManagementViewModel task)
{
tasks.add(task);
}
/**
* deletes task from the current list
* @param task to delete
*/
public void deleteTask(TaskManagementViewModel task)
{
tasks.remove(task);
}
/**
* returns the list with all available tasks
* @return the tasks
*/
public ArrayList<TaskManagementViewModel> getListOfTasks()
{
return tasks;
}
/**
* returns the number of available tasks
* @return integer number of tasks
*/
public int getAmountOfTasks()
{
return tasks.size();
}
}
Here is a part of the log:
java.io.IOException: Error code 5: Access denied at com.android.tools.idea.diagnostics.windows.WindowsDefenderRegistryStatusProvider.readExcludedPaths(WindowsDefenderRegistryStatusProvider.java:62) at com.android.tools.idea.diagnostics.windows.WindowsDefenderRegistryStatusProvider.getExcludedPaths(WindowsDefenderRegistryStatusProvider.java:49) at com.android.tools.idea.diagnostics.WindowsPerformanceHintsChecker.getExcludedPatterns(WindowsPerformanceHintsChecker.java:242) at com.android.tools.idea.diagnostics.WindowsPerformanceHintsChecker.checkWindowsDefender(WindowsPerformanceHintsChecker.java:128) at at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: com.sun.jna.platform.win32.Win32Exception: Access denied at com.sun.jna.platform.win32.Advapi32Util.registryGetValues(Advapi32Util.java:2236) at com.sun.jna.platform.win32.Advapi32Util.registryGetValues(Advapi32Util.java:2215) at com.android.tools.idea.diagnostics.windows.WindowsDefenderRegistryStatusProvider.readExcludedPaths(WindowsDefenderRegistryStatusProvider.java:57) ... 15 more
If you need other code to understand where the issue is, please don't hesitate and let me know.
This message also appears in the log after opening the fragment:
INFO - run.AndroidLogcatOutputCapture - startCapture("Pixel_5_API_30 [emulator-5222]")
INFO - run.AndroidLogcatOutputCapture - stopCapture("Pixel_5_API_30 [emulator-5222]")
INFO - run.AndroidLogcatOutputCapture - stopAll()