onAttach 中无法初始化接口对象?
an interface object cannt be initialized in onAttach?
我创建了一个接口 DataPasserInterface
来将数据从片段传递到它的宿主 activity。我在 onAttach
中初始化了界面,如下面的代码所示,问题是,在 运行 时,应用程序崩溃了,我收到下面发布的 logCat
错误。
不知道为什么this.dataPasserObj = (DataPasserInterface) activity;
会报错? logcat 说,接口对象不能转换?!
界面:
private DataPasserInterface dataPasserObj;
...
...
...
public interface DataPasserInterface {
void onTopicsListChosen(ArrayList<String> chosenTopicsList);
}
onAttach:
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Log.w(TAG, "@onAttach().");
this.dataPasserObj = (DataPasserInterface) activity;
this.designList = new ArrayList<ItemDesign>();
}
logcat:
E/AndroidRuntime(7828): FATAL EXCEPTION: main
E/AndroidRuntime(7828): Process: com.example.mqtt_designlayout_02, PID: 7828
E/AndroidRuntime(7828): java.lang.ClassCastException: com.example.mqtt_designlayout_02.SettingsActivity cannot be cast to com.example.mqtt_designlayout_02.DataPasserInterface
E/AndroidRuntime(7828): at com.example.mqtt_designlayout_02.Eco_Settings_Frag.onAttach(Eco_Settings_Frag.java:95)
更新
onCreate() 的 activity 托管片段,此 activity 实现接口
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w(TAG, "@onCreate().");
this.mqttSettingsDB = new MQTT_Settings_DB(this);
this.sqliteDB = this.mqttSettingsDB.getWritableDatabase();
if (this.fragList == null) {
this.fragList = new ArrayList<Fragment>();
this.fragList.add(new MQTT_Settings_Frag());
this.fragList.add(new Eco_Settings_Frag());
this.fragList.add(new Logger_Settings_Frag());
}
initviews(R.layout.settings_activity);
setUpActionBar();
this.viewPager.setOnPageChangeListener(viewPagerListener);
}
ClassCastException: com.example.mqtt_designlayout_02.SettingsActivity
可能没有在 SettingsActivity
中实现 DataPasserInterface
接口
建议在 Eco_Settings_Frag
class 中创建构造函数以获取 DataPasserInterface
接口并初始化其他值:
public Eco_Settings_Frag(Activity activity){
this.dataPasserObj = (DataPasserInterface) activity;
this.designList = new ArrayList<ItemDesign>();
}
并在创建 Eco_Settings_Frag
class 的对象时传递 SettingsActivity
上下文:
.....
this.fragList.add(new Eco_Settings_Frag(SettingsActivity.this));
this.fragList.add(new Logger_Settings_Frag());
.....
也在SettingsActivity
Activity
中实现DataPasserInterface
接口
我创建了一个接口 DataPasserInterface
来将数据从片段传递到它的宿主 activity。我在 onAttach
中初始化了界面,如下面的代码所示,问题是,在 运行 时,应用程序崩溃了,我收到下面发布的 logCat
错误。
不知道为什么this.dataPasserObj = (DataPasserInterface) activity;
会报错? logcat 说,接口对象不能转换?!
界面:
private DataPasserInterface dataPasserObj;
...
...
...
public interface DataPasserInterface {
void onTopicsListChosen(ArrayList<String> chosenTopicsList);
}
onAttach:
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Log.w(TAG, "@onAttach().");
this.dataPasserObj = (DataPasserInterface) activity;
this.designList = new ArrayList<ItemDesign>();
}
logcat:
E/AndroidRuntime(7828): FATAL EXCEPTION: main
E/AndroidRuntime(7828): Process: com.example.mqtt_designlayout_02, PID: 7828
E/AndroidRuntime(7828): java.lang.ClassCastException: com.example.mqtt_designlayout_02.SettingsActivity cannot be cast to com.example.mqtt_designlayout_02.DataPasserInterface
E/AndroidRuntime(7828): at com.example.mqtt_designlayout_02.Eco_Settings_Frag.onAttach(Eco_Settings_Frag.java:95)
更新
onCreate() 的 activity 托管片段,此 activity 实现接口
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w(TAG, "@onCreate().");
this.mqttSettingsDB = new MQTT_Settings_DB(this);
this.sqliteDB = this.mqttSettingsDB.getWritableDatabase();
if (this.fragList == null) {
this.fragList = new ArrayList<Fragment>();
this.fragList.add(new MQTT_Settings_Frag());
this.fragList.add(new Eco_Settings_Frag());
this.fragList.add(new Logger_Settings_Frag());
}
initviews(R.layout.settings_activity);
setUpActionBar();
this.viewPager.setOnPageChangeListener(viewPagerListener);
}
ClassCastException: com.example.mqtt_designlayout_02.SettingsActivity
可能没有在 SettingsActivity
DataPasserInterface
接口
建议在 Eco_Settings_Frag
class 中创建构造函数以获取 DataPasserInterface
接口并初始化其他值:
public Eco_Settings_Frag(Activity activity){
this.dataPasserObj = (DataPasserInterface) activity;
this.designList = new ArrayList<ItemDesign>();
}
并在创建 Eco_Settings_Frag
class 的对象时传递 SettingsActivity
上下文:
.....
this.fragList.add(new Eco_Settings_Frag(SettingsActivity.this));
this.fragList.add(new Logger_Settings_Frag());
.....
也在SettingsActivity
Activity
DataPasserInterface
接口