如何创建只有一个 activity 的 imageview?

How to create imageview that change into only one activity?

好人。我正在尝试创建一个应用程序,这个想法是有一个 activity 并且当我点击按钮时,在它的变化中有一个 imageView。然后我用片段进行了测试。但首先,我想测试 activity 中的一个片段。当我这样做时出现错误。注意:我创建了 XML 元素,但我只想测试加载到 activity 中的片段。这就是为什么我还没有将这些元素编程到 activity 中。

ACTIVITY XML:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


        <Button
            android:id="@+id/proximaId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:text="@string/botaoproxima"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.9"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="16dp"
            android:text="@string/textodia"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <RadioGroup
            android:id="@+id/radioGroupId"
            android:layout_width="0dp"
            android:layout_height="102dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2">

            <RadioButton
                android:id="@+id/botaosimId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/botaosim" />

            <RadioButton
                android:id="@+id/botaonaoId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/botaonao" />
        </RadioGroup>

        <Button
            android:id="@+id/sairId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="17dp"
            android:layout_marginRight="17dp"
            android:text="@string/botaosair"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/proximaId" />

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="379dp"
            android:layout_height="248dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintBottom_toTopOf="@+id/linearLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/sairId"
            app:layout_constraintVertical_bias="0.7" />

        <Button
            android:id="@+id/voltarId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:text="@string/botaovoltar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.9"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="64dp" />

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="395dp"
            android:layout_height="253dp"
            android:orientation="vertical"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="470dp">

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/ilustracaodias" />
        </LinearLayout>

    </android.support.constraint.ConstraintLayout>

片段XML:

     <FrameLayout 
        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"
        android:orientation="vertical"
        android:background="@android:color/holo_orange_light"
        tools:context=".Tela1">

        <ImageView
            android:id="@+id/tabelaumdiaId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/tabeladia1" />
    </FrameLayout>

主要ACTIVITY:

    public class MainActivity extends AppCompatActivity{


        @Override
        protected void onCreate(Bundle savedInstanceState) {`enter code here`
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            addFragment();

        }

        private void addFragment() {
            FragmentManager fragment = getSupportFragmentManager();
            FragmentTransaction transaction = fragment.beginTransaction();
            Tela1 tela1 = new Tela1();
            transaction.replace(R.id.container, tela1);
            transaction.commit();
        }
    }



    FRAGMENT 1:

        enter code here

    public class Tela1 extends Fragment {

    private ImageView img;

        public Tela1() {
            // Required empty public constructor
        }


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

          ImageView img = (ImageView) view.findViewById(R.id.tabelaumdiaId);

            return view;
        }

    }

错误:

    E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.niver/com.app.niver.MainActivity}: android.view.InflateException: Binary XML file line #43: Error inflating class android.support.v7.widget.ActionBarContextView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access0(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
         Caused by: android.view.InflateException: Binary XML file line #43: Error inflating class android.support.v7.widget.ActionBarContextView
            at android.view.LayoutInflater.createView(LayoutInflater.java:606)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:406)
            at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
            at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
            at com.app.niver.MainActivity.onCreate(MainActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
            at android.app.ActivityThread.access0(ActivityThread.java:123) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4424) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
            at dalvik.system.NativeStart.main(Native Method) 
         Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:586)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
            at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:406) 
            at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323) 
            at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
            at com.app.niver.MainActivity.onCreate(MainActivity.java:24) 
            at android.app.Activity.performCreate(Activity.java:4465) 
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
            at android.app.ActivityThread.access0(ActivityThread.java:123) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4424) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
            at dalvik.system.NativeStart.main(Native Method) 
         Caused by: java.lang.NoClassDefFoundError: android.support.v4.graphics.drawable.DrawableWrapper
            at android.support.v7.widget.DrawableUtils.canSafelyMutateDrawable(DrawableUtils.java:149)
            at android.support.v7.widget.AppCompatDrawableManager.tintDrawableUsingColorFilter(AppCompatDrawableManager.java:470)
            at android.support.v7.widget.AppCompatDrawableManager.tintDrawable(AppCompatDrawableManager.java:296)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:205)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
            at android.support.v7.widget.AppCompatDrawableManager.createDrawableIfNeeded(AppCompatDrawableManager.java:246)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
            at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
            at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:77)
            at android.support.v7.widget.ActionBarContextView.<init>(ActionBarContextView.java:69)
            at android.support.v7.widget.ActionBarContextView.<init>(ActionBarContextView.java:61)
            at java.lang.reflect.Constructor.constructNative(Native Method) 
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
            at android.view.LayoutInflater.createView(LayoutInflater.java:586) 
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
            at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:406) 
            at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323) 
            at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
            at com.app.niver.MainActivity.onCreate(MainActivity.java:24) 
            at android.app.Activity.performCreate(Activity.java:4465) 
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
            at android.app.ActivityThread.access0(ActivityThread.java:123) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4424) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
            at dalvik.system.NativeStart.main(Native Method)

我认为问题出在您的 dependency 版本:

尝试将您的支持库更新到 27.1.0。或更高!