Фрагмент на андроид студии - PullRequest
0 голосов
/ 19 мая 2018

У меня есть два фрагмента, но второй фрагмент не может открыться при щелчке

это фрагмент препарата, второй фрагмент не может открыть из-за вращающегося элемента i

, размещенного на первом экране, и яНе знаю, как это исправить, пожалуйста, помогите

У меня есть два фрагмента, но второй фрагмент не может открыться при нажатии

это фрагмент препарата, второй фрагмент не может открыть из-за счетчика я

размещен на первом экране, и я не знаю, как это исправить, помогите

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="51dp"
        android:layout_marginStart="63dp"
        android:text="Save" />

    <Button
        android:id="@+id/buttonCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/btnSave"
        android:layout_marginEnd="76dp"
        android:text="Cancel" />

    <EditText
        android:id="@+id/textView4"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="221dp"
        android:layout_marginStart="36dp"
        android:hint="Drug" />

    <Spinner
        android:id="@+id/SpinnerTime"
        android:layout_width="133dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/btnSave"
        android:layout_marginBottom="135dp" />

    <Spinner
        android:id="@+id/spinnerFrequency"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="175dp"
        android:layout_marginStart="86dp" />

    <Spinner
        android:id="@+id/SpinnerQty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="227dp"
        android:layout_marginEnd="63dp" />

</RelativeLayout>

это код фрагмента встречи

       <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Appointment"
        android:textSize="20sp"
        android:textStyle="bold" />
    </LinearLayout>




import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener  {
    private TabLayout tabLayout;
    private AppBarLayout appBarLayout;
    private ViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        appBarLayout = (AppBarLayout)findViewById(R.id.bar);
        viewPager = (ViewPager)findViewById(R.id.viewpager);

        Spinner spin1 = (Spinner)findViewById(R.id.spinnerFrequency);
       Spinner  spin2 = (Spinner)findViewById(R.id.SpinnerTime);
        Spinner spin3 = (Spinner)findViewById(R.id.SpinnerQty);



        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.AddFragment(new Drugfragment(),"Drug");
        adapter.AddFragment(new Appointmentfragment(),"Appointment");
        ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.Qty,android.R.layout.simple_spinner_item);
        ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(this, R.array.time,android.R.layout.simple_spinner_item);
        ArrayAdapter<CharSequence> arrayAdapter3 = ArrayAdapter.createFromResource(this, R.array.frequency,android.R.layout.simple_spinner_item);

        spin1.setAdapter(arrayAdapter);
        spin2.setAdapter(arrayAdapter2);
        spin3.setAdapter(arrayAdapter3);

        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arrayAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spin1.setOnItemSelectedListener(this);
        spin2.setOnItemSelectedListener(this);
        spin3.setOnItemSelectedListener(this);



        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(),text,Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }



}

ошибка logcat, когда я не добавляювключает макет

05-18 10:26:34.316 5868-5868/? D/dalvikvm: Not late-enabling CheckJNI (already on)
05-18 10:26:34.326 5868-5874/? E/jdwp: Failed sending reply to debugger: Broken pipe
05-18 10:26:34.326 5868-5874/? D/dalvikvm: Debugger has detached; object registry had 1 entries
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.view.Window$Callback.onPointerCaptureChanged, referenced from method android.support.v7.view.WindowCallbackWrapper.onPointerCaptureChanged
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve interface method 20907: Landroid/view/Window$Callback;.onPointerCaptureChanged (Z)V
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve interface method 20909: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve interface method 20911: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve interface method 20915: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 866: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 888: Landroid/content/res/TypedArray;.getType (I)I
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.createDeviceProtectedStorageContext, referenced from method android.support.v4.content.ContextCompat.createDeviceProtectedStorageContext
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 644: Landroid/content/Context;.createDeviceProtectedStorageContext ()Landroid/content/Context;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getCodeCacheDir, referenced from method android.support.v4.content.ContextCompat.getCodeCacheDir
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 650: Landroid/content/Context;.getCodeCacheDir ()Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getColor, referenced from method android.support.v4.content.ContextCompat.getColor
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 651: Landroid/content/Context;.getColor (I)I
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v4.content.ContextCompat.getColorStateList
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 652: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getDataDir, referenced from method android.support.v4.content.ContextCompat.getDataDir
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 654: Landroid/content/Context;.getDataDir ()Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getDrawable, referenced from method android.support.v4.content.ContextCompat.getDrawable
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 655: Landroid/content/Context;.getDrawable (I)Landroid/graphics/drawable/Drawable;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getExternalCacheDirs, referenced from method android.support.v4.content.ContextCompat.getExternalCacheDirs
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 657: Landroid/content/Context;.getExternalCacheDirs ()[Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getExternalFilesDirs, referenced from method android.support.v4.content.ContextCompat.getExternalFilesDirs
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 659: Landroid/content/Context;.getExternalFilesDirs (Ljava/lang/String;)[Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getNoBackupFilesDir, referenced from method android.support.v4.content.ContextCompat.getNoBackupFilesDir
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 663: Landroid/content/Context;.getNoBackupFilesDir ()Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getObbDirs, referenced from method android.support.v4.content.ContextCompat.getObbDirs
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 665: Landroid/content/Context;.getObbDirs ()[Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.isDeviceProtectedStorage, referenced from method android.support.v4.content.ContextCompat.isDeviceProtectedStorage
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 676: Landroid/content/Context;.isDeviceProtectedStorage ()Z
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.startForegroundService, referenced from method android.support.v4.content.ContextCompat.startForegroundService
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 691: Landroid/content/Context;.startForegroundService (Landroid/content/Intent;)Landroid/content/ComponentName;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.support.design.widget.AppBarLayout.setKeyboardNavigationCluster, referenced from method android.support.design.widget.AppBarLayout.<init>
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 3160: Landroid/support/design/widget/AppBarLayout;.setKeyboardNavigationCluster (Z)V
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x006e
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.support.design.widget.AppBarLayout.setTouchscreenBlocksFocus, referenced from method android.support.design.widget.AppBarLayout.<init>
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 3163: Landroid/support/design/widget/AppBarLayout;.setTouchscreenBlocksFocus (Z)V
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x007f
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 829: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 831: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
05-18 10:26:34.346 5868-5868/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve instanceof 239 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve direct method 21530: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve direct method 21530: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
05-18 10:26:34.346 5868-5871/? D/dalvikvm: GC_CONCURRENT freed 168K, 25% free 2708K/3580K, paused 0ms+0ms, total 2ms
05-18 10:26:34.346 5868-5868/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 1ms
05-18 10:26:34.356 5868-5868/? D/dalvikvm: GC_FOR_ALLOC freed 100K, 26% free 2829K/3804K, paused 1ms, total 1ms
05-18 10:26:34.356 5868-5868/? I/dalvikvm-heap: Grow heap (frag case) to 4.521MB for 1127532-byte allocation
05-18 10:26:34.356 5868-5877/? D/dalvikvm: GC_FOR_ALLOC freed <1K, 20% free 3930K/4908K, paused 2ms, total 2ms
05-18 10:26:34.366 5868-5871/? D/dalvikvm: GC_CONCURRENT freed <1K, 20% free 3930K/4908K, paused 2ms+0ms, total 4ms
05-18 10:26:34.366 5868-5868/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 2ms
05-18 10:26:34.366 5868-5868/? I/dalvikvm-heap: Grow heap (frag case) to 6.940MB for 2536932-byte allocation
05-18 10:26:34.366 5868-5871/? D/dalvikvm: GC_CONCURRENT freed 0K, 14% free 6408K/7388K, paused 1ms+0ms, total 5ms
05-18 10:26:34.376 5868-5868/? D/AndroidRuntime: Shutting down VM
05-18 10:26:34.376 5868-5868/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4fa4678)
05-18 10:26:34.376 5868-5868/? E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.koichisato.medtime/com.example.koichisato.medtime.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at com.example.koichisato.medtime.MainActivity.onCreate(MainActivity.java:45)
        at android.app.Activity.performCreate(Activity.java:5133)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
        at android.app.ActivityThread.access$600(ActivityThread.java:141) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5103) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:525) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
        at dalvik.system.NativeStart.main(Native Method) 

Фрагмент препарата

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

public class Drugfragment extends Fragment {
    View view;

    public Drugfragment(){

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,  @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.drugfragment,container,false);
        return view;
    }
}

фрагмент назначения

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

public class Appointmentfragment extends Fragment {

    View view;
    public Appointmentfragment(){

    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.appointmentfragment,container,false);
            return view;
    }
}

Адаптер

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class Adapter extends FragmentPagerAdapter {


    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> fragmentListTitles = new ArrayList<>();

    public Adapter(FragmentManager fn){
        super(fn);
    }

    public Fragment getItem(int position) {

        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentListTitles.size();
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentListTitles.get(position);
    }

    public void AddFragment(Fragment fragment, String Title){
        fragmentList.add(fragment);
        fragmentListTitles.add(Title);

    }
}

Activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" android:orientation="vertical">

    <android.support.design.widget.AppBarLayout

        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:elevation="0dp"
        android:gravity="center">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"/>
    </android.support.design.widget.AppBarLayout>


    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#be0a16"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorTabindi"
        app:tabMode="fixed"
        app:tabTextColor="@color/colorText">

    </android.support.design.widget.TabLayout>


    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpager">
    </android.support.v4.view.ViewPager>



</LinearLayout>

Струны

<resources>
    <string name="app_name">MedTime</string>
    <string-array name="frequency">
        <item>x1</item>
        <item>x2</item>
        <item>x3</item>
        <item>x4</item>
        <item>x5</item>

    </string-array>
    <string-array name="time">
        <item>12:00AM</item>
        <item>11:00AM</item>
        <item>10:00AM</item>
        <item>09:00AM</item>
        <item>08:00AM</item>
        <item>07:00AM</item>
        <item>06:00AM</item>
        <item>05:00AM</item>
        <item>04:00AM</item>
        <item>03:00AM</item>
        <item>02:00AM</item>
        <item>01:00AM</item>
        <item>12:00PM</item>
        <item>11:00PM</item>
        <item>10:00PM</item>
        <item>09:00PM</item>
        <item>08:00PM</item>
        <item>07:00PM</item>
        <item>06:00PM</item>
        <item>05:00PM</item>
        <item>04:00PM</item>
        <item>03:00PM</item>
        <item>02:00PM</item>
        <item>01:00PM</item>
    </string-array>

    <string-array name="Qty">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
        <item>6</item>
        <item>7</item>
        <item>8</item>
    </string-array>
</resources>

1 Ответ

0 голосов
/ 19 мая 2018

Измените свой фрагмент препарата на

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class Drugfragment extends Fragment {
    View view;


    public Drugfragment() {


    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.drugfragment, container, false);

        final Spinner spin1 = view.findViewById(R.id.spinnerFrequency);
        final Spinner spin2 = view.findViewById(R.id.SpinnerTime);
        final Spinner spin3 = view.findViewById(R.id.SpinnerQty);
        Button save = view.findViewById(R.id.btnSave);

        ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(getContext(), R.array.Qty, android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(getContext(), R.array.time, android.R.layout.simple_spinner_item);
        ArrayAdapter<CharSequence> arrayAdapter3 = ArrayAdapter.createFromResource(getContext(), R.array.frequency, android.R.layout.simple_spinner_item);


        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arrayAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin1.setAdapter(arrayAdapter);
        spin2.setAdapter(arrayAdapter2);
        spin3.setAdapter(arrayAdapter3);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = spin1.getSelectedItem().toString();
                String text2 = spin2.getSelectedItem().toString();
                String text3 = spin3.getSelectedItem().toString();
                Toast.makeText(getContext(), "text1: " + text + "\ntext2: " + text2 + "\ntext3: " + text3, Toast.LENGTH_SHORT).show();
            }
        });


        return view;
    }

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

    }
}

, а свою основную деятельность на

import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private TabLayout tabLayout;
    private AppBarLayout appBarLayout;
    private ViewPager viewPager;
    Spinner spin1;
    Spinner spin2;
    Spinner spin3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = findViewById(R.id.tablayout);
        appBarLayout = findViewById(R.id.bar);
        viewPager = findViewById(R.id.viewpager);

        spin1 = findViewById(R.id.spinnerFrequency);
        spin2 = findViewById(R.id.SpinnerTime);
        spin3 = findViewById(R.id.SpinnerQty);


        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.AddFragment(new Drugfragment(), "Drug");
        adapter.AddFragment(new appointmentfragment(), "Appointment");
        ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.Qty, android.R.layout.simple_spinner_item);
        ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(this, R.array.time, android.R.layout.simple_spinner_item);
        ArrayAdapter<CharSequence> arrayAdapter3 = ArrayAdapter.createFromResource(this, R.array.frequency, android.R.layout.simple_spinner_item);


        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arrayAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);

    }

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


}

и сделайте то же самое для фрагмента назначения, то же самое

...