我已经为选项卡式活动创建了自己的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无法转换为上下文)