在父级 Activity 中实现内部片段 class
Implements inner Fragment class in parent Activity
我有以下代码:
public class SettingsActivity extends AppCompatActivity implements SettingsActivity.SettingsFragment.SendToActivity {
...
public static class SettingsFragment extends PreferenceFragmentCompat {
SendToActivity callback;
public void setSendToActivity (SendToActivity callback) {
this.callback = callback;
}
public interface SendToActivity {
public void send(int result);
}
...
}
@Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof SettingsFragment) {
SettingsFragment settingsFragment = (SettingsFragment) fragment;
settingsFragment.setSendToActivity(this);
}
}
public void send(int result) {
...
}
}
但我收到以下错误:cyclic inheritance involving SettingsActivity
在 SettingsActivity class 的声明中。我做错了什么?
尝试将 SendToActivity
移出 SettingsFragment
,例如:
public class SettingsActivity extends AppCompatActivity implements SettingsActivity.SendToActivity {
public interface SendToActivity {
public void send(int result);
}
...
public static class SettingsFragment extends PreferenceFragmentCompat {
SendToActivity callback;
public void setSendToActivity (SendToActivity callback) {
this.callback = callback;
}
...
}
@Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof SettingsFragment) {
SettingsFragment settingsFragment = (SettingsFragment) fragment;
settingsFragment.setSendToActivity(this);
}
}
public void send(int result) {
...
}
}
我自己回答:片段 class 需要在其自己的 java 文件中。那就正常实现吧(implements SettingsFragment.SendToActivity
).
我有以下代码:
public class SettingsActivity extends AppCompatActivity implements SettingsActivity.SettingsFragment.SendToActivity {
...
public static class SettingsFragment extends PreferenceFragmentCompat {
SendToActivity callback;
public void setSendToActivity (SendToActivity callback) {
this.callback = callback;
}
public interface SendToActivity {
public void send(int result);
}
...
}
@Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof SettingsFragment) {
SettingsFragment settingsFragment = (SettingsFragment) fragment;
settingsFragment.setSendToActivity(this);
}
}
public void send(int result) {
...
}
}
但我收到以下错误:cyclic inheritance involving SettingsActivity
在 SettingsActivity class 的声明中。我做错了什么?
尝试将 SendToActivity
移出 SettingsFragment
,例如:
public class SettingsActivity extends AppCompatActivity implements SettingsActivity.SendToActivity {
public interface SendToActivity {
public void send(int result);
}
...
public static class SettingsFragment extends PreferenceFragmentCompat {
SendToActivity callback;
public void setSendToActivity (SendToActivity callback) {
this.callback = callback;
}
...
}
@Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof SettingsFragment) {
SettingsFragment settingsFragment = (SettingsFragment) fragment;
settingsFragment.setSendToActivity(this);
}
}
public void send(int result) {
...
}
}
我自己回答:片段 class 需要在其自己的 java 文件中。那就正常实现吧(implements SettingsFragment.SendToActivity
).