Java 无法在Android应用程序中实例化类
我正在开发一个Android应用程序。在我的应用程序中,我对另一个类有兴趣。一旦发生这种情况,应用程序就会崩溃。它说它不能实例化我的类。我的代码如下:Java 无法在Android应用程序中实例化类,java,android,Java,Android,我正在开发一个Android应用程序。在我的应用程序中,我对另一个类有兴趣。一旦发生这种情况,应用程序就会崩溃。它说它不能实例化我的类。我的代码如下: public class CustomObject extends Activity{ ArrayList<String> Alerts; ArrayList<String> Names; public void onCreate(Bundle savedInstanceState) { super.onCrea
public class CustomObject extends Activity{
ArrayList<String> Alerts;
ArrayList<String> Names;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Names = intent.getExtras().getStringArrayList("Names");
Alerts = intent.getExtras().getStringArrayList("Alerts");
}
public CustomObject(ArrayList<String> prop1, ArrayList<String> prop2) {
this.Names = prop1;
this.Alerts = prop2;
}
public ArrayList<String> getProp1() {
return Names;
}
public ArrayList<String> getProp2() {
return Alerts;
}
}
我在代码中找不到问题。其他一些问题已经提到了无参数构造函数,但是我如何实现它呢?这就是我的问题吗?感谢您的帮助。活动和片段可能没有带参数(也称为参数)的构造函数,如下所示:
public CustomObject(ArrayList<String> prop1, ArrayList<String> prop2) {
this.Names = prop1;
this.Alerts = prop2;
}
然后在活动的onCreate
方法中,可以使用以下方法检索值:
Intent intent = getIntent();
List<String> list1 = new ArrayList<String>(Arrays.asList(intent.getStringArrayExtra("prop1")));
List<String> list2 = new ArrayList<String>(Arrays.asList(intent.getStringArrayExtra("prop2")));
Intent-Intent=getIntent();
List list1=新的ArrayList(Arrays.asList(intent.getStringArrayExtra(“prop1”));
List list2=新的ArrayList(Arrays.asList(intent.getStringArrayExtra(“prop2”));
因为不可能轻易地在意图上发送
列表
,所以在这里它被转换为字符串数组,然后在onCreate方法中返回列表。活动和片段可能没有带参数(也称为参数)的构造函数,如下所示:
public CustomObject(ArrayList<String> prop1, ArrayList<String> prop2) {
this.Names = prop1;
this.Alerts = prop2;
}
然后在活动的onCreate
方法中,可以使用以下方法检索值:
Intent intent = getIntent();
List<String> list1 = new ArrayList<String>(Arrays.asList(intent.getStringArrayExtra("prop1")));
List<String> list2 = new ArrayList<String>(Arrays.asList(intent.getStringArrayExtra("prop2")));
Intent-Intent=getIntent();
List list1=新的ArrayList(Arrays.asList(intent.getStringArrayExtra(“prop1”));
List list2=新的ArrayList(Arrays.asList(intent.getStringArrayExtra(“prop2”));
因为不可能轻易地在意图上发送
列表
,所以在这里它被转换为字符串数组,然后在onCreate方法中返回列表。继承活动
类或片段
的类必须具有空构造函数。读一读这个
在您当前的情况下,您可能会尝试通过保留此经过论证的构造函数并调用super()
,来解决此问题,注意:这是空的超类构造函数调用
我不喜欢这种方法。Simon通过intent发送的解决方案是正确的方法。继承
活动的类或片段的类必须具有空构造函数。读一读这个
在您当前的情况下,您可能会尝试通过保留此经过论证的构造函数并调用super()
,来解决此问题,注意:这是空的超类构造函数调用
我不喜欢这种方法。Simon通过intent发送的解决方案是正确的方法。活动需要一个非参数构造函数。现在,您添加了一个带有两个参数的构造函数。因此,您还应该添加一个非构造函数:
public CustomObject(){
}
活动需要一个非参数构造函数。现在,您添加了一个具有两个参数的构造函数。因此,您还应该添加一个非构造函数:
public CustomObject(){
}
没有无参数构造函数
正是您的问题。你读过你的日志了吗,还是只是C&P在这里让我们为你读<代码>原因:java.lang.InstanceionException:无法实例化类com.dev.chicagotraintracker.CustomObject;没有空构造函数
我看到了它所说的,我只是不明白什么是无参数构造函数,我的代码的确切问题是什么。没有无参数构造函数
正是你的问题。你读过你的日志了吗,还是只是C&P在这里让我们为你读<代码>原因:java.lang.InstanceionException:无法实例化类com.dev.chicagotraintracker.CustomObject;无空构造函数
我看到了它所说的内容,我只是不明白无参数构造函数是什么,我的代码的确切问题是什么。我将如何创建该意图?这个类是什么?有没有办法在没有意图的情况下将这个ArrayList传递给另一个类?我不想让它显示我上面的类,我只想将数据从UI类传递给该类,然后再传递给另一个,然后再传递回UI类。@hichris123可以通过应用程序对象“传递”(或者说存储)任何对象,有关这方面的详细信息,我如何创建该意图?这个类是什么?有没有办法在没有意图的情况下将这个ArrayList传递给另一个类?我不想让它显示我上面的类,我只想将数据从UI类传递给该类,然后再传递给另一个,然后再传递回UI类。@hichris123可以通过应用程序对象“传递”(或者说存储)任何对象,有关这方面的详细信息,请调用super()
由Java隐式插入。Java隐式地插入了对super()
的调用。这不会改变任何事情。