Java Android应用程序意外停止

Java Android应用程序意外停止,java,android,nullpointerexception,Java,Android,Nullpointerexception,当我试着运行我的应用程序看它是否工作时,它崩溃了,只是说它停止工作了。我的应用程序旨在检查当前日期和时间,然后检查日程安排(数组),然后将文本字段更改为下一次轮渡的时间。就我检查的代码将做什么我想做的(我不知道肯定)。无论如何,这是会话日志: 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

当我试着运行我的应用程序看它是否工作时,它崩溃了,只是说它停止工作了。我的应用程序旨在检查当前日期和时间,然后检查日程安排(数组),然后将文本字段更改为下一次轮渡的时间。就我检查的代码将做什么我想做的(我不知道肯定)。无论如何,这是会话日志:

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
代码如下:

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;
    }
}
}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView-ferryTime=(TextView)findViewById(R.id.ferryTime);
setText(convert());
}
静态int[]卫星时间=新int[]{10,50};
静态int[]sunTimes=newint[]{10,50};
静态整数[]周时间=新整数[]{0,301002003004005005306006206407007157307480081583084590093010011001130012301303013001401403015001530155016101630165017117301745180018151830184519001930200002030210021302200223023002330};
静态Calendar rightNow=Calendar.getInstance();
公共静态字符串checkdate(){
if(rightNow.get(Calendar.DAY\u OF_WEEK)=Calendar.SATURDAY){
返回“星期六”;
}else if(rightNow.get(Calendar.DAY\u OF_WEEK)=Calendar.SUNDAY){
返回“星期日”;
}否则{
返回“工作日”;
}
}
公共静态int getTime(){
int time=rightNow.get(日历小时数)*100;
time=time+rightNow.get(Calendar.MINUTE);
返回时间;
}
公共静态int checkTime(){
String today=checkdate();
如果(今天=“星期六”){
for(int i=1;i getTime()){
返回卫星时间[i];
}
}
}否则如果(今天=“星期日”){
for(int i=1;i getTime()){
返回太阳时[i];
}
}           
}else if(今天==“工作日”){
for(int i=1;i getTime()){
返回周时间[i];
}
}           
}
返回0;
}
公共静态int timeLeft(){
int actualTime=getTime();
int-ferryTime=checkTime();
返回实际时间-轮回时间;
}
公共静态int convert(){
int-ferrytime=checkTime();
如果(轮渡时间>1200){
返回轮回-1200;
}否则{
返回轮回;
}
}
}

编辑:在修复初始化
TextView套圈的位置后,我更新了
日志

您正在初始化
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());
}
可能重复的
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView ferryTime = (TextView)findViewById(R.id.ferrytime);
    ferryTime.setText(convert());
}