我如何查看“ Hello World!” Android Studio中的TextView?

时间:2018-07-13 12:50:18

标签: android android-studio

我刚刚开始学习Android上的开发,但无法查看“ Hellow World!”。设计标签中的TextView。

MainActivity.java:

package com.dummy.demoapp;

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);
    }
}

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dummy.demoapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

TextView出现在组件树中,但活动的预览完全空白:

enter image description here

我尝试放大,但是TextView不在那里。

[EDIT]:红色的惊叹号(!)给我以下消息:

enter image description here

刷新并不能解决问题。有帮助吗?

4 个答案:

答案 0 :(得分:0)

Android Studio有时直到您成功构建后才出现, Build-> Build&Run,然后如果可以的话,请选择您的虚拟设备,这不是您的代码问题,它与IDE有关。

答案 1 :(得分:0)

我认为这是您默认的AppTheme,颜色为白色,尝试为TextView添加颜色

import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.context.request.WebRequest;

@Controller
public class ErrorHandler implements ErrorController {

   private final ErrorAttributes errorAttributes;

   public ErrorHandler(ErrorAttributes errorAttributes) {
      this.errorAttributes = errorAttributes;
   }

   @GetMapping("/error")
   public String handleError(Model model, WebRequest webRequest) {
      model.addAttribute("midasLocation", "xxx");
      final Throwable error = errorAttributes.getError(webRequest);
      model.addAttribute("exception", error);
      model.addAttribute("message", error == null ? "" : error.getMessage());
      return "error";
   }

   @Override public String getErrorPath() {
      return "/error";
   }

   @GetMapping("/throwErrorForTest")
   public String throwError() {
      throw new RuntimeException("my exception");
   }
}

答案 2 :(得分:0)

转到style.xml并进行以下更改:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

答案 3 :(得分:0)

单击左上角的蓝色图标,然后单击强制布局。您将看到Hello World!