简体   繁体   English

屏幕关闭时,应用停止运行

[英]App stops working when Screen turns off

I have created and android app which works in Landscape orientation mode. 我创建了一个可在Landscape模式下运行的android应用。 It keeps the screen on when it is active. 当它处于活动状态时,它将使屏幕保持打开状态。 However, when I lock the screen using the power button and the app is on, it stops working and gives the Resources$NotFoundException in LogCat , pointing to the onCreate() method of the Activity. 但是,当我使用电源按钮锁定屏幕并打开应用程序时,它将停止工作并在LogCat提供Resources$NotFoundException ,指向Activity的onCreate()方法。 Please help. 请帮忙。

MenuActivity.java MenuActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_menu); // Error points here
    bjmc.activity = this;
    preference = getSharedPreferences("BJLocalPlayer", Context.MODE_PRIVATE);
    if (!preference.contains("balance") || (preference.getLong("balance", 0)) < 100) {
        Editor edit = preference.edit();
        edit.putLong("balance", 1000);
        edit.commit();
    }
    ((TextView) findViewById(R.id.player_balance)).setText(Long
            .toString(preference.getLong("balance", 0)));

    tvAmerican = new TextView(this);
    tvAmerican.setGravity(Gravity.CENTER);
    tvAmerican.setBackgroundColor(0x40000000);
    tvAmerican.setTextColor(getResources().getColor(R.color.orangish_yellow));
    tvAmerican.setText("American");
    tvEuropean = new TextView(this);
    tvEuropean.setGravity(Gravity.CENTER);
    tvEuropean.setText("European");

    final TabHost th = (TabHost) findViewById(R.id.table_tab_host);
    th.setup();
    th.addTab(th.newTabSpec("American").setIndicator(tvAmerican)
            .setContent(R.id.american));
    th.addTab(th.newTabSpec("European").setIndicator(tvEuropean)
            .setContent(R.id.european));
    th.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
            switch (tabId) {
            case "American":
                tvAmerican.setBackgroundColor(0x40000000);
                tvAmerican.setTextColor(getResources().getColor(R.color.orangish_yellow));
                tvEuropean.setBackgroundColor(0x00000000);
                tvEuropean.setTextColor(getResources().getColor(R.color.total_text_color));
                break;
            case "European":
                tvAmerican.setBackgroundColor(0x00000000);
                tvAmerican.setTextColor(getResources().getColor(R.color.total_text_color));
                tvEuropean.setBackgroundColor(0x40000000);
                tvEuropean.setTextColor(getResources().getColor(R.color.orangish_yellow));
                break;
            }
        }
    });
    final ViewPager american = (ViewPager) findViewById(R.id.american);
    american.setAdapter(new ImagePagerAdapter(getLayoutInflater(),
            new int[] { R.drawable.am_table0, R.drawable.am_table1,
                    R.drawable.am_table2, R.drawable.am_table3,
                    R.drawable.am_table4 }));
    final ViewPager european = (ViewPager) findViewById(R.id.european);
    european.setAdapter(new ImagePagerAdapter(getLayoutInflater(),
            new int[] { R.drawable.eu_table0, R.drawable.eu_table1,
                    R.drawable.eu_table2, R.drawable.eu_table3,
                    R.drawable.eu_table4 }));

    ((Button) findViewById(R.id.button_play_single_player)).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    tableIndex = (th.getCurrentTab() == 0 ? american.getCurrentItem()
                            : european.getCurrentItem() + 5);
                    bjmc.startGameForTableIndex(tableIndex,
                        BJMatchCoordinator.SINGLEPLAYER, new BJMCDelegate(MenuActivity.this));
                }
            });

    ((Button) findViewById(R.id.button_play_bluetooth)).setOnClickListener(new BluetoothListener());
}

UPDATE UPDATE

LogCat logcat的

07-14 11:50:18.129: W/ResourceType(4091): Failure getting entry for 0x7f030018 (t=2 e=24) in package 0 (error -75)
07-14 11:50:18.179: W/dalvikvm(4091): threadid=1: thread exiting with uncaught exception (group=0x40af39f0)
07-14 11:50:18.219: E/AndroidRuntime(4091): FATAL EXCEPTION: main
07-14 11:50:18.219: E/AndroidRuntime(4091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iapp.bjp/com.iapp.bjp.MenuActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030018
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.ActivityThread.access$700(ActivityThread.java:123)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.os.Looper.loop(Looper.java:137)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at java.lang.reflect.Method.invoke(Method.java:511)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at dalvik.system.NativeStart.main(Native Method)
07-14 11:50:18.219: E/AndroidRuntime(4091): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030018
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.content.res.Resources.getValue(Resources.java:1022)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2109)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.content.res.Resources.getLayout(Resources.java:861)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:268)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.Activity.setContentView(Activity.java:1837)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at com.iapp.bjp.MenuActivity.onCreate(MenuActivity.java:55)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.Activity.performCreate(Activity.java:4470)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-14 11:50:18.219: E/AndroidRuntime(4091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-14 11:50:18.219: E/AndroidRuntime(4091):     ... 12 more

AndroidManifest.xml AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iapp.bjp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.iapp.bjp.MenuActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:screenOrientation="sensorLandscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.iapp.bjp.SinglePlayerAddBetActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_single_player_add_bet"
            android:parentActivityName="com.iapp.bjp.MenuActivity"
            android:screenOrientation="sensorLandscape" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.iapp.bjp.MenuActivity" />
        </activity>
        <activity
            android:name="com.iapp.bjp.SinglePlayerGameActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_single_player_game"
            android:screenOrientation="sensorLandscape" >
        </activity>
        <activity
            android:name="com.iapp.bjp.bluetooth.ScanDeviceActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_scan_device" >
        </activity>
    </application>

</manifest>

UPDATE 2 更新2

activity_menu.xml activity_menu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/menu_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_menu"
    android:gravity="center"
    android:keepScreenOn="true"
    android:orientation="horizontal"
    android:baselineAligned="false"
    tools:context="com.iapp.bjp.MenuActivity" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/player_balance_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_balance_text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/player_balance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <TabHost
        android:id="@+id/table_tab_host"
        android:layout_width="0dip"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_weight="2" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                style="@android:style/Widget.Holo.TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <android.support.v4.view.ViewPager
                    android:id="@+id/american"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </android.support.v4.view.ViewPager>

                <android.support.v4.view.ViewPager
                    android:id="@+id/european"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </android.support.v4.view.ViewPager>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button_play_single_player"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/home_btn_ph"
            android:text="@string/single_play_button_text" />

        <Button
            android:id="@+id/button_play_bluetooth"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/home_btn_ph"
            android:text="@string/bluetooth_button_text" />
    </LinearLayout>

</LinearLayout>

Note: Please keep in mind that the app works fine on normal usage. 注意:请记住,该应用在正常使用情况下可以正常运行。 The problem occurs when I turn the screen off using the power button. 使用电源按钮关闭屏幕时会出现问题。

从清单中删除“ android:screenOrientation =“ sensorLandscape”“,然后发布您的activity_menu.xml

The problem was that I had kept the layout xml files in the layout-land folder instead of the layout folder. 问题是我将布局xml文件保留在layout-land文件夹中,而不是在layout文件夹中。 When I locked the screen, it turned to portrait mode internally (don't know why it always stays in that orientation though) and as it could not find resources for portrait mode, it gave the Resources$NotFoundException 当我锁定屏幕时,它在内部变成了纵向模式(虽然不知道为什么它总是保持在该方向),并且由于找不到纵向模式的Resources$NotFoundException ,所以它给出了Resources$NotFoundException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM