使用片段按钮打开另一个活动 [英] Open Another Activity Using Fragment Button

查看:26
本文介绍了使用片段按钮打开另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我尝试了两种类型的代码来让它工作,当我按下按钮进入另一个活动时,它不断给我强制关闭.我正在使用 Fragment 并且该 Fragment 代码中有一个按钮,但我似乎无法让它工作.我不是经验丰富的 Android 开发人员,但我正在努力学习.

Okay so I've tried two types of code to get this to work and it keeps giving me force closes when I press the button to go into another Activity. I'm using a Fragment and there's a button in that Fragments code but I can't seem to get it to work. I'm not an experienced Android developer but I'm trying my best to learn.

Java 代码如下:

第一种方法

public class About extends Fragment {

    Intent intent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

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

        intent = new Intent(getActivity(), Contact_Developer.class);
        final Button button = (Button) rootView.findViewById(R.id.btnContactDev);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(intent);
            }
        });

        return rootView;
    }
}

第二种方法

public class About extends Fragment {

    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

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

        Button button = (Button) rootView.findViewById(R.id.btnContactDev);
        button.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View arg0) {
              Intent intent = new Intent(getActivity(), Contact_Developer.class);
              getActivity().startActivity(intent);
           }
        });
        return rootView;
    }
}

我真的不知道发生了什么以及为什么我要强制关闭,但是如果有人可以帮助我并解释一下我做错了什么,那就足够了

I really don't know what's going on and why I'm getting force closes but if anyone could help me and explain a little what I did wrong that'd be more than enough

推荐答案

不要处理 Fragment 中的 Fragment 按钮的 onClick.让它去吧,这是父母的活动.并从父活动开始活动.

Do not handle the onClick for the fragment button in the Fragment. Let it go it's parent activity. And start the activity from the parent activity.

要确保按钮 onClick 事件发送到父活动,请确保在您的 about.xml 中,对于 id btnContactDev 的按钮,你有以下参数:

To make sure that the button onClick event is sent to the parent activity, make sure, in your about.xml, for the button with id btnContactDev, you have the following parameter:

<Button android:id="@+id/btnContactDev"
  android:onClick="buttonClick"
  ...
/>

并且在您的父活动(About 片段的父活动)中,您有:

and in your parent activity (parent of About fragment), you have:

public void buttonClick(View v) {
  switch(v.getId()) {
    case R.id.btnContactDev:
      Intent myIntent = new Intent();
      myIntent.setClassName(your_package_name_string, your_activity_name_string);
      // for ex: your package name can be "com.example"
      // your activity name will be "com.example.Contact_Developer"
      startActivity(myIntent);
    break;
  }
}

HTH.

PS:此解决方案非常适合您的要求.一般来说,最好在fragment类内部处理与fragment相关的onClick事件.

PS: This solution is very specific for what your requirement. In general, it's best to handle the onClick events related to the fragment inside the fragment class.

PS:是的,正如其他解决方案所说,请确保您已在 Manifest 文件中注册了 Contact_Developer 活动.

PS: Yes, as the other solution says, make sure you have registered the Contact_Developer Activity in your Manifest file.

这篇关于使用片段按钮打开另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆