我是一名新手程序员,我正在尝试使用 Eclipse 学习 Android 编码。 这是我第一次使用 StackOverflow。
仅出于教程目的,我想制作一个简单的动物百科全书。
因此,在我的 Home 类中,有一些按钮:“Dog”、“Cat”、“Bird”等。当我单击按钮时,它会将我带到相同的布局,但是当然内容不同。
因此我创建了一个名为 AnimalData 的类,其中包含
ArrayList<Integer>
存储R.drawable.xxx
和ArrayList<String>
存储我将放在图片下方的文本(例如“Bulldog” 或“哈士奇”)然后我创建了一个名为 ChangeContent 的类来设置所有这些可绘制对象 和文本到 XML
但是每当我单击该按钮时,都会导致意外停止错误
下面是缩短的 Home 类,“崩溃制造者”不在这里。我已经使用 Thread.sleep(2000)
检查了每行的整个代码行,因此如果我的应用程序在 2 秒之前崩溃,则错误发生在 sleep() 代码之前,反之亦然。
public class Home extends Activity implements OnClickListener{
Button dog, cat, bird;
AnimalData ad;
ChangeContent cc;
private ArrayList<Integer> drawable;
private ArrayList<String> title;
public Home(){
ad = new AnimalData();
cc = new ChangeContent(ad);
drawable = new ArrayList<Integer>();
title = new ArrayList<String>()
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
//set the findViewById for all the buttons
//set onClickListener() to all the buttons
}
public void onClick(View v) {
switch (v.getId()){
case R.id.bDog:
drawable.add(R.drawable.xxx);
drawable.add(R.drawable.yyy);
title.add("Bulldog");
title.add("Husky");
break;
case R.id.Cat:
//same
break;
case R.id.bBird:
//same
break;
}
ad.setDrawable(drawable);
ad.setTitle(title);
Intent i = new Intent("animal.ChangeContent"); //from Manifest
startActivity(i);
}
}
AnimalData 只是一个典型的 getter setter,所以我将跳过代码
该错误发生在 ChangeContent 启动后,因为即使我将 sleep() 放在构造函数的第一行,它也没有任何效果。
public class ChangeContent extends Activity {
TextView title1, title2;
ImageView pic1, pic2;
private ArrayList<Integer> drawable;
private ArrayList<String> title;
public ChangeContent(AnimalData data){
drawable = new ArrayList<Integer>();
title = new ArrayList<String>();
drawable = data.getDrawable();
title = data.getTitle();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animal_info);
//findViewById for the TextView and ImageView
//setText() for TextView and setImageResource() for ImageView
}
}
抱歉问了这么长的问题,我试着让它尽可能短 你们能帮我找出错误吗? 先谢谢了
最佳答案
您正在尝试从 Intent 获取 arraylist,但没有放置 putStringArrayList 和 putIntegerArrayList 方法。
putIntegerArrayListExtra(String name, ArrayList<Integer> value)
putStringArrayListExtra(String name, ArrayList<String> value)
因此将调用 Activity 更改为以下内容:
Intent i = new Intent(Home.this, ChangeContent.class); //from Manifest
i.putIntegerArrayListExtra("drawables", drawable);
i.putStringArrayListExtra("titles", titles);
startActivity(i);
在onCreate方法中通过以下方法从intent中获取数据: getIntegerArrayListExtra 和 getStringArrayListExtra
您还可以通过将 contentChanged 方法设置为静态来执行以下操作,这样您就不需要在应用程序代码中进行太多更改,只需执行以下操作:
public class ChangeContent extends Activity {
TextView title1, title2;
ImageView pic1, pic2;
private static ArrayList<Integer> drawable;
private static ArrayList<String> title;
public static ChangeContent(AnimalData data){
drawable = new ArrayList<Integer>();
title = new ArrayList<String>();
drawable = data.getDrawable();
title = data.getTitle();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animal_info);
//findViewById for the TextView and ImageView
//setText() for TextView and setImageResource() for ImageView
}
}
关于java - Android ArrayList,意外停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10052214/