Intent 在片段内部无法打开 Activity

Intent is not working inside fragment to open Activity

我正在尝试在 TabActivity 的片段的 PlaceholderFragment class 中使用 Intent 来打开下一个 Activity。

这是我的代码:

PlaceHolderFragment class

    public ImageButton Next;
     public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View rootView=inflater.inflate(R.layout.fragment_main_tab,container,true);
        Next=(ImageButton)rootView.findViewById(R.id.btn_expand);
        Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updatedetails();
            }
        });

        View root=null;
        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1:
                 root = inflater.inflate(R.layout.fragment_main_tab, container, false);
                break;
            case 2:
                    root = inflater.inflate(R.layout.fragment_team_screen_, container, false);
                    break;
        }
        return root;
    }
public void updatedetails(){
    Intent intent = new Intent(getActivity(), Enquiry_followup.class);
    startActivity(intent);
}

你可以试试下面的方法吗?充气机的最后一个参数,设为false。

View rootView = inflater.inflate(R.layout.fragment_main_tab,container,false);

当您不负责将子视图添加到父视图时,切勿将 attachToRoot 传递为 true。

您可以从这个回答@NOW OR NOT NOW

中阅读更多相关信息

getActivity() 不是在片段中获取上下文的最佳方式。 并在创建视图时使 attachToRoot = false 试试下面的代码

public class yourClass{
public ImageButton Next;

 Context context;
@Override public void onAttach(Context context) { 
super.onAttach(context);
 this.context=context; 
}
     public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View rootView=inflater.inflate(R.layout.fragment_main_tab,container,false);
        Next=(ImageButton)rootView.findViewById(R.id.btn_expand);
        Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updatedetails();
            }
        });

        View root=null;
        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1:
                 root = inflater.inflate(R.layout.fragment_main_tab, container, false);
                break;
            case 2:
                    root = inflater.inflate(R.layout.fragment_team_screen_, container, false);
                    break;
        }
        return root;
    }
public void updatedetails(){
    Intent intent = new Intent(context, Enquiry_followup.class);
    startActivity(intent);
}
}

试试这个和 post 更新。

Edit 1:Try returning rootView instead of root in onCreateView().

public class TestFragment extends Fragment {

public ImageButton Next;

public View onCreateView(
        @NonNull LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main_tab, container, false);


    View root = null;
    switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
        case 1:
            root = inflater.inflate(R.layout.fragment_main_tab, container, false);
            break;
        case 2:
            root = inflater.inflate(R.layout.fragment_team_screen_, container, false);
            break;
    }

    Next = (ImageButton) rootView.findViewById(R.id.btn_expand);
    Next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            updatedetails();
        }
    });

    return rootView;
}

public void updatedetails() {
    Intent intent = new Intent(getActivity(), Enquiry_followup.class);
    startActivity(intent);
}
}

像这样重新排列代码。并确保 xml 文件都包含带有 id - btn_expand

的 ImageButton

您的代码的问题是 onClickListener 在设置 view 之前。