gpt4 book ai didi

java - 尝试在自定义监听器类中检索 textview 时接收 NullPointerException

转载 作者:搜寻专家 更新时间:2023-11-01 08:59:59 25 4
gpt4 key购买 nike

我在一个页面上有两个微调器,每个微调器都从同一条线路/火车上检索车站。

我想在用户从微调器中选择一个值时“更新” View 中的信息。我已经为它们创建了一个自定义微调器监听器,但我似乎无法从我的嵌入式类内部访问要更新的 TextView 。

我所描述的 Activity 的代码包含在下面。但是有很多,所以感兴趣的领域可以在最后的两个嵌入式类中找到:SpinnerActivityDestination 和 SpinnerActivityOrigin。

引发错误的确切行是:

EstimatedTime  = (TextView) findViewById(R.id.timeshow);

我正在尝试访问 View 中的文本值并显示来自微调器的数值。这是它抛出 NullPointerException 的地方,我假设是因为从类内部来看, View 没有膨胀。但我不知道如何绕过它?

如有任何帮助/建议,我们将不胜感激。

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
}
}

最佳答案

NPE 是由于 timeshow 在您当前查看的布局中不存在而引起的。更改 EstimatedTime = (TextView) findViewById(R.id.timeshow);

EstimatedTime = (TextView) view.findViewById(R.id.timeshow); 其中 view 是作为参数传入的 View。

关于java - 尝试在自定义监听器类中检索 textview 时接收 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15891362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com