Android: unable to load XML - android

New Android Studio Project
Created Assets folder
Create constructor of new activity (second in the project) to read XML when the activity is called.
put the xml file I want read in the assets folder i created
Here is the code to read the xml
AssetManager assetManager = getAssets();
InputStream xmlFile= assetManager.open("menu.xml");
// CREATE BUILDER FACTORY/BUILDER
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
//Element root = doc.getDocumentElement();
//Log.d("root element",root.getNodeName());
NodeList list = doc.getElementsByTagName("subcategory");
Log.d("NODE", String.valueOf(list.getLength()));
When running the app, the button is called to bring up the new activity, but crashes...
Please help
Here is logcat output
07-28 15:52:30.138 6990-6990/com.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app/com.app.MainMenu}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getAssets(ContextWrapper.java:74)
at com.app.MainMenu.<init>(MainMenu.java:60)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1409)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3691)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
            at dalvik.system.NativeStart.main(Native Method)
MainACtivity is a default android studio activity with a button to call the new activity
MainMenu is the second activity that calls the XML
please advise.
thank you!

I think you need to call getAssets() for whatever context your activity is in,
context.getAssets();

Related

How to skip splash screen Activity on low memory Android devices?

In an app with minSdkLevel=9 a splash screen is displayed by the following code:
public class SplashActivity extends Activity {
private static final int DELAY = 3000;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_splash);
} catch (OutOfMemoryError e) {
// ignore
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final Intent intent = new Intent(
SplashActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
}
}, DELAY);
Utils.init(this);
}
#Override
public void onBackPressed() {
// protect from exiting the application when splash screen is shown
}
}
The activity_splash.xml layout file consists mainly of an ImageView:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:contentDescription="#string/app_name"
android:scaleType="centerInside"
android:src="#drawable/splash" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:text="#string/copyright" />
</FrameLayout>
Unfortunately, on Nexus 1 the app crashes with out of memory error:
There are 2 variants of splash.png - for mdpi (280 KByte) and xxhdpi:
Surely the splash image could be scaled down more and more variants of it could be provided (ldpi, xhdpi, ...) - but why does not the try/catch in above Java code help here?
And is there maybe a better way to handle this - maybe some directive for AndroidManifest.xml to skip SplashActivity and load MainActivity on low-memory devices?
Here the full crash trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{XXX/XXX.SplashActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
at android.app.Activity.setContentView(Activity.java:1657)
at XXX.SplashActivity.onCreate(SplashActivity.java:16)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
            at android.app.Activity.setContentView(Activity.java:1657)
            at XXX.SplashActivity.onCreate(SplashActivity.java:16)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
at android.content.res.Resources.loadDrawable(Resources.java:1709)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.widget.ImageView.<init>(ImageView.java:118)
at android.widget.ImageView.<init>(ImageView.java:108)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
            at android.view.LayoutInflater.createView(LayoutInflater.java:505)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
            at android.app.Activity.setContentView(Activity.java:1657)
            at XXX.SplashActivity.onCreate(SplashActivity.java:16)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
This is the relavant bit of the stack that tells me it's an InflateException:
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
at android.app.Activity.setContentView(Activity.java:1657)
at XXX.SplashActivity.onCreate(SplashActivity.java:16)
^ your method is here
The other "Caused by:s" below are the inner exceptions.
java.lang.RuntimeException: Unable to start activity ComponentInfo{XXX/XXX.SplashActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
Try to re-size the Image from .png to .jpg format. This will decrease the size at nearly half scale. Then use a largeHeap tag in your Activities manifest file,
<application
....
android:largeHeap="true">
Also remember to make a try catch block so that you can bypass showing the Splash screen and redirect to the main activity.
While( ! success ) {
try {
// load resources
} catch (OutOfMemoryError E) {
// do something to use less memory and try again
}
}
First of all, why would you even show this Splash screen? It's not good practice, it's code doesn't do anything other than just waiting for 3 seconds to pass, it's clearly causing problems yet you insist on fixing it rather than getting rid of it altogether. Silly!
Second of all, devices like N1 that came many years ago do not have the RAM capabilities like modern devices do. With just one bitmap you're exhausting it's resources. Either optimize your bitmap usage or get rid of it altogether and find an alternative if you have to have a Splash screen.

My app freezes crashes when it's supposed to go to the next activity

I don't know why the app crashes whenever it's supposed to go to the next Activity. I made a practice app that works as it should when using the same format. I uploaded the files to gist.github.
The error is
1216-1216/com.example.chiozokamalu.newfreshstart E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException
at com.example.chiozokamalu.newfreshstart.MainActivity.onClick(MainActivity.java:164)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
On line 164 of MainActivity.java:
questionView.setText(questions[questionIndex]); // set the text to the next question
EDIT: After Varun helped me, I get a new error which is
1307-1307/com.example.chiozokamalu.newfreshstart E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chiozokamalu.newfreshstart/com.example.chiozokamalu.newfreshstart.Results1}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.chiozokamalu.newfreshstart.Results1.onCreate(Results1.java:58)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
The questions array at line no. 164 is going out of bound that means questionIndex is greater than the total length of the questions array. So you should add a check on the length before extracting value from the array.
You have added the check but after that you incremented the value so either you modify the if condition to if (questionIndex < questions.length -1)
or modify the questionIndex variable before the if condition
You are getting problem because you haven't initiated resultView9 that is why it is null and giving nullpointer, just initiatlise it as you done with other and everything is fine. You have initialised the resultView8 twice just add resultview9 over there

Converting drawable resource to Bitmap Android

I am Building an Npuzzle App.
The current issue I face is bringing a drawable object into my Activity so that I can convert it to a bitmap for splitting. I have found and attempted to use this Approach:
Drawable hold = Puzzle.this.getResources().getDrawable(R.drawable.img1);
BitmapDrawable hold2 = (BitmapDrawable) hold;
Bitmap bitPhoto = hold2.getBitmap();
When I run my app I get a null pointer exception in the first line of code. Any suggestions on how to fix this problem? this is the logcat when i run the program:
11-25 12:07:46.441 5388-5388/com.example.anthony.puzzler E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.anthony.puzzler/com.example.anthony.puzzler.Puzzle}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
at com.example.anthony.puzzler.Puzzle.<init>(Puzzle.java:28)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
            at dalvik.system.NativeStart.main(Native Method)
Clean up your project and rebuild it to make sure the id of the drawable is the correct one.
Check if getResources() is actually returning something.
Edit: calling getResources() before super.onCreate() method returns, it causes NPE.
Why don't you just directly get the media as a bitmap?
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
Should be simpler than getting a drawable and casting it.

Butterknife library not working with HoloEverywhere/Android bootstrap

I am currently coding on the Android Bootstrap app, I want to have the new DataPickers en make use of new widgets without worrying about porting back to 2.1+
so I use the HoloEveryWhere Library. I also use the Android Bootstrap with Butterknife library
When I debug I get this exception
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.webridge.whtsnxt.debug/nl.webridge.whtsnxt.authenticator.BootstrapAuthenticatorActivity}: butterknife.ButterKnife$UnableToInjectException: Unable to inject views for nl.webridge.whtsnxt.authenticator.BootstrapAuthenticatorActivity#40619210
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: butterknife.ButterKnife$UnableToInjectException: Unable to inject views for nl.webridge.whtsnxt.authenticator.BootstrapAuthenticatorActivity#40619210
at butterknife.ButterKnife.inject(ButterKnife.java:176)
at butterknife.ButterKnife.inject(ButterKnife.java:104)
at nl.webridge.whtsnxt.authenticator.BootstrapAuthenticatorActivity.onCreate(BootstrapAuthenticatorActivity.java:140)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3687)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at butterknife.ButterKnife.inject(ButterKnife.java:171)
            at butterknife.ButterKnife.inject(ButterKnife.java:104)
            at nl.webridge.whtsnxt.authenticator.BootstrapAuthenticatorActivity.onCreate(BootstrapAuthenticatorActivity.java:140)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3687)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: org.holoeverywhere.widget.AutoCompleteTextView
at nl.webridge.whtsnxt.authenticator.BootstrapAuthenticatorActivity$$ViewInjector.inject(BootstrapAuthenticatorActivity$$ViewInjector.java:14)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
I think there's a problem with the HoloEverywhere activity I use in my app.
For example I use the import org.holoeverywhere.app.Activity; in all my Activity classes.
But the Butterknife use android.app.Activity; The problem is, I can't change code in the Butterknife to use the Activity from HoloEverywhere.
Please tell me your thoughts on this I use the latest version of Butterknife and HoloEveryWhere.
Try to set up in your AndroidManifest.xml the holo theme:
<application
android:theme="#style/Holo.Theme.NoActionBar.Fullscreen"
or maybe only the in activity that is happening your problem (also in the manifest)
<activity
android:theme="#style/Holo.Theme.NoActionBar.Fullscreen"
Of course in your java class, if the component is defined as a holo component in the xml layout, in your java class you have to import that component from the holo library like you are doing:
import org.holoeverywhere.app.Activity
or
import org.holoeverywhere. whatever (like button, switch, whatever)
Check if your #InjectViews has correct type.
I've used ImageView instead of LinearLayout.
Also check if you are not using other ButterKnife's injection annotations incorrectly. I've used #OnItemClick annotation for RecyclerView, which was causing RuntimeException.

Resource not found excpetion for Android Style for Button

I want to set a button style programatically. This means without needing to define an xml file. I thought that by setting the background resource of the button with the predefined style IDs I could do this.
The line of code that fails is:
b1.setBackgroundResource(android.R.style.Widget_DeviceDefault_Light_Button_Borderless_Small);
There is an exception that states
android.content.res.Resources$NotFoundException: Resource ID #0x1030179.
What am I doing wrong?
EDIT:
I have tried to this:
Button b = new Button(context,null,android.R.style.Widget_DeviceDefault_Button_Borderless);
This no longer throws an exception but no real style is applied to the button. There is just text and there is no different states for when its pressed.
EDIT2:
Adding the original stack trace as requested:
03-03 08:17:58.810 5990-5990/legen.dary E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{legen.dary/legen.dary.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x103016c
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x103016c
at android.content.res.Resources.getValue(Resources.java:1013)
at android.content.res.Resources.getDrawable(Resources.java:658)
at android.view.View.setBackgroundResource(View.java:14179)
at legen.dary.TitleButtonView.<init>(TitleButtonView.java:24)
at legen.dary.MainActivity.onCreate(MainActivity.java:131)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Categories

Resources