当屏幕关闭时,应用程序停止工作

时间:2014-07-14 06:08:10

标签: android android-resources

我已创建并在Landscape方向模式下运行的Android应用程序。它在活动时保持屏幕开启。但是,当我使用电源按钮锁定屏幕并且应用程序处于打开状态时,它会停止工作并在Resources$NotFoundException中显示LogCat,指向活动的onCreate()方法。请帮忙。

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());
}

更新

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

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

更新2

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>

注意:请注意,该应用在正常使用情况下运行正常。使用电源按钮关闭屏幕时会出现问题。

2 个答案:

答案 0 :(得分:1)

删除&#34; android:screenOrientation =&#34; sensorLandscape&#34;&#34;从您的清单中,并发布您的activity_menu.xml

答案 1 :(得分:0)

问题是我将布局xml文件保存在layout-land文件夹而不是layout文件夹中。当我锁定屏幕时,它在内部转为纵向模式(不知道为什么它总是保持在那个方向)并且因为它无法找到纵向模式的资源,它给出了Resources$NotFoundException