![](/img/trans.png)
[英]Java Spring Boot FTPClient connection method from another class doesn´t work
[英]Android - Using method from another class doesn't work
在下面的代码中(为了简化起见,我对其进行了很多裁剪),我有一个Activity,它的onCreate()调用方法getClothes(),该方法获取衣服列表。 然后,第一批衣服装满毕加索。
public class Act_ChooseClothes extends AppCompatActivity {
Clothes myClothes;//Second class
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_clothes_start);
img_clothes_0 = (ImageView) findViewById(R.id.img_clothes);
getClothes();//Calls method which gets List of lists of clothes
Picasso.with(this).load(clothes_all_types.get(0).get(0)).into(clothes_0);//Picasso loads image from list of lists into ImageView
}
private void getClothes() {
clothes_upper = new ArrayList<>();
clothes_all_types = new ArrayList<List<File>>();//List of lists
clothes_upper = myClothes.getList("Upper");//Gets list using second class's method
clothes_all_types.add(clothes_upper);
}
}
我有第二类的方法来获取衣服列表(文件数据类型列表)。
//Second class which has "getList" method
public class Clothes{
File dir = new File(Environment.getExternalStorageDirectory().toString(), "/Clothes/" ); //Direction to clothes
//This method gets paths of clothes and puts them into list (and then returns that list)
public List<File> getList(String type){
List<File> clothes = new ArrayList<>();
File[] files=dir.listFiles();
for (int i=0; i<files.length; i++)
{
File file = files[i];
String filepath = file.getPath();
if(filepath.contains(type)){
clothes.add(file);
}
}
return clothes;
}}
此版本的代码不起作用-应用程序启动失败。
但是,如果我将第二类的方法放到第一类中(当然要删除Clothes myClothes对象),那么它就可以工作!
为什么此版本不起作用?
您没有实例化myClothes
对象。 在调用其中的方法之前实例化它,它应该可以工作。
例如,在您的活动中,使用Clothes myClothes = new Clothes();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.