我在Android Studio中有一个名为MainActivity.java的主java文件:
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);
}
}
}
另一个名为Task.java的类:
public class Task {
private String task;
public Task() {}
public Task(String task) {
this.task = task;
}
public String getTask() {
return task;
}
}
我收到这些错误:
错误:(53,35)错误:任务是抽象的;无法实例化
错误:(82,25)错误:任务是抽象的;无法实例化
错误:(91,34)错误:找不到符号方法getTask()
如果我正确使用类Task,为什么会这样?
我该怎么办?
答案 0 :(得分:1)
在MainActivity.java的顶部,您已获得import
语句,该语句导入的Task
课程不是您撰写的课程。您需要删除import
语句才能使用自己的类。如果你不这样做,那么当编译器在你的代码中看到Task
时,它假定你指的是你导入的那个,而不是与MainActivity.java在同一个包中的那个。