0

我是新手,请多多包涵。我创建了一个微调器并用一个字符串数组填充它,然后我创建了一个方法“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;
    }*/

}

然后这是我的日志:

    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)

提前致谢!

4

5 回答 5

0

您设置了全局变量,然后使用了局部变量,因此该变量仍然为空。

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

于 2012-06-06T16:48:05.823 回答
0

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

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

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

于 2012-06-06T16:48:30.427 回答
0

findViewById 应该在您正在寻找的视图的父级上调用。

代替

player1.findViewById(R.id.editTextPlayer1);

你需要

player1 = (EditText)findViewById(R.id.editTextPlayer1);
于 2012-06-06T16:49:52.693 回答
0

首先,利用switch's fall-through 功能来发挥你的优势:

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变量。

于 2012-06-06T16:53:30.727 回答
0

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

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

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

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

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

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

于 2012-06-06T17:01:49.237 回答