0
import com.ave.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;



public class Menu extends Activity{
ImageButton select;    
int isClicked = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        select = (ImageButton)findViewById(R.id.select);
select.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
             if (isClicked == 0){
                select.setImageResource(R.drawable.select_pressed);
                isClicked = 1;
             }
             else{
                select.setImageResource(R.drawable.select);
                isClicked = 0;
             }
        }});
}

ImageButton audio;    
int isClicked1 = 0;{

audio = (ImageButton)findViewById(R.id.audio);
audio.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
             if (isClicked1 == 0){
                audio.setImageResource(R.drawable.audio_pressed);
                isClicked1 = 1;
             }
             else{
                audio.setImageResource(R.drawable.audio);
                isClicked1 = 0;
             }
         }
   });
 }
}

I'm not sure whats going wrong. There are no errors within the xml files or java class, however, when i go to test out the app via my phone or the AVD, after the splash screen it just randomly force closes.

LogCat

E AndroidRuntime: FATAL EXCEPTION: main
07-05 10:47:59.318  9362  9362 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ave/com.ave.Menu}: java.lang.NullPointerException
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1618)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.app.ActivityThread.access$1500(ActivityThread.java:124)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:123)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:3806)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at java.lang.reflect.Method.invokeNative(Native Method
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Method.java:507)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at dalvik.system.NativeStart.main(Native Method)
07-05 10:47:59.318  9362  9362 E AndroidRuntime: Caused by: java.lang.NullPointerException
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.app.Activity.findViewById(Activity.java:1693)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at com.ave.Menu.<init>(Menu.java:41)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at java.lang.Class.newInstanceImpl(Native Method)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at java.lang.Class.newInstance(Class.java:1409)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1610)
07-05 10:47:59.318  9362  9362 E AndroidRuntime:    ... 11 more
9
  • and also, you should learn how to use adb logcat prior to post question here. Commented Jul 5, 2011 at 12:51
  • @ACM64 ok i figured out how to dump the output to a txtfile, but its not as organized as the console's output. I'm not sure which i should paste to this post. Commented Jul 5, 2011 at 13:43
  • @Cataroux, if you are using eclipse then you can open logcat window and view all the logs related to android emulator and your android application. Commented Jul 5, 2011 at 13:44
  • @Cataroux, It seems like emulator is not able to find some view (ImageBUtton). Make sure you have mentioned that ImageButton in xml file of your activity. Commented Jul 5, 2011 at 13:46
  • @ Shekhar oh its there. I can see it when i switch to the graphical layout view of the main xml. There aren't any errors either. Commented Jul 5, 2011 at 13:49

3 Answers 3

3

I think the issue here is that your onCreate is closed too early. Everything I see should be in the onCreate code block. Remove the } after the first onClick block (marked by }});) and add it back at the end of the file

Sign up to request clarification or add additional context in comments.

1 Comment

you are right. onCreate() is closed too early. but this will generate compile time exception, not runtime exception. I think @Cataroux has posted his code incorrectly.
1

Have you tried updating the AndroidManifest.xml file with your Activitys?

1 Comment

I'm not sure what i would put, because all i've add are imageButton methods.
0

I think I have faced this same problem.

Usually this error occurs when particular resource is not found.

In your case, check whether following resource is present in xml file corresponding to your view.

select = (ImageButton)findViewById(R.id.select);

OR this resource

audio = (ImageButton)findViewById(R.id.audio);

Hope it helps.

Logcat output can help us to pinpoint the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.