【问题标题】:Can't get Edittext values to show in new activity?无法让 Edittext 值显示在新活动中?
【发布时间】:2012-12-21 14:00:53
【问题描述】:

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

主要活动

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

}

 }

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

【问题讨论】:

  • 您的 LogCat 打印什么?
  • 它什么也没说,我没有接近任何类型的力量,或者这里的任何东西就是它现在所说的。 12-20 22:06:49.787:I/Choreographer(2536):跳过 70 帧!应用程序可能在其主线程上做了太多工作。
  • @JoelRolon 有一个建议的答案(现在没有了)关于将您的 setText() 方法更改为 setText(""+yourValue); 我想知道您是否看到并尝试过?
  • 您在发送前检查过这些值吗?
  • 你试过intent.putExtra("hFeetInput", HFEET);而不是传递捆绑包?

标签: android nullpointerexception android-edittext spinner


【解决方案1】:

将以下代码写入DisplayResults.java 文件的onItemSelected() 方法,它将解决您的问题,如果您对此有任何疑问,请告诉我。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2015-02-10
    • 2017-12-17
    相关资源
    最近更新 更多