Android Studio 在片段中添加 ImageView 时出错



1- 我有一个片段,它有 2 个编辑文本和 1 个按钮(代码下方是(

<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:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.junior_yao.fragmentfrombeginning.Top_Fragment"
  tools:layout_editor_absoluteY="81dp"
  tools:layout_editor_absoluteX="0dp">
  <EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="information"
    android:inputType="text|textShortMessage"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="16dp"
    app:layout_constraintHorizontal_bias="0.503" />
  <EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="information"
    android:inputType="textPersonName"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="@+id/editText1"
    android:layout_marginLeft="0dp"
    app:layout_constraintLeft_toLeftOf="@+id/editText1"
    android:layout_marginTop="23dp"
    app:layout_constraintTop_toBottomOf="@+id/editText1"
    app:layout_constraintHorizontal_bias="0.0" />
  <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send Info"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="@+id/editText1"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="@+id/editText1"
    android:layout_marginTop="32dp"
    app:layout_constraintTop_toBottomOf="@+id/editText2"
    app:layout_constraintHorizontal_bias="0.495" />
</android.support.constraint.ConstraintLayout>
2

-我还有另一个片段,其中包含一个图像视图和2个文本视图

<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:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.junior_yao.fragmentfrombeginning.Down_Fragment"
>
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/androidtest"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp"
    android:id="@+id/imageView" />
  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text2"
    android:textSize="25dp"
    android:textColor="#7FFF00"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="30dp"
    app:layout_constraintLeft_toLeftOf="parent" />
  <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text1"
    android:textSize="25dp"
    android:textColor="#7FFF00"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="44dp" />
</android.support.constraint.ConstraintLayout>

在第 3 部分,我在主活动布局
中添加了这两个片段3 - 如果我只添加第一个片段(我没有错误(

4 - 这是问题

如果我添加第二个片段,应用程序无法启动,请帮助我弄清楚

      <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout 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="com.example.junior_yao.fragmentfrombeginning.MainActivity">
     <fragment
    android:id="@+id/fragment"
       android:name="com.example.junior_yao.fragmentfrombeginning.Top_Fragment"
      android:layout_width="match_parent"
       android:layout_height="250dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    />
     <fragment
    android:id="@+id/fragment12"
      android:name="com.example.junior_yao.fragmentfrombeginning.Down_Fragment"
       android:layout_width="match_parent"
      android:layout_height="250dp"
      android:layout_alignParentBottom="true"
      android:layout_alignParentStart="true"
      />

     </RelativeLayout>

您需要添加更多信息才能得到帮助,调用和创建片段的 java 代码以及帮助解决问题的 logcat,XML 设计代码没有帮助。

这里是第一个片段的java代码

       package com.example.junior_yao.fragmentfrombeginning;

       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 Top_Fragment extends Fragment {

         public Top_Fragment() {
        }

         @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup   container,
                         Bundle savedInstanceState) {
         return inflater.inflate(R.layout.fragment_top, container, false);
}
         @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
            }
         }

第二个片段的 java 代码

         package com.example.junior_yao.fragmentfrombeginning;

          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 Down_Fragment extends Fragment {

           public Down_Fragment() {
                    }

               @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
                  // Inflate the layout for this fragment
                 return inflater.inflate(R.layout.fragment_down, container, false);
}
               @Override
                     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
           }
             }

现在主活动的java代码

        package com.example.junior_yao.fragmentfrombeginning;
        import android.support.v7.app.AppCompatActivity;
         import android.os.Bundle;
         public class MainActivity extends AppCompatActivity {
           @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
                  }
                 }

最新更新