【问题标题】:Why are XML Elements not visible on my emulator?为什么我的模拟器上看不到 XML 元素?
【发布时间】:2019-05-23 03:40:40
【问题描述】:

我创建了一个简单的 Android 工作室应用来练习和了解更多信息。但是 XML 中的元素在模拟器中是不可见的。我该如何解决这个问题?

我研究了但我没有找到答案。

XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:padding="16dp">

<TextView
    android:id="@+id/text_welcome"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/text_welcome"
    android:textSize="26sp"/>

<TextView
    android:id="@+id/text_instructions"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="@string/text_instruction"
    android:textSize="20sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:gravity="center">

    <EditText
        android:id="@+id/edit_feet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="4"
        android:hint="@string/edit_feet"
        android:inputType="number" />

    <EditText
        android:id="@+id/edit_inchee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:ems="4"
        android:hint="@string/edit_inches"
        android:inputType="number" />

    <EditText
        android:id="@+id/edit_pounds"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:ems="4"
        android:hint="@string/edit_pounds"
        android:inputType="number" />

</LinearLayout>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/boutton_calculate_bmi"
    android:text="@string/boutton_cal_bmi"/>
<TextView
    android:id="@+id/text_result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="@string/text_result"
    android:textSize="18sp" />

</LinearLayout>

活动代码:

package com.applike.bmi;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;


public class MainActivity extends AppCompatActivity {

   TextView text_result;
   EditText edit_feet , edit_inches , edit_pounds;
   Button button_calculate_bmi;



   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       /*
       edit_feet = (EditText)findViewById(R.id.edit_feet);
       edit_inches = (EditText)findViewById(R.id.edit_inchee);
       edit_pounds = (EditText)findViewById(R.id.edit_pounds);
       button_calculate_bmi = (Button)findViewById(R.id.boutton_calculate_bmi);
       text_result = (TextView)findViewById(R.id.text_result);


       button_calculate_bmi.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               String results="Results: ";
              // int feet = Integer.parseInt(edit_feet.getText().toString());
               if(edit_feet.getText().length() == 0){
                   edit_feet.setError("Please enter a #.");
                   results += "Oops! There was an error. ";
               }
               else if(edit_feet.getText().length() > 1){
                   edit_feet.setError("Too many digits");
                   results += "Our records indicate that your cannot possibily" +
                           " be thay tall. ";
               }
               else {
                   results += "The length of feet is " +
                           edit_feet.getText().length();
               }
               text_result.setText(results);
           }
       });

*/
   }
}

我的 XML 文件有问题吗? 请为此提供解决方案。我该如何处理?

【问题讨论】:

  • 请检查图片
  • 您能否也发布您的MainActivity.java 代码,特别是使布局膨胀的部分?
  • 尝试使用其他模拟器或设备手机也发MainActivity.java
  • 我试过了,但是不行
  • @Omid 以下任何答案对您有帮助吗?

标签: android xml android-layout android-emulator


【解决方案1】:

您没有在 Activity 中夸大您的布局,因此它没有被显示。您需要使用 onCreate() 中的 setContentView() 函数来扩展您的布局,如下所示:

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

    ...
}

【讨论】:

    【解决方案2】:

    您需要在 onCreate() 方法中设置布局,如下所示:

    setContentView(R.layout.activity_main_new);
    

    【讨论】:

      【解决方案3】:

      你没有在你的 Activity 中膨胀你的布局。你需要使用 onCreate() 中的 setContentView() 方法来膨胀你的布局,这样它就会在设备上可见:

      setContentView(R.layout.activity_main_new); //activity_main_new defines your xml layout.
      

      如果你想在 Fragment 中扩展你的布局。你需要在 Fragment 的 onCreateView() 方法中定义你的布局:

      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.homecredit, container, false); //where home credit is your layout file, container is root layout and attachToRoot is a boolean value
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-09
        • 1970-01-01
        • 2021-12-27
        • 2017-04-01
        • 2020-11-12
        • 1970-01-01
        相关资源
        最近更新 更多