将片段嵌套在片段中的麻烦

时间:2014-08-22 09:40:14

标签: android android-fragments android-nested-fragment

我是java和android的初学者,我正在开发一个项目,其中我有一个片段中使用片段的情况,我已经编写了一个程序(如下所述),我正面临一个应用程序崩溃。

主要片段:

public class Register extends Fragment implements OnClickListener {
    FragmentManager manager = getChildFragmentManager();
    ImageButton newReg;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View register = inflater.inflate(R.layout.register, container, false);
        // TODO Auto-generated method stub
        newReg = (ImageButton) register.findViewById(R.id.newreg);
        newReg.setOnClickListener(this);
        return register;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Fragment details = new Details();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(details, "adada");
        transaction.commit();
    }

}

嵌套片段:

public class Details extends Fragment {
    EditText firstN,lastN,Phone,Email;
    Button Confirm;
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View details=inflater.inflate(R.layout.register_details, container, false);
    return details;
}
}

Logcat输出:

    08-22 15:30:32.907: D/OpenGLRenderer(31032): Enabling debug mode 0
08-22 15:30:35.095: D/AndroidRuntime(31032): Shutting down VM
08-22 15:30:35.095: W/dalvikvm(31032): threadid=1: thread exiting with uncaught exception (group=0x41674d40)
08-22 15:30:35.099: E/AndroidRuntime(31032): FATAL EXCEPTION: main
08-22 15:30:35.099: E/AndroidRuntime(31032): Process: com.example.apeonomy, PID: 31032
08-22 15:30:35.099: E/AndroidRuntime(31032): java.lang.IllegalStateException: No activity
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1853)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.Fragment.performActivityCreated(Fragment.java:1714)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.os.Handler.handleCallback(Handler.java:733)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.os.Looper.loop(Looper.java:136)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at android.app.ActivityThread.main(ActivityThread.java:5086)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at java.lang.reflect.Method.invoke(Method.java:515)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-22 15:30:35.099: E/AndroidRuntime(31032):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

Logcat显示有问题的Line:

newReg = (ImageButton) register.findViewById(R.id.newreg);

您已在注册中将 newreg 定义为按钮,并将其初始化为 ImageButton

答案 1 :(得分:0)

检查布局文件register.xml。标识为newreg的视图为Button类型,但当您尝试在ImageButton中获取该视图时,您将其转换为onCreateView

答案 2 :(得分:0)

将FrameLayout放在register.xml文件中,如下所示

 <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/anyView"
        android:layout_below="@+id/anyOtherView" 
/>

FrameLayout作为容器工作。然后修改你的onClick(View v)方法,如下所示:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Fragment details = new Details();
        FragmentManager fragmentManager = getActivity()
                .getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(R.id.container, details);
        fragmentTransaction.commit();
    }