使用按钮从片段中启动新活动

时间:2016-07-13 06:40:36

标签: java android button android-activity fragment

我是Java和Android的新手,我遇到了一些问题。

我使用导航抽屉活动与链接到片段的每个菜单项。从片段中,我尝试使用链接到更多活动的按钮来设置另一个菜单。

这是我想要链接到更多活动的片段:

e.preventDefault()

我收到了这些错误:

form.submit()
  

rootView突出显示为红色"无法解析符号rootView"

     

setOnClickListener突出显示为红色"无法解析符号   setOnClickListener"

     

@Override突出显示为红色..."此处不允许使用注释"

     

视图以红色突出显示"无法解析符号视图"

我的按钮ID为package com.notimportant import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; /** * A simple {@link Fragment} subclass. */ public class About extends Fragment { public About() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_about, container, false); } Button button = (Button) rootView.findViewById(R.id.buttonDevs); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getActivity(), DevsActivity.class); startActivity(intent); } }); } 。我想要去的活动叫做Error:(35, 30) error: <identifier> expected Error:(35, 31) error: illegal start of type Error:(35, 34) error: ')' expected Error:(35, 39) error: ';' expected Error:(35, 40) error: invalid method declaration; return type required Error:(37, 28) error: ';' expected Error:(37, 38) error: ';' expected Error:(41, 6) error: illegal start of type

我做错了什么?

5 个答案:

答案 0 :(得分:2)

您必须将Button放入rootView

更改此

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_about, container, false);
    }

    Button button = (Button) rootView.findViewById(R.id.buttonDevs);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), DevsActivity.class);
            startActivity(intent);
        }
    });

到这个

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

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

     Button button = (Button) rootView.findViewById(R.id.buttonDevs);
     button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), DevsActivity.class);
            startActivity(intent);
        }
    });

        return rootView;
    }

答案 1 :(得分:0)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_about, container, false);
    Button button = (Button) view.findViewById(R.id.buttonDevs);
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(), DevsActivity.class);
        startActivity(intent);
    }
});
    return view;
}

答案 2 :(得分:0)

改变这个:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_about, container, false);
}

Button button = (Button) rootView.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(), DevsActivity.class);
        startActivity(intent);
    }
});

就此:

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

    Button button = (Button) rootView.findViewById(R.id.buttonDevs);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), DevsActivity.class);
            startActivity(intent);
        }
    });

    return rootView;
}

答案 3 :(得分:0)

将您的按钮放在onCreateView()内,如下所示 -

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_about, container, false);
        Button button = (Button) view.findViewById(R.id.buttonDevs);
        button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
              Intent intent = new Intent(getActivity(), DevsActivity.class);
              startActivity(intent);
           }
        });
        return view;
    }

答案 4 :(得分:0)

您的代码必须位于onViewCreated()方法内,因为您无法与来自onCreateView()方法的视图进行交互

 @Override
    public void onViewCreated(View rootView, @Nullable Bundle savedInstanceState) {

Button button = (Button) rootView.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(), DevsActivity.class);
        startActivity(intent);
    }
});
}
祝你好运