如何创建仅更改为一项活动的imageview?

时间:2019-05-19 17:50:21

标签: android android-fragments android-fragmentactivity

人民。我正在尝试基于这个想法进行一项活动来创建应用程序,当我单击该按钮时,就在该更改上有一个imageView。然后我做了一些测试。但是首先,我想测试一个片段进入活动状态。当我这样做时,出现一个错误。注意:我创建了XML元素,但是我只想测试加载到活动中的片段。这就是为什么我还没有将元素编程为活动的原因。

活动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>

主要活动:

    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.access$600(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.access$600(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.access$600(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.access$600(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)

1 个答案:

答案 0 :(得分:0)

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

尝试将支持库更新为27.1.0。或更高!