【发布时间】:2015-04-22 10:11:42
【问题描述】:
我使用 AVD 创建了一个 7 英寸、屏幕分辨率为 2880x1800 的虚拟设备。我的目标是获得高分辨率的 7 英寸屏幕截图。但是我的应用程序无法在此设备上加载。日志说找不到setContentView(R.layout.my_activity_layout) 的资源。怎么是真的?它适用于从普通到 xlarge 的所有其他设备。有任何想法吗?
这是我的堆栈跟踪。
04-21 21:31:52.513 2380-2380/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.nsouthproductions.mathanimalsaddition, PID: 2380
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nsouthproductions.mathanimalsaddition/com.nsouthproductions.mathanimalsaddition.Main}: android.content.res.Resources$NotFoundException: Resource ID #0x7f040008
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f040008
at android.content.res.Resources.getValue(Resources.java:1233)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2598)
at android.content.res.Resources.getLayout(Resources.java:1049)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:870)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2144)
at com.nsouthproductions.mathanimalsaddition.Main.onCreate(Main.java:333)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-21 21:31:52.740 933-933/? E/EGL_emulation﹕ tid 933: eglCreateSyncKHR(1237): error 0x3004 (EGL_BAD_ATTRIBUTE)
编辑:添加 Java 和 XML,尽管它们非常标准并且被证明可以在其他设备上工作。
Java:(错误发生在setContentView())
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Ensures they resume where left off if hit launcher and game already playing
// If the launcher is trying to create a new instance of Main, this checks to see
// if this is a new instance of the app. If not, close this activity so that the
// one below it (where they left off) is at the top.
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
Intent intent = getIntent();
if(savedInstanceState != null){
START_IN_PLAY = savedInstanceState.getBoolean(KEY_SHOWING_PLAY, false);
START_IN_PRACTICE = savedInstanceState.getBoolean(KEY_SHOWING_PRACTICE, false);
START_IN_QUIZ = savedInstanceState.getBoolean(KEY_SHOWING_QUIZ, false);
}
context = this;
mDecorView = getWindow().getDecorView();
// call before setContentView to avoid system bars briefly showing
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
System.out.println("checking os version");
if (currentapiVersion >= Build.VERSION_CODES.KITKAT) {
System.out.println("KitKat or newer");
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
);
} else{
System.out.println("Older than KitKat");
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
main_layout 为land 资源
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" tools:context=".Main"
android:id="@+id/area_main_layout">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="@+id/imageView6"
android:src="@drawable/title_math_animals"
android:cropToPadding="false"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:id="@+id/imageView7"
android:src="@drawable/title_addition"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_marginBottom="15dp"
android:layout_weight="3"
android:id="@+id/linlay_main_2">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_play"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_play" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img_play_text"
android:src="@drawable/text_play"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_practice"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_menu_other" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@+id/img_practice_text"
android:src="@drawable/text_practice"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_quiz"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_menu_other" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@+id/img_quiz_text"
android:src="@drawable/text_quiz"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_animals"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_menu_other" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@+id/img_animals_text"
android:src="@drawable/text_animals"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
</LinearLayout>
main_layout 为land 资源
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" tools:context=".Main"
android:id="@+id/area_main_layout">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView6"
android:src="@drawable/title_math_animals"
android:cropToPadding="false"
android:layout_gravity="center_horizontal"
android:layout_marginTop="7dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="45dp"
android:id="@+id/imageView7"
android:src="@drawable/title_addition"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_marginBottom="10dp"
android:layout_weight="3"
android:id="@+id/linlay_main_2">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_play"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_play" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img_play_text"
android:src="@drawable/text_play"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_practice"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_menu_other" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@+id/img_practice_text"
android:src="@drawable/text_practice"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_quiz"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_menu_other" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@+id/img_quiz_text"
android:src="@drawable/text_quiz"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_animals"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_menu_other" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@+id/img_animals_text"
android:src="@drawable/text_animals"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
</LinearLayout>
main_layout 用于 large_land 资源
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" tools:context=".Main"
android:id="@+id/area_main_layout">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="3"
android:layout_marginTop="15dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/imageView6"
android:src="@drawable/title_math_animals"
android:cropToPadding="false"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:id="@+id/imageView7"
android:src="@drawable/title_addition"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_marginBottom="15dp"
android:layout_weight="6"
android:id="@+id/linlay_main_2"
android:layout_marginTop="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_play"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_play" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img_play_text"
android:src="@drawable/text_play"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_practice"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_menu_other" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@+id/img_practice_text"
android:src="@drawable/text_practice"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_quiz"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_menu_other" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@+id/img_quiz_text"
android:src="@drawable/text_quiz"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:id="@+id/btn_animals"
android:layout_centerInParent="true"
android:soundEffectsEnabled="false"
android:background="@drawable/selector_btn_menu_other" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@+id/img_animals_text"
android:src="@drawable/text_animals"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
</LinearLayout>
如果不超过字符数限制,我无法为xlarge-land 发帖。我已经看到所有这些资源都在其他设备上成功加载。
【问题讨论】:
-
发布您的 xml 和 java 代码。
-
根
res/values目录下有my_activity_layout.xml吗 -
@kcoppock,不,我没有。我在
res/layout(及其变体)中有它。 -
@dora,我已经更新了我的 xml 和 java 代码,但我知道我的资源可以在其他设备上运行,即使找不到所需的资源,我也希望 Android 选择下一个可用的最佳资源。
-
对不起,我的意思是
res/layout:) 所以你有默认的吗?不是其中一种变体——只是res/layout/my_activity_layout.xml?my_activity_layout是否包含另一个在res/layout中不存在的布局?
标签: android android-resources android-screen-support avd