0

我正在尝试为微调器制作适配器,但是我在 answertime.setAdapter(adapter); 上得到一个 NullPointerException 为什么是这样?我的数组在它们应该在的 strings.xml 中?这是完整的代码。

package lijap.app.starcraft2counters;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class Settings extends Activity implements OnItemSelectedListener {
Spinner answertime;
Spinner gametime;
Spinner missesallowed;

String[] answerseconds = { "Unlimited", "1 second", "2 seconds",
        "3 seconds", "4 seconds", "5 seconds" };

String[] gameminutes = { "Unlimited", "1 minute", "2 minutes", "3 minutes",
        "4 minutes", "5 minutes" };

String[] numberofmisses = { "Unlimited", "5", "10", "15", "20", "25" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.AnswerSeconds, R.layout.custom_spinner_list);
    adapter.setDropDownViewResource(R.layout.custom_spinner);
    answertime.setAdapter(adapter);

    ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.GameMinutes, R.layout.custom_spinner_list);
    adapter.setDropDownViewResource(R.layout.custom_spinner);
    gametime.setAdapter(adapter);

    ArrayAdapter adapter3 = ArrayAdapter.createFromResource(this, R.array.NumberOfMisses, R.layout.custom_spinner_list);
    adapter.setDropDownViewResource(R.layout.custom_spinner);
    missesallowed.setAdapter(adapter);


    answertime = (Spinner) findViewById(R.id.s_answertime);
    answertime.setAdapter(adapter);
    answertime.setOnItemSelectedListener(this);


    missesallowed = (Spinner) findViewById(R.id.s_mistakenumber);
    missesallowed.setAdapter(adapter2);
    missesallowed.setOnItemSelectedListener(this);

    gametime = (Spinner) findViewById(R.id.s_gametime);
    gametime.setAdapter(adapter3);
    gametime.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

任何帮助表示赞赏-Lijap

4

3 回答 3

3

你需要初始化

Spinner answertime;
Spinner gametime;
Spinner missesallowed;

否则,以下行将为您提供NullPointerException

answertime.setAdapter(adapter);
gametime.setAdapter(adapter);
missesallowed.setAdapter(adapter);

要初始化Spinners,您应该在调用后添加如下内容setContentView

answertime = findViewById(R.id.answertime);
gametime = findViewById(R.id.gametime);
missesallowed = findViewById(R.id.missesallowed);
于 2012-07-05T22:30:40.820 回答
2

您永远不会初始化answertime,gametimemissesallowed. 你需要做这样的事情,

answertime = findViewById(R.id.<the id for this spinner>);
gametime = findViewById(R.id.<the id for this spinner>);
missesallowed = findViewById(R.id.<the id for this spinner>);

在你打电话setAdapter()给他们之前。

还有一个问题,

ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.GameMinutes, R.layout.custom_spinner_list);
adapter.setDropDownViewResource(R.layout.custom_spinner);
gametime.setAdapter(adapter);

请注意,您在 reference 中构造了一些东西adapter2,然后修改了adapter,并将其设置adapter为微调器的适配器。那是你要的吗?

于 2012-07-05T22:31:45.037 回答
0

还要确保视图可见性设置为 View.VISIBLE

于 2013-07-25T07:22:45.600 回答