Android应用模拟器上而不是真实的设备工作 [英] Android app working on emulator but not on real device

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

问题描述

就写了这个简单的应用程序来进行测试:显示日期和时间,另一个按钮选择一个随机的颜色并显示一个按钮。
它工作正常,在模拟器但按键无能为力(不工作),当我尝试在真实设备上运行应用程序。

有人可以帮助我了解为什么?

MainActivity.java:

 包yuvallevy.allyouneedapp;进口android.graphics.Color;
进口android.os.Bundle;
进口android.support.v7.app.AppCompatActivity;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;进口java.util.Date;
进口了java.util.Random;公共类MainActivity扩展AppCompatActivity {    私人按钮btnShowTime;
    私人按钮btnRandomColor;
    私人TextView的timeText;
    私人TextView的randomColorView;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        btnRandomColor =(按钮)findViewById(R.id.btnRandomColor);
        btnShowTime =(按钮)findViewById(R.id.btnShowTime);
        timeText =(的TextView)findViewById(R.id.timeText);
        randomColorView =(的TextView)findViewById(R.id.randomColorView);        btnShowTime.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                字符串currentDataTimeString = java.text.DateFormat.getDateTimeInstance()格式(新的Date());
                timeText.setText(currentDataTimeString);
            }
        });        btnRandomColor.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                随机RND =新的随机();
                INT颜色= Color.argb(255,rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256));
                randomColorView.setBackgroundColor(颜色);
            }
        });
    }
}

activity_main.xml中:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>
    <按钮
        机器人:ID =@ + ID / btnShowTime
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_toLeftOf =@ + ID / randomColorView
        机器人:layout_toStartOf =@ + ID / randomColorView
        机器人:文本=显示时间
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?    <的TextView
        机器人:ID =@ + ID / timeText
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / btnRandomColor
        机器人:layout_alignParentEnd =真
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentTop =真
        机器人:layout_toEndOf =@ + ID / btnRandomColor
        机器人:layout_toRightOf =@ + ID / btnRandomColor/>    <按钮
        机器人:ID =@ + ID / btnRandomColor
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ ID / btnShowTime
        机器人:文字=颜色随机
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?    <的TextView
        机器人:ID =@ + ID / randomColorView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBottom =@ + ID / btnRandomColor
        机器人:layout_alignParentEnd =真
        机器人:layout_alignParentRight =真
        机器人:layout_alignTop =@ + ID / btnRandomColor
        机器人:layout_toRightOf =@ + ID / btnRandomColor/>
< / RelativeLayout的>

AndoirdManifest.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=yuvallevy.allyouneedapp>    <应用
        机器人:allowBackup =真
        机器人:图标=@的mipmap / ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:supportsRtl =真
        机器人:主题=@风格/ AppTheme>
        <活动机器人:名字=。MainActivity>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>< /清单>


解决方案

我怀疑这个明显的问题是关系到属性的android:supportsRtl =真正的和设备/ emualtor不同的API等级。

从href=\"http://developer.android.com/intl/es/guide/topics/manifest/application-element.html\" rel=\"nofollow\">官方文档的


  

的android:supportsRtl


  
  

声明你的应用是否愿意
  支持从右到左(RTL)布局。 如果设置为true,
  targetSdkVersion设置为17
或更高,各种RTL的API会
  激活,由系统使用,因此您的应用程序可以显示RTL布局。
  如果设置为false,或者如果targetSdkVersion设置为16或更低的RTL
  的API会被忽略或不会有任何效果,你的应用程序的行为
  无论相同关联到用户的布局方向的
  区域的选择(你的布局总是会左到右)。


  
  

此属性的默认值是假的。


  
  

这个属性在API级别17加入。


这可能会导致emualtor和设备之间不同的行为。

您需要相应解决您的布局,以国旗,或尝试删除该标志

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:

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:

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

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

解决方案

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

From the official doc:

android:supportsRtl

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).

The default value of this attribute is false.

This attribute was added in API level 17.

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

You need to fix your layout accordingly to the flag, or try to remove this flag.

这篇关于Android应用模拟器上而不是真实的设备工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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