Android Help

+1 Wilford Ice Brimley · July 21, 2014
Hello everyone,

I have been reading Android Application Development for Dummies (2nd edition) by Michael Burton and Donn Felker.
(It's outdated, but it's the best my library has to offer)
Anyway, I was following the first major project that would turn off/on your ringer by pushing a button. But every time I try to run it the program crashes. I know my version android development kit is newer then what is used in the book. Also, the IDE says I have no in code errors.

here is my  Java code:

package com.dummies.android.silentmodetoggle;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

private AudioManager mAudioManager;
private boolean mPhoneIsSilent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

checkIfPhoneIsSilent();

setButtonClickListener();



mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

}

private void setButtonClickListener(){
Button toggleButton = (Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if(mPhoneIsSilent){
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent = false;
}else{
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mPhoneIsSilent = true;
}
toggleUi();

}
});

}
private void checkIfPhoneIsSilent(){
int ringerMode = mAudioManager.getRingerMode();
if (ringerMode == AudioManager.RINGER_MODE_SILENT){
mPhoneIsSilent = true;
}else{
mPhoneIsSilent = false;
}
}

private void toggleUi(){
ImageView imageView = (ImageView)findViewById(R.drawable.phone_on);
Drawable newPhoneImage;

if(mPhoneIsSilent){
newPhoneImage = getResources().getDrawable(R.drawable.phone_silent);
}else{
newPhoneImage = getResources().getDrawable(R.drawable.phone_on);
}
imageView.setImageDrawable(newPhoneImage);
}
@Override
protected void onResume(){
super.onResume();
checkIfPhoneIsSilent();
toggleUi();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}

}



My Fragment_main.xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dummies.android.silentmodetoggle.MainActivity$PlaceholderFragment" >
    
    <ImageView
        android:id="@+id/phone_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/phone_on" />
    
    <Button 
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Toggle Silent Mode" />
    
   

    

</LinearLayout>




my logCat:
07-21 16:00:50.675: D/AndroidRuntime(24575): Shutting down VM

07-21 16:00:50.675: W/dalvikvm(24575): threadid=1: thread exiting with uncaught exception (group=0x41ea9700)
07-21 16:00:50.680: E/AndroidRuntime(24575): FATAL EXCEPTION: main
07-21 16:00:50.680: E/AndroidRuntime(24575): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dummies.android.silentmodetoggle/com.dummies.android.silentmodetoggle.MainActivity}: java.lang.NullPointerException
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.app.ActivityThread.access$700(ActivityThread.java:168)
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.os.Looper.loop(Looper.java:137)
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.app.ActivityThread.main(ActivityThread.java:5493)
07-21 16:00:50.680: E/AndroidRuntime(24575): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 16:00:50.680: E/AndroidRuntime(24575): at java.lang.reflect.Method.invoke(Method.java:525)
07-21 16:00:50.680: E/AndroidRuntime(24575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
07-21 16:00:50.680: E/AndroidRuntime(24575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
07-21 16:00:50.680: E/AndroidRuntime(24575): at dalvik.system.NativeStart.main(Native Method)
07-21 16:00:50.680: E/AndroidRuntime(24575): Caused by: java.lang.NullPointerException
07-21 16:00:50.680: E/AndroidRuntime(24575): at com.dummies.android.silentmodetoggle.MainActivity.checkIfPhoneIsSilent(MainActivity.java:63)
07-21 16:00:50.680: E/AndroidRuntime(24575): at com.dummies.android.silentmodetoggle.MainActivity.onCreate(MainActivity.java:28)
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.app.Activity.performCreate(Activity.java:5372)
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
07-21 16:00:50.680: E/AndroidRuntime(24575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
07-21 16:00:50.680: E/AndroidRuntime(24575): ... 11 more


Post a Reply

Replies

Oldest  Newest  Rating
0 Wilford Ice Brimley · July 22, 2014
Line 62-69

private void checkIfPhoneIsSilent(){
int ringerMode = mAudioManager.getRingerMode();
if (ringerMode == AudioManager.RINGER_MODE_SILENT){
mPhoneIsSilent = true;
}else{
mPhoneIsSilent = false;
}
}


line 63 is: int ringerMode = mAudioManager.getRingerMode();
  • 1

Java / Android Development

107,105 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator