Reputation:
I have this main java file called MainActivity.java in Android Studio:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter recyclerViewAdapter;
private EditText addTaskBox;
private DatabaseReference databaseReference;
private List<Task> allTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
allTask = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference();
addTaskBox = (EditText)findViewById(R.id.add_task_box);
recyclerView = (RecyclerView)findViewById(R.id.task_list);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
Button addTaskButton = (Button)findViewById(R.id.add_task_button);
assert addTaskButton != null;
addTaskButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String enteredTask = addTaskBox.getText().toString();
if(TextUtils.isEmpty(enteredTask)){
Toast.makeText(MainActivity.this, "You must enter a task first", Toast.LENGTH_LONG).show();
return;
}
if(enteredTask.length() < 6){
Toast.makeText(MainActivity.this, "Task count must be more than 6", Toast.LENGTH_LONG).show();
return;
}else{
Task taskObject = new Task(enteredTask);
databaseReference.push().setValue(taskObject);
addTaskBox.setText("");
}
}
});
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
getAllTask(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
getAllTask(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
taskDeletion(dataSnapshot);
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void getAllTask(DataSnapshot dataSnapshot){
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
String taskTitle = singleSnapshot.getValue(String.class);
allTask.add(new Task(taskTitle));
recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, allTask);
recyclerView.setAdapter(recyclerViewAdapter);
}
}
private void taskDeletion(DataSnapshot dataSnapshot){
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
String taskTitle = singleSnapshot.getValue(String.class);
for(int i = 0; i < allTask.size(); i++){
if(allTask.get(i).getTask().equals(taskTitle)){
allTask.remove(i);
}
}
Log.d(TAG, "Task tile " + taskTitle);
recyclerViewAdapter.notifyDataSetChanged();
recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, allTask);
recyclerView.setAdapter(recyclerViewAdapter);
}
}
}
And another class called Task.java:
public class Task {
private String task;
public Task() {}
public Task(String task) {
this.task = task;
}
public String getTask() {
return task;
}
}
And I receive these errors:
Error:(53, 35) error: Task is abstract; cannot be instantiated
Error:(82, 25) error: Task is abstract; cannot be instantiated
Error:(91, 34) error: cannot find symbol method getTask()
Why does this happen if I use the class Task correctly?
What should I do?
Upvotes: 0
Views: 68
Reputation: 79875
At the top of MainActivity.java, you've got an import
statement that imports a Task
class that's not the one you've written. You need to remove that import
statement to be able to use your own class. If you don't do that, then when the compiler sees Task
in your code, it assumes you mean the one you imported, not the one in the same package as MainActivity.java.
Upvotes: 1