Android 应用程序可在模拟器上运行,但不能在真实设备上运行 [英] Android app working on emulator but not on real device

查看:81
本文介绍了Android 应用程序可在模拟器上运行,但不能在真实设备上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚编写了这个用于测试的简单应用:一个按钮显示日期和小时,另一个按钮选择随机颜色并显示它.它在模拟器上运行良好,但当我尝试在真实设备上运行该应用程序时,按钮什么也不做(不工作).

Just wrote this simple app for testing: one button that displays date and hour, and another button that selects a random color and shows it. It works fine on Emulator but the buttons do nothing (don't work) when i try to run the app on a real device.

有人能帮我理解为什么吗?

Can someone help me understand why?

MainActivity.java:

MainActivity.java:

package yuvallevy.allyouneedapp;

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

import java.util.Date;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private Button btnShowTime;
    private Button btnRandomColor;
    private TextView timeText;
    private TextView randomColorView;

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

        btnRandomColor = (Button) findViewById(R.id.btnRandomColor);
        btnShowTime = (Button) findViewById(R.id.btnShowTime);
        timeText = (TextView) findViewById(R.id.timeText);
        randomColorView = (TextView) findViewById(R.id.randomColorView);

        btnShowTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String currentDataTimeString = java.text.DateFormat.getDateTimeInstance().format(new Date());
                timeText.setText(currentDataTimeString);
            }
        });

        btnRandomColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random rnd = new Random();
                int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
                randomColorView.setBackgroundColor(color);
            }
        });
    }
}

activity_main.xml:

activity_main.xml:

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


    <Button
        android:id="@+id/btnShowTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/randomColorView"
        android:layout_toStartOf="@+id/randomColorView"
        android:text="Show Time"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/timeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnRandomColor"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/btnRandomColor"
        android:layout_toRightOf="@+id/btnRandomColor" />

    <Button
        android:id="@+id/btnRandomColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/btnShowTime"
        android:text="Random Color"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/randomColorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btnRandomColor"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/btnRandomColor"
        android:layout_toRightOf="@+id/btnRandomColor" />


</RelativeLayout>

AndoirdManifest.xml:

AndoirdManifest.xml:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

推荐答案

我怀疑这个明显的问题与属性 android:supportsRtl="true" 和设备的不同 API 级别有关/模拟器.

I suspect that this apparent problem is related to the attribute android:supportsRtl="true" and the different API levels of your device/emualtor.

来自官方文档:

android:supportsRtl

声明您的应用程序是否愿意支持从右到左 (RTL) 布局.如果设置为 true 并且targetSdkVersion 设置为 17 或更高,各种 RTL API 将被由系统激活和使用,以便您的应用程序可以显示 RTL 布局.如果设置为 false 或 targetSdkVersion 设置为 16 或更低,则 RTLAPI 将被忽略或无效,您的应用程序将正常运行无论与用户相关联的布局方向如何,都相同区域设置选择(您的布局总是从左到右).

Declares whether your application is willing to support right-to-left (RTL) layouts. If set to true and targetSdkVersion is set to 17 or higher, various RTL APIs will be activated and used by the system so your app can display RTL layouts. If set to false or if targetSdkVersion is set to 16 or lower, the RTL APIs will be ignored or will have no effect and your app will behave the same regardless of the layout direction associated to the user's Locale choice (your layouts will always be left-to-right).

该属性的默认值为 false.

The default value of this attribute is false.

此属性是在 API 级别 17 中添加的.

This attribute was added in API level 17.

这可能会导致模拟器和您的设备之间的行为不同.

This may cause different behavior between the emualtor and your device.

您需要根据标志调整布局,或尝试移除此标志.

这篇关于Android 应用程序可在模拟器上运行,但不能在真实设备上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆