Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an ImageButton I just added in my layout, but when I call setContentView on the layout my program is now crashing. Looking at logcat, it seems to give me a NullPointerException when trying to create this ImageButton. ic_menu_send.png is in the appropriate drawable folders. Here's the xml for the ImageButton:

<ImageButton
    android:id="@+id/imageButton1"
    android:src="@drawable/ic_menu_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/enterPassword2"
    android:layout_alignRight="@id/enterPassword1"
    android:layout_alignTop="@id/enterPassword2"
    android:background="@style/AppTheme"
    android:contentDescription="@string/submit_button" />

And here is the onCreate for this Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_time); // causes nullpointerexception
    EditText enterPassword1 = (EditText)findViewById(R.id.enterPassword1);
    enterPassword1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            return onEditAction(v);
        }
    });
    EditText enterPassword2 = (EditText)findViewById(R.id.enterPassword2);
    enterPassword2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            return onEditAction(v);
        }
    });
    /*ImageButton submit1 = (ImageButton)findViewById(R.id.imageButton1);
    submit1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText enterPassword1 = (EditText)findViewById(R.id.enterPassword1);
            onEditAction(enterPassword1);
        }
    });*/

The ImageButton part of the code was only commented out for testing, but the program crashes much earlier. Does anyone have an idea of what may be causing this? Thank you for your assistance.

Edit: Here are the logcat logs:

01-27 15:52:11.264: E/AndroidRuntime(31195): FATAL EXCEPTION: main
01-27 15:52:11.264: E/AndroidRuntime(31195): java.lang.RuntimeException: Unable to start activity ComponentInfo{us.securecow.nearfieldunlock/us.securecow.nearfieldunlock.FirstTimeActivity}: android.view.InflateException: Binary XML file line #50: Error inflating class android.widget.ImageButton
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.os.Looper.loop(Looper.java:137)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at java.lang.reflect.Method.invokeNative(Native Method)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at java.lang.reflect.Method.invoke(Method.java:511)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at dalvik.system.NativeStart.main(Native Method)
01-27 15:52:11.264: E/AndroidRuntime(31195): Caused by: android.view.InflateException: Binary XML file line #50: Error inflating class android.widget.ImageButton
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.Activity.setContentView(Activity.java:1881)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at us.securecow.nearfieldunlock.FirstTimeActivity.onCreate(FirstTimeActivity.java:19)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.Activity.performCreate(Activity.java:5104)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-27 15:52:11.264: E/AndroidRuntime(31195):    ... 11 more
01-27 15:52:11.264: E/AndroidRuntime(31195): Caused by: java.lang.reflect.InvocationTargetException
01-27 15:52:11.264: E/AndroidRuntime(31195):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-27 15:52:11.264: E/AndroidRuntime(31195):    ... 24 more
01-27 15:52:11.264: E/AndroidRuntime(31195): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.content.res.Resources.loadDrawable(Resources.java:1927)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.View.<init>(View.java:3328)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.widget.ImageView.<init>(ImageView.java:114)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.widget.ImageButton.<init>(ImageButton.java:87)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.widget.ImageButton.<init>(ImageButton.java:83)
01-27 15:52:11.264: E/AndroidRuntime(31195):    ... 27 more
share|improve this question
    
Post your LogCat logs. –  kcoppock Jan 27 '13 at 21:56
1  
Are you familiar with debugging? Null Pointer crashes are fairly easy to find if you just step through your code. Also the LogCat usually tells you the exact line it happens at. Posting the logcat output will help. –  Nathan Jan 27 '13 at 21:57
    
@nathan No, unfortunately I'm teaching myself all of this stuff. I'll edit my post to include logcat logs. –  Nathan Jan 27 '13 at 22:00
    
@kcoppock Posted. –  Nathan Jan 27 '13 at 22:07
1  
I don't think that android:background="@style/AppTheme" is valid. Try taking it out for testing. –  A--C Jan 27 '13 at 22:08

6 Answers 6

up vote 1 down vote accepted

Stack trace says it all:

Resource is not a Drawable (color or path):

According to the Android documentation, the background attribute needs either A drawable or a color.

Full quote:

A drawable to use as the background. This can be either a reference to a full drawable resource (such as a PNG image, 9-patch, XML state list description, etc), or a solid color such as "#ff000000" (black).

Style doesn't fit any of those criteria, and so, your app crashes at runtime.

share|improve this answer

The problem is related with one of the following:

  • android:src="@drawable/ic_menu_send"
  • android:background="@style/AppTheme"

and most probably the second one. These parameters require a drawable type of resource and one of them is not a drawable (as the stack trace suggests).

share|improve this answer

Remove "android:background="@style/AppTheme" from the layout xml.

share|improve this answer

this is what you should look for:

01-27 15:52:11.264: E/AndroidRuntime(31195): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}

try to remove the @style line and see what happens than, because you said the other resources are in place

share|improve this answer
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}

Well... dhoo...

share|improve this answer

Check your images(ic_menu_send) are there in your project under drawable folder.

I have same situation, but my images are corrupted and they are not visible when I check them.

After I copied all my images under drawable forlder, its all working fine.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.