1 つのページに 2 つのスピナーがあり、それぞれが同じ路線/列車から駅を取得します。
ユーザーがスピナーから値を選択したときに、ビューの情報を「更新」したかったのです。両方のカスタム スピナー リスナーを作成しましたが、テキストビューにアクセスして、埋め込みクラス内から更新できないようです。
説明されているアクティビティのコードは以下に含まれています。ただし、その数は多いため、関心のある領域は、最後にある 2 つの組み込みクラス (SpinnerActivityDestination と SpinnerActivityOrigin) で見つけることができます。
エラーをスローしている正確な行は次のとおりです。
EstimatedTime = (TextView) findViewById(R.id.timeshow);
ビュー内のテキスト値にアクセスして、スピナーから数値を表示しようとしています。これはNullPointerExceptionをスローする場所です。クラス内からビューが膨張していないためだと思います。しかし、私はそれを回避する方法がわかりませんか?
ヘルプ/アドバイスをいただければ幸いです。
public class ChooseStations extends Activity {
public int GlobalSpinnerOrigin;
public int GlobalSpinnerDestination;
public double GlobalStationTime;
private MetroSleepDb db;
private Cursor stations;
SimpleCursorAdapter adapter3;
SimpleCursorAdapter adapter2;
TextView EstimatedTime;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_stations);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
String line_id = intent.getStringExtra("line");
db = new MetroSleepDb(this);
stations = db.getStations(line_id);
GlobalStationTime = 1.3;
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
s1.setOnItemSelectedListener(new SpinnerActivityOrigin());
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
s2.setOnItemSelectedListener(new SpinnerActivityDestination());
adapter2 = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
stations,
new String[] { "station_name"},
new int[] {android.R.id.text1}, 0);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter2);
adapter3 = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
stations,
new String[] { "station_name"},
new int[] {android.R.id.text1}, 0);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter3);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (checkSameValues()) {
showDialog_error();
} else {
Intent intent = new Intent(ChooseStations.this, ShowClock.class);
//intent.putExtra("line", line_value);
startActivity(intent);
}
}
});
}
public void showDialog_error() {
AlertDialog.Builder builder = new AlertDialog.Builder(ChooseStations.this);
builder.setMessage(R.string.dialogue_message)
.setTitle(R.string.dialog_title)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
public boolean checkSameValues() {
boolean result = false;
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
int v1 = s1.getSelectedItemPosition();
int v2 = s2.getSelectedItemPosition();
if(v1 == v2) {
result = true;
}
return result;
}
public String getItem(int pos) {
Cursor c = (Cursor) adapter2.getItem(pos);
String value = c.getString(c.getColumnIndex("line_id"));
return value;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public class SpinnerActivityOrigin extends Activity implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
GlobalSpinnerOrigin = pos;
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
public class SpinnerActivityDestination extends Activity implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
GlobalSpinnerDestination = pos;
if(GlobalSpinnerOrigin != GlobalSpinnerDestination){
int NewGlobalCalculation = Math.abs(GlobalSpinnerOrigin - GlobalSpinnerDestination);
int NewTimeArrival = multiply(NewGlobalCalculation,GlobalStationTime);
EstimatedTime = (TextView) findViewById(R.id.timeshow);
// EstimatedTime.setText(NewTimeArrival);
}
}
public int multiply(int a,double b){
return (int) (a * b);
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
}