Android : Button not working in Android AudioManager

on Tuesday, October 28, 2014


Good day all, I'm new to android development and I'm trying to make a simple Audio Manager where the MainActivity class will handle button listener event and Listeners class will have silent(), vibrate(), ring() methods. So far app is launching but whenever I click on vibrate button app gets Force Close. I think there is something wrong in my code but I can't figure it out since I'm new.


MainActivity.java



public class MainActivity extends Activity {
private Button home, pocket, silent;
private TextView tv;
private Listeners myAudManHolder;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

home = (Button) findViewById(R.id.home);
pocket = (Button) findViewById(R.id.pocket);
silent = (Button) findViewById(R.id.silent);
tv = (TextView) findViewById(R.id.tv);

myAudManHolder = new Listeners();

pocket.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
myAudManHolder.vibrate();
}

});
}


Listeners.java



public class Listeners {
AudioManager myAudMan;

public void vibrate() {
myAudMan.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}


LogCat Logs



10-28 20:57:51.381: W/dalvikvm(1232): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-28 20:57:51.401: E/AndroidRuntime(1232): FATAL EXCEPTION: main
10-28 20:57:51.401: E/AndroidRuntime(1232): java.lang.NullPointerException
10-28 20:57:51.401: E/AndroidRuntime(1232): at edu.shihank.audiomanager.Listeners.vibrate(Listeners.java:13)
10-28 20:57:51.401: E/AndroidRuntime(1232): at edu.shihank.audiomanager.MainActivity$1.onClick(MainActivity.java:39)


What is wrong in my vibrate() method and its OnClickListener event? Please I need this solution today and your help would be really appreciable. Tnx.


0 comments:

Post a Comment