当我尝试运行我的应用程序以查看它是否有效时,它崩溃并且只是说它停止工作。我的应用程序旨在检查当前日期和时间,然后检查计划(数组),然后将文本字段更改为下一次渡轮的时间。据我检查,代码将做我想做的事情(我不确定)。无论如何这是会话日志:
05-02 21:09:20.350: D/AndroidRuntime(935): Shutting down VM
05-02 21:09:20.350: W/dalvikvm(935): threadid=1: thread exiting with uncaught exception (group=0xb4a85ba8)
05-02 21:09:20.370: E/AndroidRuntime(935): FATAL EXCEPTION: main
05-02 21:09:20.370: E/AndroidRuntime(935): Process: sep.justin.statenislandferry, PID: 935
05-02 21:09:20.370: E/AndroidRuntime(935): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{sep.justin.statenislandferry/com.example.statenislandferry.MainActivity}: java.lang.NullPointerException
05-02 21:09:20.370: E/AndroidRuntime(935): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-02 21:09:20.370: E/AndroidRuntime(935): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-02 21:09:20.370: E/AndroidRuntime(935): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-02 21:09:20.370: E/AndroidRuntime(935): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-02 21:09:20.370: E/AndroidRuntime(935): at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 21:09:20.370: E/AndroidRuntime(935): at android.os.Looper.loop(Looper.java:136)
05-02 21:09:20.370: E/AndroidRuntime(935): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 21:09:20.370: E/AndroidRuntime(935): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 21:09:20.370: E/AndroidRuntime(935): at java.lang.reflect.Method.invoke(Method.java:515)
05-02 21:09:20.370: E/AndroidRuntime(935): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 21:09:20.370: E/AndroidRuntime(935): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 21:09:20.370: E/AndroidRuntime(935): at dalvik.system.NativeStart.main(Native Method)
05-02 21:09:20.370: E/AndroidRuntime(935): Caused by: java.lang.NullPointerException
05-02 21:09:20.370: E/AndroidRuntime(935): at android.app.Activity.findViewById(Activity.java:1884)
05-02 21:09:20.370: E/AndroidRuntime(935): at com.example.statenislandferry.MainActivity.<init>(MainActivity.java:22)
05-02 21:09:20.370: E/AndroidRuntime(935): at java.lang.Class.newInstanceImpl(Native Method)
05-02 21:09:20.370: E/AndroidRuntime(935): at java.lang.Class.newInstance(Class.java:1208)
05-02 21:09:20.370: E/AndroidRuntime(935): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-02 21:09:20.370: E/AndroidRuntime(935): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-02 21:09:20.370: E/AndroidRuntime(935): ... 11 more
更新:
05-02 22:05:02.090: W/ResourceType(824): No package identifier when getting value for resource number 0x00000406
05-02 22:05:02.090: D/AndroidRuntime(824): Shutting down VM
05-02 22:05:02.100: W/dalvikvm(824): threadid=1: thread exiting with uncaught exception (group=0xb4a2dba8)
05-02 22:05:02.110: E/AndroidRuntime(824): FATAL EXCEPTION: main
05-02 22:05:02.110: E/AndroidRuntime(824): Process: sep.justin.statenislandferry, PID: 824
05-02 22:05:02.110: E/AndroidRuntime(824): java.lang.RuntimeException: Unable to start activity ComponentInfo{sep.justin.statenislandferry/com.example.statenislandferry.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x406
05-02 22:05:02.110: E/AndroidRuntime(824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.os.Looper.loop(Looper.java:136)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 22:05:02.110: E/AndroidRuntime(824): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 22:05:02.110: E/AndroidRuntime(824): at java.lang.reflect.Method.invoke(Method.java:515)
05-02 22:05:02.110: E/AndroidRuntime(824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 22:05:02.110: E/AndroidRuntime(824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 22:05:02.110: E/AndroidRuntime(824): at dalvik.system.NativeStart.main(Native Method)
05-02 22:05:02.110: E/AndroidRuntime(824): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x406
05-02 22:05:02.110: E/AndroidRuntime(824): at android.content.res.Resources.getText(Resources.java:244)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.widget.TextView.setText(TextView.java:3888)
05-02 22:05:02.110: E/AndroidRuntime(824): at com.example.statenislandferry.MainActivity.onCreate(MainActivity.java:18)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.app.Activity.performCreate(Activity.java:5231)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 22:05:02.110: E/AndroidRuntime(824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-02 22:05:02.110: E/AndroidRuntime(824): ... 11 more
05-02 22:05:06.750: I/Process(824): Sending signal. PID: 824 SIG: 9
这是代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView ferryTime = (TextView)findViewById(R.id.ferrytime);
ferryTime.setText(convert());
}
static int[] satTimes = new int[] {10,50};
static int[] sunTimes = new int[] {10,50};
static int[] weekTimes = new int[] {0,30,100,200,300,400,500,530,600,620,640,700,715,730,745,800,815,830,845,900,930,1000,1030,1100,1130,1200,1230,1300,1330,1400,1430,1500,1530,1550,1610,1630,1650,1710,1730,1745,1800,1815,1830,1845,1900,1930,2000,2030,2100,2130,2200,2230,2300,2330};
static Calendar rightNow = Calendar.getInstance();
public static String checkdate(){
if(rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
return "saturday";
}else if (rightNow.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
return "sunday";
} else {
return "weekday";
}
}
public static int getTime(){
int time = rightNow.get(Calendar.HOUR_OF_DAY) * 100;
time = time + rightNow.get(Calendar.MINUTE);
return time;
}
public static int checkTime(){
String today = checkdate();
if (today == "saturday"){
for(int i = 1; i <= satTimes.length; i ++ ){
if(satTimes[i] > getTime()){
return satTimes[i];
}
}
} else if (today == "sunday"){
for(int i = 1; i <= sunTimes.length; i ++ ){
if(sunTimes[i] > getTime()){
return sunTimes[i];
}
}
} else if (today == "weekday") {
for(int i = 1; i <= weekTimes.length; i ++ ){
if(weekTimes[i] > getTime()){
return weekTimes[i];
}
}
}
return 0;
}
public static int timeLeft(){
int actualTime = getTime();
int ferryTime = checkTime();
return actualTime - ferryTime;
}
public static int convert(){
int ferrytime = checkTime();
if (ferrytime > 1200){
return ferrytime - 1200;
}else{
return ferrytime;
}
}
}
编辑:我在修正初始化log
的展示位置后更新了TextView ferrytime
。
答案 0 :(得分:3)
您正在TextView ferryTime
方法之外初始化onCreate
。当您致电ferryTime.setText()
时,您会获得NullPointerException
。将其移至onCreate
,如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView ferryTime = (TextView)findViewById(R.id.ferrytime);
ferryTime.setText(convert());
}