【问题标题】:Android hello world app, can't see hello world string on deviceAndroid hello world 应用,在设备上看不到 hello world 字符串
【发布时间】:2016-04-05 10:35:01
【问题描述】:

这是 hello world 项目。

content_main.xml =>

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.user.myapplication.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

MainActivity.java=>

package com.example.user.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

如果我运行该项目,我会看到黑屏设备。看不到“Hello World!”该设备上的字符串。为什么是这样?它是 Nexus 5 API 23 x86 。 截屏:

从底部的 AVD 窗口我看到:

模拟器:设备 fd:640

HAXM is not working and emulator runs in emulation mode

我该如何解决?

【问题讨论】:

  • 更改textColor 那有什么问题?
  • 布局文件中甚至没有工具栏或浮动操作按钮,这应该会崩溃。我不确定你在这里做什么。
  • @MD,如何更改 textColor?我必须写这个吗? holder.text.setTextColor(Color.RED);我会在哪里写这个?
  • 你能在模拟器中添加你的应用截图吗
  • 这不是文本颜色问题。你的模拟器坏了。见stackoverflow.com/questions/10022580/…

标签: java android


【解决方案1】:

您的问题分为两部分:

1.模拟器不工作:HAXM 不工作,模拟器运行 仿真模式

这里已经回答了这个问题:How to fix: “HAX is not working and emulator runs in emulation mode”。只需一次搜索即可。

2。 “Hello!World”未显示

那是因为你的模拟器根本没有运行。 (只要你的代码是理智的) 只需在您的物理设备上运行它(我希望您有一个),然后查看字符串是否显示。

只是想知道,这其中有什么复杂的东西让这个问题值得悬赏? :)

【讨论】:

    【解决方案2】:

    如果您在黑色背景上运行它,您可以将文本颜色更改为白色。

    <TextView
        android:textColor="@android:color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    

    【讨论】:

      【解决方案3】:
      android:textColor="#FFFFFF"
      

      在你的 textView 中添加这一行

      【讨论】:

      • 因为您的背景和文本视图的颜色都是黑色的。因此,如果您通过在 Textview 中添加这一行来将文本视图的颜色从黑色更改为白色。它可能会起作用
      【解决方案4】:

      您的代码正在对R.layout.activity_main 执行 setContentView(它似乎有工具栏和 fab 按钮之类的东西?!?),而您向我们展示的带有 hello world 文本的布局是 content_main.xml

      改变这个,看看会发生什么:

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

      【讨论】:

      • 这是Android Studio在创建“Blank Activity”时创建的代码
      【解决方案5】:

      您的 XML 布局的名称是什么。是 content_main.xml 还是 activity_main

      如果是content_main.xml,你应该这样做

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

      【讨论】:

        【解决方案6】:

        您正在使用content_main.xml 布局而不是activity_main.xml

        应该是:

         setContentView(R.layout.content_main);
        

        【讨论】:

          【解决方案7】:

          将此代码添加到您的 mainatcivity 中

          public class MainActivity   extends  Activity {
          
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
          
          }   }
          

          activity_main.xml 及以下

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="#fff"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           >
           <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#000"
              android:layout_centerInParent="true"
              android:text="Hello World!" />
          </RelativeLayout>
          

          【讨论】:

          • 可能与颜色无关。可能是模拟器不工作。
          【解决方案8】:

          你的模拟器不工作。

          1. 这可能会对您有所帮助。 HAXM is not working and emulator runs in emulation mode

          2. 另一方面,您应该知道这一点;

          重要:

          英特尔 HAXM不能在没有英特尔处理器的系统上使用,或者英特尔处理器缺乏“硬件要求”中描述的硬件功能 部分。要确定您的英特尔处理器的功能,请访问http://ark.intel.com/ 此外, 英特尔 HAXM 只能用于英特尔提供的 Android* x86 仿真器映像。

          英特尔 HAXM 不能与 ARM* Android* 模拟器映像或非英特尔 x86 Android* 模拟器映像一起使用。 Source

          1. 如果它对您没有帮助或您的硬件无法用于 HAXM,您可以尝试Genymotion

          * 我推荐第三个。它比默认的 RAM 使用效果更好

          【讨论】:

            【解决方案9】:

            您已经从 Android Studio 创建了“空白活动”,它会自动为您生成所有这些代码。这里的许多其他答案都是针对实现的(对 activity_main.xml 与 content_main.xml 或未使用的 fab 的混淆,等等)。尽管对新手来说有点混乱,但这段代码应该可以正常工作。如果您没有更改默认代码中的任何内容,那么也不太可能是颜色问题。

            看起来这与您的模拟器有关。尝试创建一个新的模拟器并运行它。它应该像设备一样运行,独立于您的代码。验证模拟器是否正常工作(或尝试其他模拟器,如 Genymotion)。当您知道自己有一个好的模拟器时,请尝试再次运行您的代码。 Android Studio 应该询问您在哪个设备(真实或模拟)上运行它。您还可以将设备插入计算机并在真机上运行代码以验证其是否有效。

            【讨论】:

            • 如何创建新的仿真?
            【解决方案10】:

            您使用错误的 xml 来设置活动 - MainActivity 而不是使用 - setContentView(R.layout.activity_main) write setContentView(R.layout.content_main) as content_main 有您的 textView 小部件,其中包含“Hello World”字符串。

            这样做后,您的活动可能会出现编译错误,因为 content_main 中没有 FAB 小部件...所以只需删除 fab 的代码和所有您不需要的代码,同时显示 hello world 示例!

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-12-14
              • 1970-01-01
              • 2020-05-02
              • 1970-01-01
              • 2012-05-10
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多