无法获取要在新活动中显示的Edittext值?

时间:2012-12-21 02:59:36

标签: android nullpointerexception android-edittext spinner

有点像这样的菜鸟。我从编辑文本中获取一些值以转换为新活动时遇到一些麻烦。我正在尝试将其设置为所选的微调器项目将其放置在右侧文本视图中的位置。我没有关闭力,但它只是没有将用户值放入文本视图中。任何帮助将不胜感激。

主要活动

public class RoofingClaculatorActivity extends Activity implements OnClickListener,           android.view.View.OnClickListener {

EditText hFeetInput;
EditText hInchesInput;
EditText wFeetInput;
EditText wInchesInput;
Spinner slopeSpinner;
Button save;
int pos;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button save = (Button) findViewById(R.id.savedata);
    save.setOnClickListener(this);
}

public void onClick(View v) {
    // TODO Auto-generated method stub

    EditText hFeetInput = (EditText) findViewById(R.id.hFeetInput);
    EditText hInchesInput = (EditText) findViewById(R.id.hInchesInput);
    EditText wFeetInput = (EditText) findViewById(R.id.wFeetInput);
    EditText wInchesInput = (EditText) findViewById(R.id.wInchesInput);

    String hFeet = hFeetInput.getText().toString();
    String hInches = hInchesInput.getText().toString();
    String wFeet = wFeetInput.getText().toString();
    String wInches = wInchesInput.getText().toString();

    Double HFEET = Double.parseDouble(hFeet);
    Double HINCHES = Double.parseDouble(hInches);
    Double WFEET = Double.parseDouble(wFeet);
    Double WINCHES = Double.parseDouble(wInches);

    Intent i = new Intent(this, DisplayResults.class);
    Bundle b = new Bundle();
    b.putDouble("hFeetInput", HFEET);
    b.putDouble("hInchesInput", HINCHES);
    b.putDouble("wFeetInput", WFEET);
    b.putDouble("wInchesInput", WINCHES);

    i.putExtras(b);
    startActivity(i);
}

public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub

}

}

第二次活动

public class DisplayResults extends Activity implements OnClickListener, OnItemSelectedListener {
static Double aHInches;
static Double aWInches;
static Double aHFeet;
static Double aWFeet;
private static double Squared = 0;
private static double Inches = 0;   
static String aSquared= Double.toString(Squared);
static String addedInches=Double.toString(Inches);;;
static TextView a_feet;
static TextView b_feet;
static TextView a_Inches;
static TextView b_Inches;
int pos;
Spinner slopeSpinner;
ArrayAdapter<String> adapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.displayresult);
    initialize();

    adapter = new ArrayAdapter<String>(this, R.array.slopeArray, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    slopeSpinner = (Spinner) findViewById(R.id.slopeSpinner);




    Bundle gotData = getIntent().getExtras();
    aHFeet = gotData.getDouble("hFeetInput");
    aHInches = gotData.getDouble("hInchesInput");
    aWFeet = gotData.getDouble("wFeetInput");
    aWInches = gotData.getDouble("wInchesInput");
    Inches = aHInches + aWInches;
    Squared = aHFeet * aWFeet / 100;
    aSquared = Double.toString(Squared);
    addedInches = Double.toString(Inches);

}



private void initialize() {
    // TODO Auto-generated method stub
    a_feet = (TextView) findViewById(R.id.aHeight);
    b_feet = (TextView) findViewById(R.id.bHeight);
    a_Inches = (TextView) findViewById(R.id.aInches);
    b_Inches = (TextView) findViewById(R.id.bInches);

}

public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub

}

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    slopeSpinner.setAdapter(adapter);
    slopeSpinner.setOnItemSelectedListener(this);
    pos = slopeSpinner.getSelectedItemPosition();

    if(pos == 0){
        a_feet.setText(aSquared);
        a_Inches.setText(addedInches);
    }else if (pos == 1){
        b_feet.setText(aSquared);
        b_Inches.setText(addedInches);
    }
}

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

}

 }

再次感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

将下面的代码写入onItemSelected() DisplayResults.java文件的if (pos == 0){ a_feet.setText("Squared is:- " + aSquared); a_Inches.setText("Added Inches is:- " +addedInches); } else if (pos == 1){ b_feet.setText("Squared is:- " + aSquared); b_Inches.setText("Added Inches is:- " + addedInches); } 方法中,它会解决您的问题,如果您对此有任何疑问,请告诉我。

{{1}}