Domanda

When ever I try to run my app to see if it works, it crashes and just says it stop working. My app is designed to check the current date and time and then check the schedule(arrays) and then change a text field to the time of the next ferry. As far as i checked the code will do what i want to do (I dont know for sure). Anyways This is the session log :

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

UPDATED:

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

This is the code :

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;
    }
}
}

EDIT: I've updated the log after fixing the placement of the initializing TextView ferrytime.

È stato utile?

Soluzione

You're initializing TextView ferryTime outside of onCreate method. When you call ferryTime.setText() you get NullPointerException. Move it to onCreate like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView ferryTime = (TextView)findViewById(R.id.ferrytime);
    ferryTime.setText(convert());
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top