每次按下菜单按钮 --> History 启动 History.java 类时,我都会收到此应用程序的错误。我相当肯定它与将两个数组从 TipBookActivity.java 类发送到 History.java 类的 Bundle 方法有关。
下面是 TipBookActivity 代码:
public class TipBookActivity extends Activity {
/** Called when the activity is first created. */
TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
String sTip,sHour;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
Button bSubmit = (Button) findViewById(R.id.bSubmit);
final Bundle bTip = new Bundle();
final Bundle bHour = new Bundle();
final ArrayList<String> tipList = new ArrayList<String>();
final ArrayList<String> hourList = new ArrayList<String>();
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textHour.setText(editHour.getText().toString());
textTip.setText(editTip.getText().toString());
wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
String tip = String.format("$%.2f",wage);
textWage.setText(String.valueOf(tip) + " an hour");
textHour.setText(editHour.getText() + " Hour(s)");
textTip.setText("$" + editTip.getText());
bTip.putStringArray(sTip,new String[] {editTip.getText().toString()});
bHour.putStringArray(sHour,new String[] {editHour.getText().toString()});
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
Intent i = new Intent(TipBookActivity.this,History.class);
i.putExtras(bTip);
i.putExtras(bHour);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mMain = getMenuInflater();
mMain.inflate(R.menu.main_menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuHistory:
startActivity(new Intent("com.smarticle.tipbook.HISTORY"));
return true;
case R.id.menuClear:
//set up next tutorials
Toast display = Toast.makeText(this, "Clear History feature coming soon.", Toast.LENGTH_SHORT);
display.show();
return true;
}
return false;
}
}
历史类代码:
public class History extends Activity{
private ListView mainListViewTip;
private ListView mainListViewHour;
private ArrayAdapter<String>listAdapterTip;
private ArrayAdapter<String>listAdapterHour;
String sTip,sHour;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.history_main);
Bundle bTip = this.getIntent().getExtras();
Bundle bHour = this.getIntent().getExtras();
String[] array1 = bTip.getStringArray(sTip);
String[] array2 = bHour.getStringArray(sHour);
ListView mainListViewTip = (ListView) findViewById(R.id.mainListViewTip);
ListView mainListViewHour = (ListView) findViewById(R.id.mainListViewHour);
ArrayList<String> tipList = new ArrayList<String>();
ArrayList<String> hourList = new ArrayList<String>();
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
listAdapterTip = new ArrayAdapter<String>(this,R.layout.simplerow,tipList);
listAdapterHour = new ArrayAdapter<String>(this,R.layout.simplerow,hourList);
mainListViewTip.setAdapter(listAdapterTip);
mainListViewHour.setAdapter(listAdapterHour);
}
}
任何有关确定错误原因的帮助将不胜感激。该代码有效(理论上,我认为),它在实践中不起作用。大体思路是在EditText字段中输入两个数字,保存为字符串,显示为TextView,设置为ArrayList,然后捆绑发送到其他类中显示在ListView中。