After following a beginners tutorial I ended up combining a bunch of lessons to create a timer app which takes an amount of time and plays media once the time passes. The app worked perfectly…Yesterday. I don’t recall changing anything but when I tried to debug today the app would load to my phone, install, and crash. I get a load of error messages which I don’t understand:
02-28 15:12:58.864: E/AndroidRuntime(26257): FATAL EXCEPTION: main
02-28 15:12:58.864: E/AndroidRuntime(26257): java.lang.RuntimeException: Unable to start activity ComponentInfo{digital.clock.activity/digital.clock.activity.DigitalActivity}: java.lang.NullPointerException
02-28 15:12:58.864: E/AndroidRuntime(26257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-28 15:12:58.864: E/AndroidRuntime(26257): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-28 15:12:58.864: E/AndroidRuntime(26257): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-28 15:12:58.864: E/AndroidRuntime(26257): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-28 15:12:58.864: E/AndroidRuntime(26257): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 15:12:58.864: E/AndroidRuntime(26257): at android.os.Looper.loop(Looper.java:130)
02-28 15:12:58.864: E/AndroidRuntime(26257): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-28 15:12:58.864: E/AndroidRuntime(26257): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 15:12:58.864: E/AndroidRuntime(26257): at java.lang.reflect.Method.invoke(Method.java:507)
02-28 15:12:58.864: E/AndroidRuntime(26257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-28 15:12:58.864: E/AndroidRuntime(26257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-28 15:12:58.864: E/AndroidRuntime(26257): at dalvik.system.NativeStart.main(Native Method)
02-28 15:12:58.864: E/AndroidRuntime(26257): Caused by: java.lang.NullPointerException
02-28 15:12:58.864: E/AndroidRuntime(26257): at digital.clock.activity.DigitalActivity.onCreate(DigitalActivity.java:74)
02-28 15:12:58.864: E/AndroidRuntime(26257): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 15:12:58.864: E/AndroidRuntime(26257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-28 15:12:58.864: E/AndroidRuntime(26257): ... 11 more
And my main activity looks like so…
package digital.clock.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.AlarmManager;
import android.app.PendingIntent;
import java.util.ArrayList;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
public class DigitalActivity extends Activity
{
private GestureLibrary gLib;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gLib.load())
{
Toast.makeText(this, "Could not load Gesture Library", Toast.LENGTH_LONG).show();
finish();
}
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
final OnGesturePerformedListener handleGestureListener = new OnGesturePerformedListener()
{
@Override
public void onGesturePerformed(GestureOverlayView gestureView, Gesture gesture)
{
ArrayList<Prediction> predictions = gLib.recognize(gesture);
// one prediction needed
if (predictions.size() > 0)
{
Prediction prediction = predictions.get(0);
// checking prediction
if (prediction.score > 1.0)
{
// and action
timerAlert2();
}
}
}
};
gestures.addOnGesturePerformedListener(handleGestureListener);
//Create button and do something with intents...
Button Activity2 = (Button) findViewById(R.id.Button01);
Activity2.setOnClickListener(new View.OnClickListener()
{
public void onClick (View view)
{
Intent replyIntent = new Intent();
setResult(RESULT_OK, replyIntent);
finish();
}
});
Button startTimer = (Button) findViewById(R.id.startTimer);
startTimer.setOnClickListener(new View.OnClickListener()
{
public void onClick (View view)
{
timerAlert(view);
}
});
Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
stopAlarm.setOnClickListener(new View.OnClickListener()
{
public void onClick (View view)
{
stopService(new Intent(getBaseContext(), MediaPlayerService.class));
}
});
}
public void timerAlert (View view)
{
//Declare the EditText object
EditText textField = (EditText) findViewById(R.id.timeInSeconds);
//Get user-entered number and convert to string
int i = Integer.parseInt(textField.getText().toString());
//Declare intent object
Intent timerIntent = new Intent(this, TimerBroadcastReceiver.class);
//Create PendingIntent and set parameters (context, code, intent object to use, constants)
PendingIntent myPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, timerIntent, 0);
//Create AlarmManager and assign it the ALARM_SERVICE
AlarmManager myAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//set AlarmManager parameters (type, TriggerTime, Operation)
//RTC_WAKEUP will wake up the phone for the alarm, RTC will activate the alarm next time the phone wakes
myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), myPendingIntent);
Toast.makeText(this, "Alarm is set for " + i + " seconds!", Toast.LENGTH_LONG).show();
}
public void timerAlert2 ()
{
int i = 5;
//Declare intent object
Intent timerIntent = new Intent(this, TimerBroadcastReceiver.class);
//Create PendingIntent and set parameters (context, code, intent object to use, constants)
PendingIntent myPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, timerIntent, 0);
//Create AlarmManager and assign it the ALARM_SERVICE
AlarmManager myAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//set AlarmManager parameters (type, TriggerTime, Operation)
//RTC_WAKEUP will wake up the phone for the alarm, RTC will activate the alarm next time the phone wakes
myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 60000), myPendingIntent);
Toast.makeText(this, "Alarm is set for " + i + " minutes!", Toast.LENGTH_LONG).show();
}
}
Seems like it isn’t finding your button: (it crashes on the line that tries to access the button you instantiated in line 73: Button01).
Search your project, it should have a sub-directory called res and in it there should be a file named main.xml. In it you should find an element
You should verify if your project includes this file and has that line in it. If you still have the problem, clean your project, that should correct it.