为什么我的应用会在Android上的ImageView点击中从一个片段移动到另一个片段?

时间:2014-04-22 08:56:12

标签: android android-fragments

我在点击mycontacts extends Fragment时尝试从tasks extends Fragment移至ImageView。我的ImageView位于使用ListView生成的SimpleAdapter。我的布局的ID如下:R.id.tasky表示任务片段,R.id.mycontacts表示mycontacts片段。我的代码和错误日志如下。因为我是碎片的新手,请一步一步向我解释。

mycontacts扩展Fragment

 purple.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
     android.app.FragmentTransaction t = getActivity().getFragmentManager().beginTransaction();
        Fragment fragment = new tasks();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.tasky, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
}
});

错误日志:

04-22 04:41:52.486: E/FragmentManager(3250): No view found for id 0x7f04004f (com.example.taskmanager:id/tasky) for fragment tasks{b4ee0f70 #1 id=0x7f04004f}
04-22 04:41:52.486: E/FragmentManager(3250): Activity state:
04-22 04:41:53.626: E/AndroidRuntime(3250): FATAL EXCEPTION: main
04-22 04:41:53.626: E/AndroidRuntime(3250): Process: com.example.taskmanager, PID: 3250
04-22 04:41:53.626: E/AndroidRuntime(3250): java.lang.IllegalArgumentException: No view found for id 0x7f04004f (com.example.taskmanager:id/tasky) for fragment tasks{b4ee0f70 #1 id=0x7f04004f}
04-22 04:41:53.626: E/AndroidRuntime(3250):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at android.os.Handler.handleCallback(Handler.java:733)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at android.os.Looper.loop(Looper.java:136)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at java.lang.reflect.Method.invoke(Method.java:515)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-22 04:41:53.626: E/AndroidRuntime(3250):     at dalvik.system.NativeStart.main(Native Method)
04-22 04:45:17.276: E/FragmentManager(3351): No view found for id 0x7f04004f (com.example.taskmanager:id/tasky) for fragment tasks{b4ef7ac0 #1 id=0x7f04004f}
04-22 04:45:17.276: E/FragmentManager(3351): Activity state:
04-22 04:45:18.476: E/AndroidRuntime(3351): FATAL EXCEPTION: main
04-22 04:45:18.476: E/AndroidRuntime(3351): Process: com.example.taskmanager, PID: 3351
04-22 04:45:18.476: E/AndroidRuntime(3351): java.lang.IllegalArgumentException: No view found for id 0x7f04004f (com.example.taskmanager:id/tasky) for fragment tasks{b4ef7ac0 #1 id=0x7f04004f}
04-22 04:45:18.476: E/AndroidRuntime(3351):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at android.os.Handler.handleCallback(Handler.java:733)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at android.os.Looper.loop(Looper.java:136)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at java.lang.reflect.Method.invoke(Method.java:515)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-22 04:45:18.476: E/AndroidRuntime(3351):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您的mycontacts Fragment xml没有ViewGroup,其ID为R.id.tasky。您可以使用interface作为对活动的回调,然后将容器中的现有片段替换为另一个片段。

您可以找到代码片段@

http://developer.android.com/training/basics/fragments/communicating.html

在文档示例中,ListFragment上的ListItemClick将值传递给Activity。然后在活动中用新的片段替换现有的片段并将值传递给它。

同样在ImageView上点击与活动进行通信,然后从活动中替换容器中的相应片段。