问题
I've had an app published and running on google play for several months.
Today I received an error from a user with a resource not found exception. I thought i may have forgotten some drawable. From the error i got the object ID (I took ID 0x7f030003
from the error) and looked for it in R.java to determine what object was missing.
To my surprise I found that the IDis from my Main Activity, so I'm sure I've not forgotten this!
My app starts with main and all other activities/fragments are called from it. When going back I just finish opened activities and go back to Main. So there is no explicit call to Main from my code so I'm lost about what cause this. I can not reproduce it and only have the report from the user.
Did i do some mistake looking for the resource? what may be happening? heres is the error that I receive through the console
java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.myapp/com.myapp.MainActivity}:
> android.content.res.Resources$NotFoundException: Resource ID
> #0x7f030003 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
> at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
> at android.app.ActivityThread.access$2300(ActivityThread.java:125) at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
> at android.os.Handler.dispatchMessage(Handler.java:99) at
> android.os.Looper.loop(Looper.java:123) at
> android.app.ActivityThread.main(ActivityThread.java:4627) at
> java.lang.reflect.Method.invokeNative(Native Method) at
> java.lang.reflect.Method.invoke(Method.java:521) at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at
> dalvik.system.NativeStart.main(Native Method) Caused by:
> android.content.res.Resources$NotFoundException: Resource ID
> #0x7f030003 at android.content.res.Resources.getValue(Resources.java:892) at
> android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
> at android.content.res.Resources.getLayout(Resources.java:731) at
> android.view.LayoutInflater.inflate(LayoutInflater.java:318) at
> android.view.LayoutInflater.inflate(LayoutInflater.java:276) at
> com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
> at android.app.Activity.setContentView(Activity.java:1647) at
> com.myapp.MainActivity.onCreate(MainActivity.java:48) at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
> ... 11 more
This is my main oncreate().
Basically I have a layout that i use as splash screen. The timer triggers another thread so i can load basic stuff while i'm updating a status bar in splash screen. once finished the splash is set to gone and the main menu is visible. If MyApp is destroyed by the OS my Boolean variable "inicializado" is null and then i've to reload my things.That's why i use it to show or not the splash
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
splashProgress = (ProgressBar) findViewById(R.id.mainSplash_pb_progressBar);
splashDescProgress = (TextView) findViewById(R.id.mainSplash_tv_descProgreso);
PACKAGE_NAME = MyApp.getContext().getPackageName();
media_btnClick = MediaPlayer.create(this, R.raw.btn_click);
if (savedInstanceState == null) {
TextView mi_textView = (TextView) findViewById(R.id.main_tv_Version);
PackageInfo mi_packageInfo = null;
try {
mi_packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
mi_textView.setText("v" + mi_packageInfo.versionName);
PACKAGE_CODE = mi_packageInfo.versionCode;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
}
savedInstanceStateNull = true;
} else {
savedInstanceStateNull = false;
}
if (!MyApp.Inicializado) {
cargarDatosSplash();
Timer timer = new Timer();
timer.schedule(new LanzarInicio(), 10);
} else {
LinearLayout layoutSplash = (LinearLayout) findViewById(R.id.main_splash);
LinearLayout layoutMain = (LinearLayout) findViewById(R.id.main_menu);
layoutSplash.setVisibility(View.GONE);
layoutMain.setVisibility(View.VISIBLE);
}
}
class LanzarInicio extends TimerTask {
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
new cargarObjetosTask().execute();
}
});
}
}
回答1:
If your activity is locked in rotation, add this to the manifest:
<activity
android:name=".MyActivity"
android:screenOrientation="landscape" (or "portrait")
android:configChanges="orientation"
/>
Same solution as the answer of this question.
Did i do some mistake looking for the resource? what may be happening?
I had your problem in another weird situation, in the same place setContentView(R.layout.activity_main);
My app is locked in landscape orientation. The first time OnCreate
is called everything is fine; the second time (it happen just after pressing the button to turn off the screen, for example), the device reads the orientation state, and seems that it looks for a resource in portrait mode that I don't have (because I don't need it). I came to this conclusion because other solution is to add the related layout to the "layout" (i was using only the "layout-land" directory) resources directory.
It could be a problem related to specific (and perhaps old) devices.
回答2:
Make sure that a layout is available for the device screen size you're testing. ie check if you've an splashscreen.xml layout at res/drawable-mdpi for small devices.
来源:https://stackoverflow.com/questions/18873441/unable-to-start-activity-resourcesnotfoundexception