Android application runs on an emulator, but not on a real device

I just wrote this simple application for testing: one button that displays the date and time, and another button that selects a random color and displays it. It works fine on the emulator, but the buttons do nothing (don't work) when I try to run the application 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>
+4
source share
3 answers

I suspect this obvious problem is with the attribute android:supportsRtl="true"and the various API levels of your device / emualtor.

:

Android: supportsRtl

, Right-to-left (RTL). true targetSdkVersion 17 , API RTL , RTL. false targetSdkVersion 16 , RTL API , , ( ).

- false.

API 17.

emualtor .

.

+5

, - , , .

, , RelativeLayout LinearLayout, (, , LinearLayouts IDE )

+1

If Instant Run is activated, this will cause some classes to move. Disabling Instant Run Go to File → Settings → Build, Run, Deploy → Instant Launch → uncheck the box “Enable Instant Launch”

0
source

Source: https://habr.com/ru/post/1612508/


All Articles