Android 应用在 Android Studio 模拟器中无法显示

4

我刚接触安卓开发。我尝试重建一个应用,当您按下按钮时,会显示文本“new text string”。然而,当我运行AVD并选择虚拟设备时,它没有显示。只有我之前测试的应用程序。有人能提供解决方法吗?非常感谢。

我的代码:
<?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

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


}

清单文件

<?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 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>

非常抱歉排版不好,我完全是新手。 - user3388470
既然问题不在你的代码中,你应该发布控制台输出。 - Yaroslav Mytkalyk
C:\Android\sdk\tools\emulator.exe -avd AVD_for_Nexus_S_by_Google -netspeed full -netdelay none 设备已连接:模拟器-5554 设备在线:模拟器-5554 目标设备:AVD_for_Nexus_S_by_Google 正在上传本地路径:C:\Androd\AndroidStudioProjects\SimpleActivityExample\SimpleActivityExample\build\apk\SimpleActivityExample-debug-unaligned.apk 远程路径:/data/local/tmp/com.example.simpleactivityexample 正在安装com.example.simpleactivityexample 设备shell命令:pm install -r“/data/local/tmp/com.example.simpleactivityexample” 错误:无法访问包管理器。系统是否在运行? - user3388470
3个回答

3

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


0

你的代码看起来没问题;当你点击“以Android应用程序运行”时,请检查logcat和控制台输出,看看安装是否正常。

Logcat位于顶部菜单下方

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

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


我使用Android Studio,但是找不到logcat。 - user3388470
@user3388470 当您尝试安装时,您的模拟器可能不在线;请等待模拟器完全启动,然后从菜单中选择模拟设备,然后安装(首先升级您的AS)。 - Mesher

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接