【问题标题】:Intent is not working inside fragment to open Activity意图在片段内部不起作用以打开 Activity
【发布时间】:2019-06-27 08:45:02
【问题描述】:

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

这是我的代码:

PlaceHolderFragment

    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);
}

【问题讨论】:

  • 你得到什么错误?
  • 没有错误但无法打开活动。我也将检查Logcat
  • 把 Log.e() 放在 startActivity(intent) 之后;行并查看日志行打印与否?
  • 尝试将 onClick 监听器放入方法 onViewCreated...
  • @Aniket_1994 你能发布 logcat 吗?

标签: android android-intent fragment


【解决方案1】:
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 的 ImageButton - btn_expand

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

【讨论】:

  • 非常感谢@MilanJoseph...设置视图对我有帮助。
  • 使用您的代码后,我的 SWITCH CASE 不起作用。我无法在 tab2 中看到不同的字段。只有我可以看到与 tab 1 中相同的字段。 @米兰约瑟夫
  • 在开关中我给出了 1 个硬编码,请提供您的登录信息。然后它会工作。
【解决方案2】:

你可以试试下面的。充气机的最后一个参数,设为假。

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

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

您可以从此答案@NOW OR NOT NOW了解更多信息

【讨论】:

  • 代码运行时是否到达startActivity()方法?
【解决方案3】:

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);
}
}

试试这个并发布更新。

编辑 1:尝试在 onCreateView() 中返回 rootView 而不是 root。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2020-11-15
    相关资源
    最近更新 更多