gpt4 book ai didi

android - 从另一个 fragment 打开 fragment - 错误

转载 作者:行者123 更新时间:2023-11-29 20:27:40 26 4
gpt4 key购买 nike

..在其他 fragment 中这工作正常。

                Fragment fragment = null;
fragment = new Fragment_2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

但是在onclick事件中

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class Fragment1 extends Fragment implements
Button btnuevaventa;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
btnuevaventa=(Button)getView().findViewById(R.id.bt_nuevaventa);
btnuevaventa.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Fragment fragment = null;
fragment = new Fragment_2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
});

这是activity_main.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>

日志猫

09-01 16:50:53.039: E/AndroidRuntime(5555): FATAL EXCEPTION: main
09-01 16:50:53.039: E/AndroidRuntime(5555): java.lang.IllegalArgumentException: No view found for id 0x7f09003d (com.example.appcreativeventas:id/frame_container) for fragment Fragment_Inicio_Tab{420f42b0 #1 id=0x7f09003d}
09-01 16:50:53.039: E/AndroidRuntime(5555): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
09-01 16:50:53.039: E/AndroidRuntime(5555): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
09-01 16:50:53.039: E/AndroidRuntime(5555): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
09-01 16:50:53.039: E/AndroidRuntime(5555): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
09-01 16:50:53.039: E/AndroidRuntime(5555): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
09-01 16:50:53.039: E/AndroidRuntime(5555): at android.os.Handler.handleCallback(Handler.java:615)
09-01 16:50:53.039: E/AndroidRuntime(5555): at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 16:50:53.039: E/AndroidRuntime(5555): at android.os.Looper.loop(Looper.java:137)
09-01 16:50:53.039: E/AndroidRuntime(5555): at android.app.ActivityThread.main(ActivityThread.java:4960)
09-01 16:50:53.039: E/AndroidRuntime(5555): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 16:50:53.039: E/AndroidRuntime(5555): at java.lang.reflect.Method.invoke(Method.java:511)
09-01 16:50:53.039: E/AndroidRuntime(5555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-01 16:50:53.039: E/AndroidRuntime(5555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-01 16:50:53.039: E/AndroidRuntime(5555): at dalvik.system.NativeStart.main(Native Method)

最佳答案

你处理这个错误,你想通过托管它们的 Activity 加载 fragment ,而不是从其他 fragment ...所以你使用一个接口(interface)来告诉 Activity 执行代码,因为在这种情况下 R.id.fragment_container 是目前不是 Fragment 中的 id。尝试类似的东西:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class Fragment1 extends Fragment
Button btnuevaventa;
ClickCallback mCallback;
public interface ClickCallback{
void buttonClicked();
}

@Override
public void onAttached(Activity activity){
super.onAttach(activity);
mCallback = (ClickCallback) activity;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
btnuevaventa=(Button)getView().findViewById(R.id.bt_nuevaventa);
btnuevaventa.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

mCallback.buttonClicked();
}
});
}
}

然后在您的 Activity 中实现接口(interface):

Activity 实现 ClickCallback

@Override
public void buttonClicked(){
Fragment fragment = null;
fragment = new Fragment_2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

}

关于android - 从另一个 fragment 打开 fragment - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com