Java Switch语句抛出Java.lang.NullPointerException - Android初学者

时间:2012-06-06 16:45:41

标签: java android eclipse android-layout nullpointerexception

我是新手,所以请耐心等待。我创建了一个微调器并用字符串数组填充它,然后我创建了一个方法“OnItemSelected”并插入了一个switch语句。然后该活动抛出NullPointerException。当我删除switch语句时,活动有效。我该如何解决这个问题?

代码:

EditText player1 = null;
EditText player2 = null;
EditText player3 = null;
EditText player4 = null;
EditText player5 = null;
EditText player6 = null;
EditText player7 = null;
EditText player8 = null;

Spinner spinner;

int numberOfPlayers = 0;
int counter = 0;

private String[] players ={"2","3","4", "5", "6", "7", "8"};
//private String[] names ={" "};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {        
    spinner = (Spinner)findViewById(R.id.spinnerplayers);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, players);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(this);
    }catch (Exception e) {

        // handle any errors

        Log.e("TestActivity", "Error in activity", e);  // log the error

        // Also let the user know something went wrong

        Toast.makeText(getApplicationContext(), e.getClass().getName() + " " + e.getMessage(),Toast.LENGTH_LONG).show();
    }
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
        long id) {
        numberOfPlayers = pos + 2;

    /*switch(pos){
    case 0:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        break;
    case 1:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        break;
    case 2:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        break;
    case 3: 
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);
        player5.findViewById(R.id.editTextPlayer5);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        player5.setVisibility(View.VISIBLE);
        break;
    case 4:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);
        player5.findViewById(R.id.editTextPlayer5);
        player6.findViewById(R.id.editTextPlayer6);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        player5.setVisibility(View.VISIBLE);
        player6.setVisibility(View.VISIBLE);
        break;
    case 5:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);
        player5.findViewById(R.id.editTextPlayer5);
        player6.findViewById(R.id.editTextPlayer6);
        player7.findViewById(R.id.editTextPlayer7);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        player5.setVisibility(View.VISIBLE);
        player6.setVisibility(View.VISIBLE);
        player7.setVisibility(View.VISIBLE);
        break;
    case 6:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);
        player5.findViewById(R.id.editTextPlayer5);
        player6.findViewById(R.id.editTextPlayer6);
        player7.findViewById(R.id.editTextPlayer7);
        player8.findViewById(R.id.editTextPlayer8);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        player5.setVisibility(View.VISIBLE);
        player6.setVisibility(View.VISIBLE);
        player7.setVisibility(View.VISIBLE);
        player8.setVisibility(View.VISIBLE);
        break;
    }*/

}

然后这是我的logcat:

    06-06 12:44:42.433: E/AndroidRuntime(5764): FATAL EXCEPTION: main
06-06 12:44:42.433: E/AndroidRuntime(5764): java.lang.NullPointerException
06-06 12:44:42.433: E/AndroidRuntime(5764):     at com.brightdesign.truthordare.Main.onItemSelected(Main.java:69)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:873)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.widget.AdapterView.access$300(AdapterView.java:45)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:839)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.os.Handler.handleCallback(Handler.java:587)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.os.Looper.loop(Looper.java:123)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at java.lang.reflect.Method.invokeNative(Native Method)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at java.lang.reflect.Method.invoke(Method.java:521)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at dalvik.system.NativeStart.main(Native Method)

提前致谢!

5 个答案:

答案 0 :(得分:0)

设置全局变量然后使用局部变量,因此变量仍为null。

这是我可以从你的代码中推断出的唯一问题,因为onItemSelected的一半被注释掉了。

答案 1 :(得分:0)

您正在访问com.brightdesign.truthordare.Main.onItemSelected(Main.java:69)中的空指针。唯一的例外是答案,而不是问题: - )

鉴于你有“EditText player1 = null;”这一行我们从来没有看到它的分配,我们必须是通灵的来帮助你(除非这是所有的代码,在这种情况下你需要回到你的“学习Java”书籍并涵盖基础知识)。

您需要在访问它之前进行分配。它是空的。

答案 2 :(得分:0)

应该在您要查找的视图的父级上调用findViewById。

而不是

player1.findViewById(R.id.editTextPlayer1);

你需要

player1 = (EditText)findViewById(R.id.editTextPlayer1);

答案 3 :(得分:0)

首先,使用switch的直通功能:

switch(pos){
case 2:
    player4 = (EditText) findViewById(R.id.editTextPlayer4);
    player4.setVisibility(View.VISIBLE);
    // Notice: no break command!

case 1:
    player3 = (EditText) findViewById(R.id.editTextPlayer3);
    player3.setVisibility(View.VISIBLE);

case 0: default:
    player1 = (EditText) findViewById(R.id.editTextPlayer1);
    player2 = (EditText) findViewById(R.id.editTextPlayer2);

    player1.setVisibility(View.VISIBLE);
    player2.setVisibility(View.VISIBLE);
}

此代码尝试在main.xml中查找每个EditText,并将该View设置为适当的player变量。

答案 4 :(得分:0)

我认为这会导致代码中的问题出现在onItemSelected() - 方法:

player1.findViewById(R.id.editTextPlayer1);
player2.findViewById(R.id.editTextPlayer2);
//...

方法findViewById()返回另一个视图或活动主视图的子视图。在你的情况下,引用似乎是空指针,你也不处理结果,这是使用该方法的唯一原因。

您应该只在onCreate()方法中设置一次这些引用:

spinner = (Spinner)findViewById(R.id.spinnerplayers);
//...
player1 = (EditText)findViewById(R.id.editTextPlayer1);
player2 = (EditText)findViewById(R.id.editTextPlayer2);
//...

这样你的所有引用在onCreate()之后都是有效的,你可以在onItemSelected()方法中安全地访问它们(我假设它导致了NullPointerException)