“列表”是抽象的;无法实例化

时间:2019-05-15 11:03:30

标签: android

列表列表=新列表(id,名称,类型);给我一个错误,说“列表”是抽象的;无法实例化。不确定这实际上意味着什么,看着几个不同的回答问题,没有任何帮助解决此错误的方法。有关此令人沮丧的问题的任何建议或解决方案,请随时提供帮助。

package connect2you.com;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class WindowsPost extends AppCompatActivity {

    EditText editTextName;
    Button buttonAddList;
    Spinner spinnerGenres;

    DatabaseReference databaseLists;

    ListView listViewType;

    List<List> listValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_windows_post);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        databaseLists = FirebaseDatabase.getInstance().getReference("Windows Thread");

        editTextName = (EditText) findViewById(R.id.editTextName);
        buttonAddList = (Button) findViewById(R.id.buttonAddList);
        spinnerGenres = (Spinner) findViewById(R.id.spinnerGenres);

        listViewType = (ListView) findViewById(R.id.listViewType);

        listValues = new ArrayList<>();
        buttonAddList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addList();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        databaseLists.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                listValues.clear();

                for (DataSnapshot listSnapshot : dataSnapshot.getChildren()){
                    List list = listSnapshot.getValue(List.class);

                    listValues.add(list);
                }

                ListValues adapter = new ListValues(WindowsPost.this, listValues);
                listViewType.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu (Menu menu){

        getMenuInflater().inflate(R.menu.menu, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.menuLogOut){

            startActivity(new Intent(WindowsPost.this, MainActivity.class));
        }

        return super.onOptionsItemSelected(item);
    }



    private void addList(){
        String name = editTextName.getText().toString().trim();
        String genre = spinnerGenres.getSelectedItem().toString();

        if (!TextUtils.isEmpty(name)){

            String id = databaseLists.push().getKey();

            List list = new List (id, name, genre);

            databaseLists.child(id).setValue(list);

            editTextName.setText("");

            Toast.makeText(this, "Post added", Toast.LENGTH_LONG).show();

        }else{
            Toast.makeText(this,"You need to enter a title", Toast.LENGTH_LONG).show();
        }

    }
}

列表类别

package connect2you.com;

public class List {

   String listId;
   String listName;
   String listGenre;

    public List(){

    }

    public List(String listId, String listName, String listGenre) {
        this.listId = listId;
        this.listName = listName;
        this.listGenre = listGenre;
    }


    public String getListId() {
        return listId;
    }

    public String getListName() {
        return listName;
    }

    public String getListGenre() {
        return listGenre;
    }
}

4 个答案:

答案 0 :(得分:3)

您的名称空间混乱。在main方法中,您具有以下import语句;

import java.util.List;

这与您打算使用的List类冲突。删除导入,或将List类命名为其他名称。我推荐后者,因为List是Java,是一种广泛使用的接口。

答案 1 :(得分:2)

您已导入:

import java.util.List;

因此,当您尝试创建新的List实例时,Java认为您正在尝试执行new java.util.List()

您可以更改类的名称以避免冲突,或者创建明确定义类型的新实例:

connect2you.com.List list = new connect2you.com.List(id, name, genre);

为避免出现问题和丑陋的代码,我可能会将List重命名为其他名称。

答案 2 :(得分:0)

您的类列表与Java Package类相矛盾。

  1. 您可以将列表中的自定义类重构为其他名称,例如CustomList。
  2. 或者您可以在创建类的实例时指定包名称,例如

    <fieldset> <legend>France Women</legend> <span> Manager: <p id="franceMgr"></p> </span> <span> World Ranking: <p id="franceRanking"></p> </span> </fieldset>

答案 3 :(得分:0)

将您的List类重命名为Lists或您喜欢的任何名称

package connect2you.com;

public class Lists {

   String listId;
   String listName;
   String listGenre;

public Lists(){

}

public Lists(String listId, String listName, String listGenre) {
    this.listId = listId;
    this.listName = listName;
    this.listGenre = listGenre;
}


public String getListId() {
    return listId;
}

public String getListName() {
    return listName;
}

public String getListGenre() {
    return listGenre;
}
}

在活动中导入您的Lists

package connect2you.com;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import connect2you.com.Lists;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class WindowsPost extends AppCompatActivity {

EditText editTextName;
Button buttonAddList;
Spinner spinnerGenres;

DatabaseReference databaseLists;

ListView listViewType;

List<Lists> listValues;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_windows_post);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    databaseLists = FirebaseDatabase.getInstance().getReference("Windows Thread");

    editTextName = (EditText) findViewById(R.id.editTextName);
    buttonAddList = (Button) findViewById(R.id.buttonAddList);
    spinnerGenres = (Spinner) findViewById(R.id.spinnerGenres);

    listViewType = (ListView) findViewById(R.id.listViewType);

    listValues = new ArrayList<>();
    buttonAddList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addList();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();

    databaseLists.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            listValues.clear();

            for (DataSnapshot listSnapshot : dataSnapshot.getChildren()){
                Lists list = listSnapshot.getValue(Lists.class);

                listValues.add(list);
            }

            ListValues adapter = new ListValues(WindowsPost.this, listValues);
            listViewType.setAdapter(adapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

@Override
public boolean onCreateOptionsMenu (Menu menu){

    getMenuInflater().inflate(R.menu.menu, menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == R.id.menuLogOut){

        startActivity(new Intent(WindowsPost.this, MainActivity.class));
    }

    return super.onOptionsItemSelected(item);
}



private void addList(){
    String name = editTextName.getText().toString().trim();
    String genre = spinnerGenres.getSelectedItem().toString();

    if (!TextUtils.isEmpty(name)){

        String id = databaseLists.push().getKey();

        Lists lists = new Lists(id, name, genre);

        databaseLists.child(id).setValue(lists);

        editTextName.setText("");

        Toast.makeText(this, "Post added", Toast.LENGTH_LONG).show();

    }else{
        Toast.makeText(this,"You need to enter a title", Toast.LENGTH_LONG).show();
    }

}
}

我希望这对您有用。