此问题的标题描述了我的问题。我已经尝试了一段时间,我已经厌倦了搜索,并希望得到一些帮助。它曾经工作,直到我添加了Handler和runner。我正在尝试创建一个应用程序,这是一个简单的时钟应用程序,用户可以设置为他们想要的任何时间。每当我去运行应用程序时,它会打开然后说它无法正常启动。这是一个由另一个活动设置的活动,然后它会更改它接收的值,它还会在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>
清单:
<?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>
谢谢。
答案 0 :(得分:1)
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;
使用此::
更改上述代码int Hour = extras.getInt("HoursS");
Hours = Hour;
int Minute = extras.getInt("MinutesS");
Minutes = Minute;
int Second = extras.getInt("SecondsS");
Seconds = Second;
String TimeOfDaySs = extras.getString("TimeOfDayS");
TimeOfDayS = TimeOfDaySs;
简单地说,你得到空指针异常bcoz你正在将空值设置为Hour,Minute,Second和TimeOfDaySs并访问它们。
...干杯!!!