如何在片段中传递值?

use*_*576 1 android android-fragments

我是安卓新手。我正在尝试按照这些视频链接中的教程进行操作。我知道这里有很多教程和我的相同问题,但我想知道当我单击提交按钮时我的应用程序停止的确切原因。抱歉,我还不熟悉它们在 android studio 中的功能以及系统的工作原理。我尝试创建 2 个片段,顶部和底部片段。在顶部片段中有一个提交表单。当我点击提交按钮时。该值将显示到底部片段。这就像将值传递给另一个片段。我创建了一个片段类名 TopSectionFragment

package com.example.aldrin.fragmenttutorial;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;

public class TopSectionFragment extends Fragment{
    @Nullable
    private static EditText topTextInput;
    private static EditText bottomTextInput;

    TopSectionListener activityCommander;

    public interface TopSectionListener{
        public void createMem(String top, String bottom);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        Activity activity = context instanceof Activity ? (Activity) context : null;

        try{
            activityCommander = (TopSectionListener) activity;
        }catch (ClassCastException e){
            throw new ClassCastException(activity.toString());
        }
    }



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

        View view = inflater.inflate(R.layout.top_section_fragment,container,false);

        topTextInput = (EditText)view.findViewById(R.id.topText);
        bottomTextInput = (EditText)view.findViewById(R.id.bottomText);

        final Button button = (Button) view.findViewById(R.id.submitButton);

        button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        buttonClick(v);
                    }
                }
        );

        return view;
    }

    public void buttonClick(View v){
        activityCommander.createMem(topTextInput.getText().toString(),bottomTextInput.getText().toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

我为底部创建了另一个片段。我将其命名为 BottomPictureFragment

package com.example.aldrin.fragmenttutorial;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class BottomPictureFragment extends Fragment {

    private static TextView t1;
    private static TextView t2;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.bottom_picture_fragment,container,false);

        t1 = (TextView) view.findViewById(R.id.topText);
        t2 = (TextView) view.findViewById(R.id.bottomText);
        return view;
    }

    public void setMemeText(String top, String bottom){
        t1.setText(top);
        t2.setText(bottom);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的项目中使用 Kitkat。当我按照视频教程进行操作时,onAttach(Activity activity)已被弃用。我尝试使用onAttach(Context context)但同样的问题。当我将它运行到我的华硕手机时。当我提交时它会停止该过程。这是我的 MainActity 的代码

package com.example.aldrin.fragmenttutorial;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void createMem(String top, String bottom) {
        BottomPictureFragment bottomFragment = (BottomPictureFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
        bottomFragment.setMemeText(top,bottom);
    }
Run Code Online (Sandbox Code Playgroud)

}

但是,如果您提供另一种将值传递给其他片段的解决方案。会更受赞赏。它将帮助我学习更多。希望得到您的帮助。我只是从视频中复制代码。

我收到了这些错误

10-27 13:42:49.436 6176-6176/com.example.users.fragmenttutorial W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
10-27 13:42:49.436 6176-6176/com.example.users.fragmenttutorial W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-27 13:42:51.636 6176-6176/com.example.users.fragmenttutorial D/AndroidRuntime: Shutting down VM
10-27 13:42:51.636 6176-6176/com.example.users.fragmenttutorial W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x430f2140)
10-27 13:42:51.636 6176-6176/com.example.users.fragmenttutorial E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.users.fragmenttutorial, PID: 6176
                                                                                   java.lang.NullPointerException
                                                                                       at com.example.users.fragmenttutorial.BottomPictureFragment.setMemeText(BottomPictureFragment.java:28)
                                                                                       at com.example.users.fragmenttutorial.MainActivity.createMem(MainActivity.java:18)
                                                                                       at com.example.users.fragmenttutorial.TopSectionFragment.buttonClick(TopSectionFragment.java:64)
                                                                                       at com.example.users.fragmenttutorial.TopSectionFragment$1.onClick(TopSectionFragment.java:55)
                                                                                       at android.view.View.performClick(View.java:4478)
                                                                                       at android.view.View$PerformClick.run(View.java:18698)
                                                                                       at android.os.Handler.handleCallback(Handler.java:733)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:149)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                                       at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

Uma*_*air 5

将数据从一个片段传递到另一个片段的简单方法是这样的:使用 Bundle 发送String/int或任何您想发送的内容:

TopSectionFragment firstFragment = new TopSectionFragment();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
firstFragment.setArguments(args);

//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, firstFragment).commit();
Run Code Online (Sandbox Code Playgroud)

并在 BottomPictureFragment 的 onCreateView 中:

//Retrieve the value
String value = getArguments().getString("YourKey");
Run Code Online (Sandbox Code Playgroud)

您可以在这些链接中找到有关如何将数据从一个片段通信或发送到另一个片段的更多帮助: 如何在片段之间传递值

如何将数据从一个 Fragment 发送到另一个 Fragment?(从这里得到答案)

如何在 Android 中将值从一个 Fragment 传递到另一个?

希望它有助于欢呼。