0

我有一个小问题:我有 3 个活动。活动列表类.java

public class ListClass extends Activity {

......


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            onItemClick1(parent, view, position, id);

        }

    });

}

public void onItemClick1(AdapterView<?> parent, View view, int position,
        long id) {

    String name = (String) parent.getItemAtPosition(position);
    String address = (String) listAddress.get(position);
    int status = (int) listStatus.get(position);
    double lat = (double) listLat.get(position);
    double lng = (double) listLng.get(position);
    String serialNumber = (String) listSerialNumber.get(position);

    getIntent().putExtra("name", name);
    getIntent().putExtra("address", address);
    getIntent().putExtra("status", status);
    getIntent().putExtra("latitude", lat);
    getIntent().putExtra("longitude", lng);
    getIntent().putExtra("serialNumber", serialNumber);

    startActivity(getIntent());

}

public Intent getIntent() {
    Intent intent = new Intent(ListClass.this, ClassB.class);
    return intent;
}

如您所见,我在 ClassB.java 上使用了额外的值:

public class ClassB extends Activity {

TextView textViewTitle;
TextView textView2;
TextView textViewInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.charge_box);
    this.textViewTitle = (TextView) findViewById(R.id.charge_box__textView1);
    this.textView2 = (TextView) findViewById(R.id.charge_box__textView2);
    this.textViewInfo = (TextView) findViewById(R.id.charge_box__textView3);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    Bundle bundle = getIntent().getExtras();
    String name = bundle.getString("name");
        String address = bundle.getString("address");
        int status = bundle.getInt("status");
        textViewTitle.setText(name);
        textViewInfo.setText(address + " " + status);

    }
}

到目前为止,一切都很好!但我需要从 MainClass.java 的另一个活动启动 ActivityB。

    Intent intent = new Intent(MainActivity.this,
                    ClassB.class);

            startActivity(intent);

问题是当 ClassB 使用时,Bundle bundle = getIntent().getExtras();他没有找到“额外”,因为 MainClass 的 Intent 与 ListClass 的 Intent 不同!我能怎么做??我可以决定使用哪个 Intent 吗?谢谢你!

编辑:@skygeek 好的,我正在处理它,但是如果额外的值发生变化,我该如何创建一个静态变量?

public void onItemClick1(AdapterView<?> parent, View view, int position,
        long id) {

    String name = (String) parent.getItemAtPosition(position);
    String address = (String) listAddress.get(position);
    int status = (int) listStatus.get(position);
    double lat = (double) listLat.get(position);
    double lng = (double) listLng.get(position);
    String serialNumber = (String) listSerialNumber.get(position);

    Intent intent = new Intent(ListClass.this, ChargeBoxInfoActivity.class);
    intent.putExtra("name", name);
    intent.putExtra("address", address);
    intent.putExtra("status", status);
    intent.putExtra("latitude", lat);
    intent.putExtra("longitude", lng);
    intent.putExtra("serialNumber", serialNumber);
    startActivity(intent);
}
4

4 回答 4

2

检查 ClassB 的 onCreate 方法

     if(getIntent().getExtras() != null)
     {
     // do your functionality with extras
        Bundle bundle = getIntent().getExtras();
        String name = bundle.getString("name");
        String address = bundle.getString("address");
        int status = bundle.getInt("status");
        textViewTitle.setText(name);
        textViewInfo.setText(address + " " + status);
     }

     else
      {
       // do whatever you want to do
      }
于 2013-06-20T09:51:46.713 回答
0

首次调用活动时创建具有静态字段意图的静态对象(检查其是否为空)。因此,下次您从另一个类调用相同的活动时,请使用具有已设置意图的相同静态对象再次调用该活动,因此您会发现这些额外内容!

于 2013-06-20T09:51:22.870 回答
0

这是错误的:

getIntent().putExtra("name", name);
getIntent().putExtra("address", address);
getIntent().putExtra("status", status);
getIntent().putExtra("latitude", lat);
getIntent().putExtra("longitude", lng);
getIntent().putExtra("serialNumber", serialNumber);
startActivity(getIntent());

}

public Intent getIntent() {
Intent intent = new Intent(ListClass.this, ClassB.class);
return intent;  

}

您在每个函数调用 getIntent() 后创建新意图...试试这个:

Intent intent = new Intent(ListClass.this, ClassB.class);
intent.putExtra("name", name);
intent.putExtra("address", address);
intent.putExtra("status", status);
intent.putExtra("latitude", lat);
intent.putExtra("longitude", lng);
intent.putExtra("serialNumber", serialNumber);
startActivity(intent);
于 2013-06-20T10:20:35.133 回答
0

我明白...试试这个:

try{
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
    String address = bundle.getString("address");
    int status = bundle.getInt("status");
    textViewTitle.setText(name);
    textViewInfo.setText(address + " " + status);
}}catch (Exception ignored) {
}
于 2013-06-20T11:01:26.130 回答