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

}

4

1 回答 1

3

timeshowNPE 是由您当前正在查看的布局中不存在的事实引起的。改变 EstimatedTime = (TextView) findViewById(R.id.timeshow);

EstimatedTime = (TextView) view.findViewById(R.id.timeshow);哪里view是作为参数传入的视图。

于 2013-04-09T00:43:35.253 回答