我得到了这两节课。下面的一个应该得到一个带电影的ArrayList ......
import java.util.ArrayList ;
import android.app.Activity ;
import android.os.Bundle ;
public class UIDActivity extends Activity {
private static ArrayList<Movie> movieList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public static void main(String String, int
int args[]
) {
movieList = new ArrayList<Movie>();
movie1 = new Movie("Fight Club", "David Fincher", "1999", "3");
movieList.add(movie1);
}
public String getTitle() {
return title;
}
public String getDirector() {
return director;
}
public int getYear() {
return year;
}
public int getRating() {
return rating;
}
}
这是Movie类,我在其中告诉Movie的属性是什么
import java.io.Serializable ;
@SuppressWarnings("serial")
public class Movie implements Serializable {
public String title;
public String director;
public int year;
public int rating;
public Movie(String deTitle, String deDirector, int hetYear, int deRating) {
title = deTitle;
director = deDirector;
year = hetYear;
rating = deRating;
}
public void setTitle(String title) {
this.title = title;
}
public void setDirector(String director) {
this.director = director;
}
public void setYear(int year) {
this.year = year;
}
public void setRating(int rating) {
this.rating = rating;
}
}
出于某种原因,我无法弄清楚我一直忘记的简单事情?!?! Eclipse希望将我的公共电影改为四次字符串...没有错误,但其余的都不会有效。
答案 0 :(得分:2)
您的Movie构造函数需要两个String对象和两个int -
public Movie(String deTitle, String deDirector, int hetYear, int deRating)
但是你传递了四个String对象 -
movie1 = new Movie("Fight Club", "David Fincher", "1999", "3");
为什么要尝试将四个String值传递给Movie构造函数而不是构造函数所期望的两个String和两个int?顺便提一句,你应该创建一个movie对象 -
movie1 = new Movie("Fight Club", "David Fincher", 1999, 3);
答案 1 :(得分:1)
你只需要在你的Movie类中拥有setter / getters,另外,确保类变量被声明为private并且只能通过适当的getter访问。
public class Movie实现Serializable {
private String title;
private String director;
private int year;
private int rating;
public Movie(String deTitle, String deDirector, int hetYear, int deRating) {
title = deTitle;
director = deDirector;
year = hetYear;
rating = deRating;
}
public void setDirector(String director) {
this.director = director;
}
public String getDirector() {
return director;
}
...
}
另外,我将使用List接口定义您的列表,并将实例化定义为ArrayList。
答案 2 :(得分:0)
据我所知,如果你创建一个静态变量,你必须在那时初始化它。
试试这个:
private static ArrayList<Movie> movieList = new ArrayList<Movie>();
顺便说一句,难道UIDActivity的getter不应该被移动到Movie类或者包含你想要哪部电影的索引吗?
答案 3 :(得分:0)
问题在于您尝试使用的构造函数需要两个Strings
和两个ints
,而是传递构造函数4个字符串。
电影类中构造函数的方法签名是
public Movie(String deTitle, String deDirector, int hetYear, int deRating)
您尝试调用构造函数的方式不正确。
此:
movie1 = new Movie("Fight Club", "David Fincher", "1999", "3");
应该是:
movie1 = new Movie("Fight Club", "David Fincher", 1999, 3);