Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在片段中单击按钮时检索EditText和微调器?_Java_Android_Android Fragments - Fatal编程技术网

Java 如何在片段中单击按钮时检索EditText和微调器?

Java 如何在片段中单击按钮时检索EditText和微调器?,java,android,android-fragments,Java,Android,Android Fragments,我试图从EditText中获取字符串,并在片段中单击按钮时使用微调器 public void buttonClick (View view) { Spinner stars = findViewById(R.id.starsSpinner); String starsText = stars.getSelectedItem().toString(); EditText time = findViewById(R.id.editTime); String tim

我试图从EditText中获取字符串,并在片段中单击按钮时使用微调器

public void buttonClick (View view) {

    Spinner stars = findViewById(R.id.starsSpinner);
    String starsText = stars.getSelectedItem().toString();

    EditText time = findViewById(R.id.editTime);
    String timeText = time.getText().toString();

    EditText location = findViewById(R.id.editLocation);
    String locationText = location.getText().toString();

    String space = " ";

    String shareBody ="!r " + starsText + space + locationText + space + timeText;

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setClassName("com.discord", "com.discord.app.AppActivity$AppAction");
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

    startActivity(Intent.createChooser(sharingIntent, shareBody));

}
我在一个活动中编写了一些代码,但我不知道如何更改它,使其在片段中工作

public void buttonClick (View view) {

    Spinner stars = findViewById(R.id.starsSpinner);
    String starsText = stars.getSelectedItem().toString();

    EditText time = findViewById(R.id.editTime);
    String timeText = time.getText().toString();

    EditText location = findViewById(R.id.editLocation);
    String locationText = location.getText().toString();

    String space = " ";

    String shareBody ="!r " + starsText + space + locationText + space + timeText;

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setClassName("com.discord", "com.discord.app.AppActivity$AppAction");
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

    startActivity(Intent.createChooser(sharingIntent, shareBody));

}
活动中的代码与discord应用程序共享一个字符串。我对碎片的每一次修改都会使应用程序崩溃


谢谢您的帮助。

首先,您不能像处理活动一样一次点击一个片段 因此,您应该从xml中删除这一行:

        android:onClick="someMethod"
然后在片段中初始化button对象并调用 它的setOnClickListener方法

看看这个:

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

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

    //using findViewById() in a Fragment
    Button button=rootView.findViewById(R.id.button_id);

    //handle onclick events for the button
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            EditText time = rootView.findViewById(R.id.editTime);

        }
    });

    return rootView;
}

我希望这能有所帮助。

您的片段代码在哪里?你必须给我们提供更多的细节来帮助。加上日志中显示的错误,你真是天才。在这种格式下工作得非常好。我很高兴终于有了代码工作后,我拔出头发的年龄。非常感谢。@Steve不客气。如果答案对你有帮助,请接受