当Android应用在模拟器上运行时,开关会消失

时间:2019-05-11 21:25:53

标签: java android button crash switch-statement

我可以在带有Android 9的Samsung Note 8上使用我的应用程序,但不能在Note 5和模拟器5.1 WVGA API 25 Android 7.1.1上使用我的应用程序

该应用在Android 9(Note8手机)上运行良好,但是在低于Android 9版本的应用上尤其是三星Note 5和Emulator Nexus Android 7.1.1崩溃。当我对Create的最后三个代码进行注释(或删除)时,我可以首先在Emulator上运行该应用程序(但在其他按钮上单击其他按钮后会崩溃),但是“ Switch”按钮不会显示在“ App Running”上。那么,是什么导致我的代码在Note 8上正常运行,但在Note 5上却没有正常运行?我使用了Toggle Switch而不是Switch,但是没有用。 (此名为Tap Count的应用程序已在Google Play上发布,用户警告我该应用程序不适用于旧版Android。)

Activity_main.xml的切换选项

 <Switch
    android:id="@+id/switch1"
    android:layout_width="85dp"
    android:layout_height="45dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:checked="false"
    android:text="text"
    android:textOff="(+1)"
    android:textOn="(-1)"
    android:textStyle="bold"
    android:theme="@style/SCBSwitch"
    android:onClick="perform_action5"
    app:layout_constraintBottom_toTopOf="@+id/button1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

MainActivity.Java的代码:

package com.obn.tapcount;

 import ...

 public class MainActivity extends AppCompatActivity {

int n=0;
Button button1;
Button button2;
Button button3;
Button button4;

TextView textView;
Boolean switchState;



private String text;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);

    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimaryDark)));
    //getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);



    Switch simpleSwitch = (Switch) findViewById(R.id.switch1);
    Boolean switchState = simpleSwitch.isChecked();
    simpleSwitch.setText("1");
    simpleSwitch.setTextColor(Color.GREEN);


}

按下Switch时,该代码正在从XML调用以运行以下代码:

    public void perform_action5(View v) {

    Switch simpleSwitch = (Switch) findViewById(R.id.switch1);
    Boolean switchState = simpleSwitch.isChecked();


    if (simpleSwitch.isChecked()) {
        simpleSwitch.setText("(-1)");
        simpleSwitch.setTextColor(Color.RED);

    } else {
        simpleSwitch.setText("(+1)");
        simpleSwitch.setTextColor(Color.GREEN);

    }
    MediaPlayer mp5 = MediaPlayer.create(MainActivity.this, R.raw.clicksound);
    mp5.start();
    {
        sleep(80);
        mp5.release();
    }

}

AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.obn.tapcount"
android:versionCode="7"
android:versionName="1.6"
>

<uses-permission android:name="android.permission.VIBRATE"/>


<application
    android:allowBackup="true"
    android:icon="@mipmap/app_icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:resizeableActivity="true"
        android:supportsPictureInPicture="true"
        android:windowSoftInputMode="stateHidden"

        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>


    </activity>

</application>

成绩页面是:

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.obn.tapcount"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 7
    versionName '1.6'
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
'proguard-rules.pro'
    }
}
productFlavors {
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testİmplementation 'junit:junit:4.12'
androidTestİmplementation 'com.android.support.test:runner:1.0.2'
androidTestİmplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

0 个答案:

没有答案