-2

I am trying to caal my Utill.java class from my Profile.java class. But here ia m facing issue on run time. I am getting error of unable to find activity compmonent. This is my code file. Profile.java

public class Profile extends ListActivity implements OnItemLongClickListener{


        //creating context menu
                @Override
                public void onCreateContextMenu(ContextMenu menu, View v,
                        ContextMenuInfo menuInfo) {
                    // TODO Auto-generated method stub
                    super.onCreateContextMenu(menu, v, menuInfo);
                    MenuInflater inflater = getMenuInflater();
                    inflater.inflate(R.menu.context_menu, menu);

                    }


        // creating context item    

                @Override
                public boolean onContextItemSelected(MenuItem item) {
                    // TODO Auto-generated method stub
                    switch(item.getItemId()){

                    case R.id.item1:
    //calling vibration method from here
                        startActivity(new Intent(Profile.this,Util.class));
                        return true;


                    case R.id.item2 :
                        startActivity(new Intent(Profile.this,Custom.class));

                        return true;
                    default:
                    return super.onContextItemSelected(item);
                    }




                }


            }

Utill.java

  public class Util extends Activity{

        private AudioManager audi = null;


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


            audi = (AudioManager) getSystemService(AUDIO_SERVICE);
//creating object to call methods           
Util mytest = new Util();
            mytest.activateVibration(); // calling method
            activateVibration();


        }
    //Method for activate vibration
        private void activateVibration() {
            // TODO Auto-generated method stub

                audi.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                }


    }

This is my logcat file

08-01 00:55:07.122: D/libEGL(26849): loaded /system/lib/egl/libGLES_hawaii.so
08-01 00:55:07.122: D/(26849): mem_init ++
08-01 00:55:07.122: D/(26849): gHwMemAllocator client 3
08-01 00:55:07.122: D/(26849): **** Using ION allocator ****
08-01 00:55:07.122: D/(26849): registered SIGUSR1[10] for pid[26849]
08-01 00:55:07.122: D/(26849): HwMemAllocatorImpl Static Counters 0 0
08-01 00:55:07.122: D/(26849): HwMemAllocatorImpl[4fa2edcc] totalDeviceAllocSize[0] totalFree[0] maxFree[0] in numSlabs[0]
08-01 00:55:07.122: D/(26849): mem_init 4fa2edcc--
08-01 00:55:07.122: D/ION(26849): config: version(0x10000) secure(0xf000) 256M(0x22d) fast(0x608) hwwr(0x608)
08-01 00:55:07.142: D/MM_DEVICE(26849): Waiting for mm thread to come up
08-01 00:55:07.142: D/MM_DEVICE(26849): mm_device_thread starting
08-01 00:55:07.152: D/HAWAII_EGL(26849): eglCreateContext() config: 18 context: 0x4fc28fa8, VC context 1, Thread 26849
08-01 00:55:07.152: D/HAWAII_EGL(26849): Set SWAP INTERVAL 0
08-01 00:55:07.162: D/HAWAII_EGL(26849): eglCreateWindowSurface() surface: 0x4fc28fc8, VC surface: 1, Thread: 26849
08-01 00:55:07.162: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x4fc28fc8, 0x4fc28fc8) Thread: 26849
08-01 00:55:07.162: D/OpenGLRenderer(26849): Enabling debug mode 0
08-01 00:55:08.233: D/AbsListView(26849): Get MotionRecognitionManager
08-01 00:55:08.253: D/AbsListView(26849): onVisibilityChanged() is called, visibility : 4
08-01 00:55:08.253: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:08.253: D/AbsListView(26849): onVisibilityChanged() is called, visibility : 0
08-01 00:55:08.253: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:08.273: D/HAWAII_EGL(26849): Set SWAP INTERVAL 0
08-01 00:55:08.273: D/HAWAII_EGL(26849): eglCreateWindowSurface() surface: 0x50672480, VC surface: 2, Thread: 26849
08-01 00:55:08.273: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50672480, 0x50672480) Thread: 26849
08-01 00:55:08.273: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:08.273: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x4fc28fc8, 0x4fc28fc8) Thread: 26849
08-01 00:55:08.283: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50672480, 0x50672480) Thread: 26849
08-01 00:55:08.293: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:08.293: D/WritingBuddyImpl(26849): getCurrentWritingBuddyView() 
08-01 00:55:08.343: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:09.174: D/AbsListView(26849): Get MotionRecognitionManager
08-01 00:55:09.254: D/HAWAII_EGL(26849): Set SWAP INTERVAL 0
08-01 00:55:09.254: D/HAWAII_EGL(26849): eglCreateWindowSurface() surface: 0x50541bf0, VC surface: 3, Thread: 26849
08-01 00:55:09.254: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50541bf0, 0x50541bf0) Thread: 26849
08-01 00:55:09.264: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:09.264: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:09.274: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:09.284: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:09.294: D/AbsListView(26849): unregisterIRListener() is called 
08-01 00:55:09.474: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50672480, 0x50672480) Thread: 26849
08-01 00:55:09.865: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50541bf0, 0x50541bf0) Thread: 26849
08-01 00:55:09.985: D/HAWAII_EGL(26849): eglMakeCurrent(NULL) Thread: 26849
08-01 00:55:09.985: D/HAWAII_EGL(26849): eglDestroySurface() surface: 0x50541bf0, android window 0x500cd850, Thread: 26849
08-01 00:55:10.005: D/AbsListView(26849): onDetachedFromWindow
08-01 00:55:10.015: D/AndroidRuntime(26849): Shutting down VM
08-01 00:55:10.015: W/dalvikvm(26849): threadid=1: thread exiting with uncaught exception (group=0x41d7c930)
08-01 00:55:10.045: E/AndroidRuntime(26849): FATAL EXCEPTION: main
08-01 00:55:10.045: E/AndroidRuntime(26849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.haider.first/com.haider.first.Util}: java.lang.NullPointerException
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.app.ActivityThread.access$700(ActivityThread.java:157)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.os.Looper.loop(Looper.java:176)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.app.ActivityThread.main(ActivityThread.java:5317)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at dalvik.system.NativeStart.main(Native Method)
08-01 00:55:10.045: E/AndroidRuntime(26849): Caused by: java.lang.NullPointerException
08-01 00:55:10.045: E/AndroidRuntime(26849):    at com.haider.first.Util.activateVibration(Util.java:30)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at com.haider.first.Util.onCreate(Util.java:20)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.app.Activity.performCreate(Activity.java:5326)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-01 00:55:10.045: E/AndroidRuntime(26849):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
08-01 00:55:10.045: E/AndroidRuntime(26849):    ... 11 more

i am facing error "unable to find activitycomponentinfo, java.lang.runtimeexception" plzz helpppp !!

shani jani
  • 39
  • 1
  • 1
  • 7
  • Please format your code and include the error message w/ log info. – Andrew_CS Jul 31 '15 at 19:47
  • Also, it looks like an Android app, it might help to use the tags 'Java' and 'Android'. – Andrew_CS Jul 31 '15 at 19:47
  • I have posted my logcat file also..now see the error – shani jani Jul 31 '15 at 19:58
  • Don't instantiate an Activity with `Util mytest = new Util();`. Just call the method with `activateVibration` as you are remove the bit about `mytest` – codeMagic Jul 31 '15 at 19:58
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Jul 31 '15 at 20:09
  • Ohh thanks codeMagic.. it worked for me !! But facing ta little issue it vibrates my phone with opening new blank file. How can i get rid of this blank file????? – shani jani Jul 31 '15 at 20:16

1 Answers1

0

Your Util class doesn't need to extend Activity(when Activity is extended a new window will appear when you start it with Intent). So you need to do it like this(Right click on java folder--->New Java class(If you are using Android Studio, and don't extend Activity):

public class Util{

    private AudioManager audi = null;

        //Method for activate vibration
    private void activateVibration() {
        // TODO Auto-generated method stub
            audi = (AudioManager) getSystemService(AUDIO_SERVICE);
            audi.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            }

}

And call the method like that in your activity:

switch(item.getItemId()){

    case R.id.item1:

    //Object initialization
    Util myUtil = new Util();

    //Call activateVibration() method
    myUtil.activateVibration();

    return true;


    case R.id.item2:
    startActivity(new Intent(Profile.this,Custom.class));

    return true;

    default:
    return super.onContextItemSelected(item);
    }

I think you need to learn more about Java and OOP concepts in order to start developing Android apps.

Hope it helps!

Chris
  • 6,105
  • 6
  • 38
  • 55
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/85077/discussion-on-answer-by-cristi-run-time-exception-error). – Martijn Pieters Aug 04 '15 at 07:33