1

每次按下菜单按钮 --> 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中。

4

2 回答 2

1

您没有在两个活动中初始化sTipsHour字符串。所以用任何常量值初始化 sTipShour字符串,就像在这两个活动中一样:

String sTip="sTip",sHour="sHour";

并且从 TipBookActivity 你没有将意图传递给 startActivity 所以首先声明 Intent i 全局然后启动你的 Activity 为:

   TextView textTip,textHour,textWage;
    EditText editHour,editTip;
    float wage;
    int precision = 100;
    String sTip,sHour;
    Intent i; // declare here

    @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));
            i = new Intent(TipBookActivity.this,History.class);
            i.putExtras(bTip);
            i.putExtras(bHour);
        }       
    });
}
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
        case R.id.menuHistory:
             startActivity(i));        // start Activity here by passing intent    
             return true;
于 2012-07-27T04:21:01.013 回答
0

您遵循的方法是完全错误的,您可以一次将一个 Bundle 放置到意图中,并且您正在放置两个 Bundle,sTip 和 sHours。

第二个捆绑 sHours 将覆盖第一个,我认为它是空指针异常的主要原因,而不是您应该将所有值(在您的情况下为两个字符串数组)放到一个捆绑中。并将该捆绑包放入 Intent。

执行以下操作:

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="sTip";
String sHour="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 bundle= 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());
            bundle.putStringArray(sTip,new String[] {editTip.getText().toString()});
            bundle.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(bundle);

        }       
    });
}

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="sTip";
String sHour="sHour";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_main);
        Bundle bundle= this.getIntent().getExtras();
        String[] array1 = bundle.getStringArray(sTip);
        String[] array2 = bundle.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);
    }

    }
于 2012-07-27T04:39:37.710 回答