我创建了一个Modal Bottom Sheet
片段,默认情况下这些行
/**
* <p>A fragment that shows a list of items as a modal bottom sheet.</p>
* <p>You can show this modal bottom sheet from your activity like this:</p>
* <pre>
* ShoppingCartListDialogFragment.newInstance(30).show(getSupportFragmentManager(), "dialog");
* </pre>
* <p>You activity (or fragment) needs to implement {@link ShoppingCartListDialogFragment.Listener}.</p>
*/
然后在我的MainActivity.java
我有onClick
这样的功能:
public void showShoppingCart(View view) {
// Testing
ShoppingCartListDialogFragment.newInstance(30).show(getSupportFragmentManager(), "dialog");
}
每当我点击按钮时,我的应用程序崩溃,我从片段的onAttach
方法中得到错误
@Override
public void onAttach(Context context) {
super.onAttach(context);
final Fragment parent = getParentFragment();
if (parent != null) {
mListener = (Listener) parent;
} else {
mListener = (Listener) context; <-- Error on this line
}
}
有谁知道我在这里失踪了什么?我真的没有得到最后的评论。我在哪里实施.Listener
?
错误日志:
FATAL EXCEPTION: main
Process: com.example.johng.assosfood, PID: 28856
java.lang.ClassCastException: com.example.johng.assosfood.ProductsActivity cannot be cast to com.example.johng.assosfood.ShoppingCartListDialogFragment$Listener
at com.example.johng.assosfood.ShoppingCartListDialogFragment.onAttach(ShoppingCartListDialogFragment.java:59)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1367)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1754)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1822)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2591)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2378)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2333)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2240)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
答案 0 :(得分:1)
当我为片段设置监听器时,我的onAttach()通常看起来像这样:
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof yourFragmentListener) {
mListener = (yourFragmentListener) context;
} else {
// Error Code
}
}
这种方式只有在它附加的活动实现其侦听器时才会强制转换。如果您收到错误代码,则可能无法在MainActivity中实现片段的侦听器。像这样:
public class MainActivity extends Activity implements ShoppingCartListDialog.Listener {
// If it successfully implements, (you have all the methods
//defined in this class) then it is an instance of the listener
}