androidxmlandroid-inflateandroid-runtime

inflate a view without XML in Android


first of all, please any one can tell me is it possible to inflate a view without XML ?

Description is like this:
I have dynamically generated a view hierarchy by coding, like

Relative layout contain 4 Linear layouts
-1st Linear Layout contain Image View
-2nd Linear Layout contain surface view
-3rd Linear Layout contain Button
-4th Linear Layout contain Text Box

and there is no issue in generating all view / view hierarchy (I tested by debugging and inspecting child views)

But I am not able to set it as a setContent()
Because I don't have XML and not allowed to use XML(not sure why), i.e. I have no access of R.Layout. So I cant use inflater.Inflate(R.Layout...)

When I am setting main relative layout as a content, I am getting following error

12-16 11:32:03.653: E/AndroidRuntime(22564): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package.class}: android.content.res.Resources$NotFoundException: Resource ID #0x1d97b

Anyone have any idea ?

----Updated Log----
12-16 12:33:16.143: E/AndroidRuntime(25358): FATAL EXCEPTION: main 12-16 12:33:16.143: E/AndroidRuntime(25358): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package.class}: android.content.res.Resources$NotFoundException: Resource ID #0x1d97b 12-16 12:33:16.143: E/AndroidRuntime(25358): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343) 12-16 12:33:16.143: E/AndroidRuntime(25358): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395) 12-16 12:33:16.143: E/AndroidRuntime(25358): at android.app.ActivityThread.access$600(ActivityThread.java:162) 12-16 12:33:16.143: E/AndroidRuntime(25358): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 12-16 12:33:16.143: E/AndroidRuntime(25358): at android.os.Handler.dispatchMessage(Handler.java:107) 12-16 12:33:16.143: E/AndroidRuntime(25358): at android.os.Looper.loop(Looper.java:194) 12-16 12:33:16.143: E/AndroidRuntime(25358): at android.app.ActivityThread.main(ActivityThread.java:5371) 12-16 12:33:16.143: E/AndroidRuntime(25358): at java.lang.reflect.Method.invokeNative(Native Method) 12-16 12:33:16.143: E/AndroidRuntime(25358): at java.lang.reflect.Method.invoke(Method.java:525) 12-16 12:33:16.143: E/AndroidRuntime(25358): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 12-16 12:33:16.143: E/AndroidRuntime(25358): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 12-16 12:33:16.143: E/AndroidRuntime(25358): at dalvik.system.NativeStart.main(Native Method) 12-16 12:33:16.143: E/AndroidRuntime(25358): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x1d97b
hope it might help you to understand a bit more

---Dropbox link of my activity class which generate views---
https://www.dropbox.com/s/vclymo0cs0zp360/ScannerActivity.java?dl=0


Solution

  • is it possible to inflate a view without XML ?

    No. The definition of the term "inflation" in Android is to convert an XML resource file into a corresponding tree of Java objects. Layout inflation converts an XML layout resource into a corresponding tree of View objects.

    But I am not able to set it as a setContent()

    I do not know what setContent() method it is that you are referring to. To put a View as the main content of an Activity, use setContentView().