Switch导致android app终止/ java.lang.NullPointerException

时间:2013-11-30 13:56:13

标签: android nullpointerexception switch-statement

我的Android应用程序中的switch类出现问题,而且我对Java相对较新。 我在Android 4.3中使用Eclipse。 LogCat给了我这个输出:

11-30 08:29:18.296: D/AndroidRuntime(1562): Shutting down VM
11-30 08:29:18.334: W/dalvikvm(1562): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-30 08:29:18.354: E/AndroidRuntime(1562): FATAL EXCEPTION: main
11-30 08:29:18.354: E/AndroidRuntime(1562): java.lang.NullPointerException
11-30 08:29:18.354: E/AndroidRuntime(1562):     at de.madstar.asg_erfurtvertretungsplan.Vertreter$2.onClick(Vertreter.java:83)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at android.view.View.performClick(View.java:4240)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at android.view.View$PerformClick.run(View.java:17721)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at android.os.Handler.handleCallback(Handler.java:730)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at android.os.Looper.loop(Looper.java:137)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at java.lang.reflect.Method.invoke(Method.java:525)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-30 08:29:18.354: E/AndroidRuntime(1562):     at dalvik.system.NativeStart.main(Native Method)

我的应用程序:Vertreter.java(我的评论是德语,不要受到惊吓:D):

package de.madstar.asg_erfurtvertretungsplan;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class Vertreter extends Activity implements android.view.View.OnClickListener {

public Vertreter() {
   super();
   }

public Button btnHeute;
public Button btnMorgen;
public WebView WV;
public int foo = 1;

//Teil vom Day-Checker von btnMorgen
public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}
Day day;
public Vertreter(Day day) {
    this.day = day;
}




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vertreter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.vertreter, menu);

    WV = (WebView)findViewById(R.id.webView1);

    btnHeute = (Button)findViewById(R.id.button1);
    btnHeute.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {...});


    btnMorgen = (Button)findViewById(R.id.button2);
    btnMorgen.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click

            SimpleDateFormat formatter1 = new SimpleDateFormat ("yyyy-MM-");
            SimpleDateFormat formatter2 = new SimpleDateFormat ("dd");

            Date currentTime = new Date();


            //Prüfen ob Freitag,Samstag oder Sontag um auf Montag zu springen
            switch(day) {          
            case FRIDAY:
                foo = 3;
                break;

            case SATURDAY:
                foo = 2;
                break;

            case SUNDAY:
                foo = 1;
                break;

            case MONDAY: 
                foo = 1;
                break;

            case TUESDAY:
                foo = 1;
                break;

            case WEDNESDAY:
                foo = 1;
                break;

            case THURSDAY:
                foo = 1;
                break;
        }
            //formatter2 in integer umwandeln
            int form2 = Integer.parseInt(formatter2.format(currentTime));



            WV.loadUrl("http://schedule.asg-erfurt.de/" + formatter1.format(currentTime) + (form2 + foo));
        }
    });
    //      btnMorgen.setOnClickListener(this);



    return true;    
}

//Ab hier Optionen für dropdown menu in actionbar
@Override
public boolean onOptionsItemSelected(MenuItem item) {...}

private void settingsMenuItem(){...}

@Override
public void onClick(View v) {
    // TODO Nothing, just need it to run Onclick listener for buttons

}

}

我不知道为什么我的应用程序停止了,我尝试使用断点来查找源代码但我刚刚发现问题可能来自switch(day) {...}

1 个答案:

答案 0 :(得分:0)

您正在处理一个null的变量。当你做“日子”;你没有创建Day的实例,我没有在你的代码中看到任何Vertreter的创建,所以只要你没有在参数中创建一个带有参数日的对象,我想你就是问题。

Day day;
public Vertreter(Day day) 
{
    this.day = day;
}

因此,首先调用您的开关是空引用。 希望它会对你有所帮助。