簡體   English   中英

在片段中添加ImageView時出現Android Studio錯誤

[英]Android Studio error when adding ImageView in fragment

1-我有一個片段,它有2個EditText和1個Button(代碼下面是)

<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個TextView

<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部分,我在主要活動布局中添加了這2個片段
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);
           }
             }

現在主要Activity的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);
                  }
                 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM