无论我尝试什么,片段上的按钮都不会链接到其他活动

我已经为选项卡式活动创建了自己的Java类片段,并且一切进行得很好,直到必须将一些图像按钮链接到其他活动上。

出于我不理解的原因,我用来在其他活动上做按钮的代码在我的片段页面上不起作用。

我已经弄清楚了如何使用getView在片段上使用findViewbyId ...,但是仍然不能让我将图像按钮链接到另一个活动。

package com.example.reachv2.ui.main;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.reachv2.HelpLine;
import com.example.reachv2.R;

public class Frag2 extends Fragment {
    private ImageButton button;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag2_layout,container,false);
        button = (ImageButton) getView().findViewById(R.id.imageButton2);
        button.setOnClicklistener(new View.OnClicklistener() {
            @Override
            public void onClick(View v) {
                openHelpLine();
            }
        });
        }

    public void openHelpLine() {
        Intent intent = new Intent(Frag2.this,HelpLine.class);
        startactivity(intent);
    }
}
  

错误:找不到适合的构造函数   意向(Frag2,Class)       构造函数Intent.Intent(String,Uri)不适用       (参数不匹配; Frag2无法转换为String)       构造函数Intent.Intent(Context,Class)不适用       (参数不匹配; Frag2无法转换为上下文)

mlefk 回答:无论我尝试什么,片段上的按钮都不会链接到其他活动

首先,您正在使用一个不存在的Intent构造函数,编译器在错误部分对此进行了说明。

第二,我认为您正在使用Intent重定向到片段,您可以尝试这种方法吗?

NextFragment nextFrag= new NextFragment();
getActivity().getSupportFragmentManager().beginTransaction()
         .replace(R.id.Layout_container,nextFrag,"findThisFragment")
         .addToBackStack(null)
         .commit();
,

代替new Intent(Frag2.this,HelpLine.class);试试这个new Intent(getContext(),HelpLine.class);

,

尝试:

   @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.frag2_layout,container,true);
            button = (ImageButton) view .findViewById(R.id.imageButton2);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openHelpLine();
                }
            });
            }

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

尝试一下(Kotlin版本):

class Frag2 : Fragment(),View.OnClickListener {

    private var button: ImageButton? = null

    override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.frag2_layout,false)
        button = view.findViewById(R.id.imageButton2)
        button?.setOnClickListener(this)
        return view
    }

    private fun openHelpLine() {
        val intent = Intent(requireContext(),HelpLine::class.java)
        startActivity(intent)
    }

    override fun onClick(v: View) {
        when (v) {
            button -> openHelpLine()
        }
    }
}
,

Intent需要将Context作为其第一个参数。 上下文有2种

1)应用上下文

2)活动上下文

上下文是在其中运行应用程序的环境的句柄。

在您的代码上下文中,这里是Activity,因为片段是Activity的一部分。

片段不是上下文。

活动是片段的父对象或容器。 所以片段的上下文始终是Activity ..

为此,您需要使用getActivity()代替Frag2。this

本文链接:https://www.f2er.com/3116066.html

大家都在问