Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.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 通过单击按钮将数据从片段移动到新活动_Java_Android - Fatal编程技术网

Java 通过单击按钮将数据从片段移动到新活动

Java 通过单击按钮将数据从片段移动到新活动,java,android,Java,Android,我在一个Viewpager中有三个框架,我想通过单击其中一个按钮开始一个新的活动。我还想从四个微调器元素中获取一些信息。我可以从微调器获取信息并保存在全局变量中,但当我单击按钮时,变量变为空。我就是搞不懂这件事,快把我逼疯了 下面是我如何制作片段并找到正确的布局XML: public class SlideFragment extends Fragment { private int currentPageNumber; private View mainView;

我在一个Viewpager中有三个框架,我想通过单击其中一个按钮开始一个新的活动。我还想从四个微调器元素中获取一些信息。我可以从微调器获取信息并保存在全局变量中,但当我单击按钮时,变量变为空。我就是搞不懂这件事,快把我逼疯了
下面是我如何制作片段并找到正确的布局XML:

public class SlideFragment extends Fragment {

    private int currentPageNumber;
    private View mainView;

    public static SlideFragment create(int pageNumber, String origin) {
        SlideFragment myFragment = new SlideFragment();

        Bundle args = new Bundle();
        args.putInt("page", pageNumber);
        args.putString("origin", origin);
        myFragment.setArguments(args);

        return myFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentPageNumber = getArguments().getInt("page");
    }

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

        String origin = getArguments().getString("origin");
        // This is the mainActivity
        Rastimar rastimar = new Rastimar();

        if (origin == "profile") {
            if (currentPageNumber == 0) {
                mainView = inflater.inflate(R.layout.profile_screen1, container, false);
            }
            else {
                mainView = inflater.inflate(R.layout.profile_screen2, container, false);
            }
        }
        else if (origin == "rastimi") {
            switch (currentPageNumber) {
                case 0:
                    mainView = inflater.inflate(R.layout.rastimar, container, false);
                    rastimar.makeSpinners(mainView);
                    rastimar.makeListView(mainView);
                    break;
                case 1:
                    mainView = inflater.inflate(R.layout.rastima_leit, container, false);
                    rastimar.makeSpinnersForRastimaLeit(mainView);
                    break;
                case 2:
                    mainView = inflater.inflate(R.layout.rastima_yfirlit, container, false);
                    rastimar.makeExpandableListView(mainView);
                    rastimar.makeSpinnersForYfirlit(mainView);
                    break;
            }
        }

        return mainView;
    }
}
在这里,我从微调器获取数据,这是有效的

private class onSpinnerSelected implements AdapterView.OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent, View view,
                               int pos, long id) {
        // Find the spinner that was pressed by id
        switch(parent.getId()) {
            case R.id.dates:
                selectedRastimaDate = parent.getItemAtPosition(pos).toString();
                break;
            case R.id.courses:
                selectedCourse = parent.getItemAtPosition(pos).toString();
                break;
            case R.id.spinner_start_time:
                selectedStartTime = parent.getItemAtPosition(pos).toString();
                break;
            case R.id.spinner_end_time:
                selectedEndTime = parent.getItemAtPosition(pos).toString();
                break;
            case R.id.spinner_dates:
                selectedDate = parent.getItemAtPosition(pos).toString();
                break;
        }
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}
这是片段的XML布局文件

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="cityboys.golfapp.Rastimar">

    <TextView
        android:id="@+id/rastimaLeit_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textStyle="bold"
        android:textColor="#2d7165"
        android:layout_centerHorizontal="true"
        android:text="Rástímaleit"
        android:paddingBottom="10dp"/>

    <Spinner
        android:id="@+id/dates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rastimaLeit_title"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentLeft="true"/>

    <Spinner
        android:id="@+id/spinner_start_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rastimaLeit_title"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true"/>

    <Spinner
        android:id="@+id/courses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dates"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentLeft="true"/>

    <Spinner
        android:id="@+id/spinner_end_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner_start_time"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lausir tímar"
        android:id="@+id/button"
        android:layout_below="@+id/spinner_end_time"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:onClick="findAvailableTime" />

</RelativeLayout>

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="#2d7165"
    android:dividerHeight="0.5dp"
    android:background="#2b574e" />

我不确定我应该提供更多的信息,所以如果这还不够,就要求更多!
提前感谢。

XML看起来不错。但是将
滑动标签
定义为
公共静态类
,或将其移动到单独的文件中。会出现很多错误,请尝试理解并修复您可以解决的问题,然后更新问题。SlideFragment位于单独的文件中。那么有必要将其定义为公共静态类吗?不,这很好。您的“全局”变量如
selectedCourse
在哪里定义(在哪个类中)?选择的
喷丝头在哪个类别中定义?如果你把它变成静态的会发生什么?你真是个天才!使喷丝头处于选中状态并使变量处于静态状态就可以实现这一点。万分感谢!好。。。这不是一个完全正确的解决方案,但我很高兴你能让它起作用:)
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="cityboys.golfapp.Rastimar">

    <TextView
        android:id="@+id/rastimaLeit_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textStyle="bold"
        android:textColor="#2d7165"
        android:layout_centerHorizontal="true"
        android:text="Rástímaleit"
        android:paddingBottom="10dp"/>

    <Spinner
        android:id="@+id/dates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rastimaLeit_title"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentLeft="true"/>

    <Spinner
        android:id="@+id/spinner_start_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rastimaLeit_title"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true"/>

    <Spinner
        android:id="@+id/courses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dates"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentLeft="true"/>

    <Spinner
        android:id="@+id/spinner_end_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner_start_time"
        android:textColor="#2d7165"
        android:layout_marginRight="5dp"
        android:layout_alignParentRight="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lausir tímar"
        android:id="@+id/button"
        android:layout_below="@+id/spinner_end_time"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:onClick="findAvailableTime" />

</RelativeLayout>

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="#2d7165"
    android:dividerHeight="0.5dp"
    android:background="#2b574e" />