Java Android 应用程序不会显示在 Android Studio 模拟器中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22226757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:27:11  来源:igfitidea点击:

Android App won't display in Android Studio Emulator

javaandroidandroid-intent

提问by user3388470

I am very new to Android development. I have tried to recreate an app where if you press a button, a message "new text string" is displayed. However, when I run the AVD and choose the Virtual Device, it doesn't display it. Just an old TestApp I had. Can anyone suggest a fix? I'd really appreciate it. My code:

我对Android开发很陌生。我试图重新创建一个应用程序,如果您按下按钮,则会显示一条消息“新文本字符串”。但是,当我运行 AVD 并选择虚拟设备时,它不会显示它。只是我拥有的一个旧的 TestApp。任何人都可以建议修复吗?我真的很感激。我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=      "http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:id="@+id/hello_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />


<Button android:id="@+id/trigger"
    android:layout_width="100dip" android:layout_height="100dip"
    android:text="@string/button1"
    android:layout_below="@+id/hello_text"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="111dp" />
 </RelativeLayout>

SimpleActivity.java

简单活动.java

package com.example.simpleactivityexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import android.widget.*;
import android.view.View;
public class SimpleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
    Button startButton = (Button) findViewById(R.id.trigger);
    startButton.setOnClickListener(new View.OnClickListener()
    {
        private TextView tv = (TextView) findViewById(R.id.hello_text);
        public void onClick(View view)
        {
            tv.setText("new text string");
        }
    }
    );

}
@Override
protected void onStart() {
    super.onStart();
    Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
    super.onResume();
    Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
    super.onRestart();
    Toast.makeText(this, "onRestart", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
    Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
    super.onPause();
}
@Override
protected void onStop() {
    Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
    super.onStop();
}
@Override
protected void onDestroy() {
    Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
    super.onDestroy();
}


}

Manifest

显现

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

<application
    android:debuggable="true"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.simpleactivityexample.SimpleActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  </application>

 </manifest>

Strings.xml

字符串.xml

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
 <string name="app_name">Dietary Application</string>
 <string name="hello">Hello!</string>
 <string name="button1">This is a button!</string>
 </resources>

回答by Mesher

Your code seems fine; check the logcat and console outputs when you click 'Run as Android Application' to see if the install is working.

你的代码看起来不错;当您单击“作为 Android 应用程序运行”以查看安装是否正常时,请检查 logcat 和控制台输出。

Logcat is located in the top menu under

Logcat位于顶部菜单下

Window- Show View - Other - Android - Logcat

窗口-显示视图-其他-Android-Logcat

Also try cleaning the project and restarting the emulator; and also, if the app doesn't show up when the emulator loads, try, without exiting the emulator, to run it again.

还可以尝试清理项目并重新启动模拟器;此外,如果应用程序在模拟器加载时没有出现,请尝试在不退出模拟器的情况下再次运行它。

回答by Yaroslav Mytkalyk

Are you using the latest studio? Seems like an old bug

您使用的是最新的工作室吗?好像是个老bug

Error when running emulator in Android Studio

在 Android Studio 中运行模拟器时出错

Try waiting until the emulator boots, and then run the project.

尝试等到模拟器启动,然后运行项目。

回答by user3388470

I managed to solve it.I am using Android Studio by the way. I went to the run tab at the top of the screen and went into ''Edit Configurations''. I then ticked ''Show chooser dialog''. I started the AVD and when it was fully started, A pressed the run function(green triangle) and it started on the Android. Thanks so much to everyone who helped!

我设法解决了它。顺便说一下,我正在使用 Android Studio。我转到屏幕顶部的运行选项卡并进入“编辑配置”。然后我勾选了“显示选择器对话框”。我启动了 AVD,当它完全启动时,A 按下运行功能(绿色三角形),它在 Android 上启动。非常感谢所有帮助过的人!