The title of this problem describes my problem. I have tried fixing this for a while and i am tired of searching and would like some help please. It used to work until I added the Handler and runner. I am trying to make an application that is a simple clock application that the user can set to any time that they want to. Whenever I go to run the application, it opens and then says it would not start properly. This is an activity that is set by another activity and then it changes the values it receives, it also displays these values in textviews.
package CoopFun.Clocks;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.content.Intent;
import android.app.Activity;
public class ClockMain extends Activity {
int Hours;
int Minutes;
int Seconds;
String TimeOfDayS;
TextView HoursMainV;
TextView MinutesMainV;
TextView SecondsMainV;
TextView TimeOfDayMainV;
Timer oneSecond;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.clock_main);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
int Hour = extras.getInt("HoursS");
Hour = Hours;
int Minute = extras.getInt("MinutesS");
Minute = Minutes;
int Second = extras.getInt("SecondsS");
Second = Seconds;
String TimeOfDaySs = extras.getString("TimeOfDayS");
TimeOfDaySs = TimeOfDayS;
HoursMainV = (TextView) findViewById(R.id.HoursMainV);
HoursMainV.setText(""+Hour);
MinutesMainV = (TextView) findViewById(R.id.MinutesMainV);
MinutesMainV.setText(":"+Minute);
SecondsMainV = (TextView) findViewById(R.id.SecondsMainV);
SecondsMainV.setText(":"+Second);
TimeOfDayMainV = (TextView) findViewById(R.id.TimeOfDayMainV);
TimeOfDayMainV.setText(" "+TimeOfDaySs);
final Handler handler=new Handler();
final Runnable r = new Runnable()
{
public void run()
{
++Seconds;
if(Seconds == 60){
++Minutes;
Seconds = 0;
if(Minutes == 60) {
++Hours;
Minutes = 0;
if(Hours == 12){
if(TimeOfDayS.equals("AM")) {
TimeOfDayS = "PM";
} else{
TimeOfDayS = "AM";
}
Hours = 0;
}
}
}
HoursMainV.append(""+Hours);
if(Minutes <=9) {
MinutesMainV.append(":0"+Minutes);
} else {
MinutesMainV.append(":"+Minutes);
}
if(Seconds <=9) {
SecondsMainV.append(":0"+Seconds);
} else {
SecondsMainV.append(":"+Seconds);
}
TimeOfDayMainV.append(" " + TimeOfDayS);
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textSize="50dip"
android:id="@+id/HoursMainV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dip"></TextView>
<TextView
android:textSize="50dip"
android:id="@+id/MinutesMainV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView
android:textSize="50dip"
android:id="@+id/SecondsMainV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView
android:textSize="50dip"
android:id="@+id/TimeOfDayMainV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="CoopFun.Clocks"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Clock"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ClockMain">
</activity>
</application>
</manifest>
Thank you.
Change above code with this::
Simply, u were getting null pointer exception bcoz u were setting null values to Hour,Minute,Second and TimeOfDaySs and accessing them.
Cheers….!!!