Java 使用片段按钮打开另一个活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20735723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:56:20  来源:igfitidea点击:

Open Another Activity Using Fragment Button

javaandroidbuttonandroid-fragments

提问by Kodi

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.

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

Here's the Java code:

这是Java代码:

1st Method

第一种方法

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

2nd Method

方法二

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

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

采纳答案by VJ Vélan Solutions

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

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

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

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

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

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

在你的父活动(About片段的父)中,你有:

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: This solution is very specific for what your requirement. In general, it's best to handle the onClickevents related to the fragment inside the fragment class.

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

PS: Yes, as the other solution says, make sure you have registered the Contact_DeveloperActivity in your Manifestfile.

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

回答by user2845557

Have you declared the proper activity in the AndroidManifest.xml? Each activity other then the first should be declared inside the application tag, like this:

您是否在 AndroidManifest.xml 中声明了正确的活动?除了第一个活动之外的每个活动都应该在应用程序标签内声明,如下所示:

<activity
   android:name=".Contact_Developer"
   android:label="@string/app_name" >
</activity>

If it's not found, it will give force close.

如果没有找到,它将强制关闭。

回答by ericn

I think the issue here is that the activity is not ready.

我认为这里的问题是活动还没有准备好。

I would not recommend you following harikris's solution even though it works. It's usually bad practice to put code (onClick()which handles the click event) in your XML file. For example, it will be very difficult to hunt that piece of code down to review/ change it in the future yourself, not to mention another developer in your team.

即使它有效,我也不建议您遵循harikris的解决方案。将代码(onClick()处理点击事件)放在 XML 文件中通常是不好的做法。例如,您将来很难找到那段代码以进行/更改,更不用说团队中的其他开发人员了。

I'd suggest to listen to the event the activity is ready i.e. onActivityCreated()within your Fragment class. Override the implementation of the same method and call something in your activity e.g. onFragmentReady() like in the example below.

我建议收听活动准备就绪的事件,即onActivityCreated()在您的 Fragment 类中。覆盖相同方法的实现并在您的活动中调用某些内容,例如 onFragmentReady(),如下例所示。

Fragment class:

片段类

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

    @Override
        public void onActivityCreated (Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            ((MyActivity) this.getActivity()).onFragmentReady();
        } 
}  

Activity class:

活动类

public class MyActivity extends FragmentActivity {
    private void onFragmentReady() {
        Log.i(TAG, "testing");
         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);
            }
        });
    }

    public class About extends Fragment {
        ...
    }
}

回答by ginnamann

I think you should use getContext()or getApplicationContext()instead of getActivity()so code will be Intent intent =new Intent(getContext(),Contact_Developer.class); startActivity(intent);

我认为你应该使用getContext()getApplicationContext()代替getActivity()这样的代码 Intent intent =new Intent(getContext(),Contact_Developer.class); startActivity(intent);