无法实例化该类型

时间:2014-08-04 04:40:08

标签: java android android-layout android-activity parse-platform

我试图解决这个错误,即使有研究,我的尝试也没有成功。特别是,我收到以下错误: 无法实例化类型List

以下是代码:

public class MatchingActivity extends Activity {


    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;   
    protected List<ParseUser> mUsers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.matching);
        // Show the Up button in the action bar.
         //create list variable
         mUsers = new List<ParseUser>(); 



    }
    @Override
    protected void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();


        setProgressBarIndeterminateVisibility(true);

        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.findInBackground(new FindCallback<ParseUser>() {
            public void done(List<ParseUser> users, ParseException e) {
                if (e == null) {

                    //add all the users to your list variable 
                    mUsers.addAll(users); 

                } else {
                    // Something went wrong.
                }
            }
        });

        //check the size of your list to see how big it is before accessing it
        final int size = mUsers.size(); 

       //or use a loop to loop through each one
        for(ParseUser mParseUser : mUsers)
        {
              //skip over the current user
           if(mParseUser == ParseUser.getCurrentUser())
               continue; 

           mParseUser.getString("name");
           mParseUser.getNumber("age"); 
           mParseUser.getString("headline");
        }

    }
    }   

非常感谢任何帮助。 提前致谢

更新

public class MatchingActivity extends Activity {


    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;   
    protected List<ParseUser> mUsers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.matching);
        // Show the Up button in the action bar.
         //create list variable
        mUsers = (List<ParseUser>) findViewById(R.id.listView1);



    }
    @Override
    protected void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();


        setProgressBarIndeterminateVisibility(true);

        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.findInBackground(new FindCallback<ParseUser>() {
            public void done(List<ParseUser> users, ParseException e) {
                if (e == null) {



                  //add all the users to your list variable 
                    mUsers.addAll(users); 



                } else {
                    // Something went wrong.
                }
            }
        });

        //check the size of your list to see how big it is before accessing it
        final int size = mUsers.size(); 

       //or use a loop to loop through each one
        for(ParseUser mParseUser : mUsers)
        {
              //skip over the current user
           if(mParseUser == ParseUser.getCurrentUser())
               continue; 

           mParseUser.getString("name");
           mParseUser.getNumber("age"); 
           mParseUser.getString("headline");

           ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                   this, 
                   android.R.layout.simple_list_item_1, Unsure what to input here, 
                   as I want to return all three items (name, age, headline) from parse into the list);
           mUsers.setAdapter(arrayAdapter);
        }

    }
    }   

提示错误 方法setAdapter(ArrayAdapter)未定义类型List

感谢您的支持

2 个答案:

答案 0 :(得分:3)

您无法使用Interface List

实例化new List()

关键字new用于创建(实例化)对象。在这种情况下,您可以使用Interface List

的任何类来实例化implements List
mUsers = new ArrayList<ParseUser>(); //example with ArrayList

请参阅Java api hereList所有已知实施类

答案 1 :(得分:2)

列表是一个界面。接口无法实例化。

所以试试这个:

mUsers = new ArrayList<ParseUser>();

而不是

mUsers = new List<ParseUser>(); 

参考:Cannot instantiate the type List<Product>