【问题标题】:Android Studio error when adding ImageView in fragment在片段中添加 ImageView 时 Android Studio 错误
【发布时间】:2017-09-12 14:13:56
【问题描述】:

1- 我有一个片段,它有 2 个 EditText 和 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- 我有另一个片段,它有一个 Image View 和 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>

在第三部分,我在主要活动布局中添加了这 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 android xml android-layout android-fragments


    【解决方案1】:

    这里是第一个片段的 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);
                      }
                     }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-09
        • 2015-10-05
        • 1970-01-01
        • 2018-04-11
        相关资源
        最近更新 更多