1

値をアクティビティに渡すために次のコードを書きました

  Intent intent = new Intent(DetailActivity.this,
                        MyMapActivity.class);
        intent.putExtra("tourguide.intent.extra.isbusiness", false);

        startActivity(intent);

そして、受信側には次のコードがあります

boolean IsBusiness = false;
    IsBusiness = getIntent().getParcelableExtra(
        "tourguide.intent.extra.isbusiness");

NullPointerException エラーが発生しています

次のように CASTING でも試しました

 IsBusiness = (boolean)getIntent().getParcelableExtra(
        "tourguide.intent.extra.isbusiness");

しかし、役に立たない。:(

4

4 に答える 4

3

試す

  getIntent().getBooleanExtra(name, defaultValue);

何かのようなもの、

 IsBusiness = getIntent().getBooleanExtra("tourguide.intent.extra.isbusiness", false);
于 2012-12-31T09:30:04.697 に答える
1

まず、getIntent()メソッドを使用して、アクティビティを開始したインテントを取得します。

Intent intent = getIntent();

IsBusiness = intent.getBooleanExtra("mauritiustourguide.intent.extra.isbusiness");
于 2012-12-31T09:29:43.813 に答える
1

文字列の代わりにブール値を渡すだけです。小包の必要はありません。あなたの場合

Intent intent = new Intent(DetailActivity.this,
                        TourMapActivity.class);
intent.putExtra("isBusiness", false);

startActivity(intent);

Parselable メカニズムを使用して、エンティティをプレーンな型にシリアル化して渡すことができます。

Parselable を使用するための Google フォームの最初の例を次に示します。

public class ObjectA は Parcelable を実装します {

private String strValue;
private Integer intValue;

/**
 * Standard basic constructor for non-parcel
 * object creation
 */
public ObjectA() { ; };

/**
 *
 * Constructor to use when re-constructing object
 * from a parcel
 *
 * @param in a parcel from which to read this object
 */
public ObjectA(Parcel in) {
    readFromParcel(in);
}

/**
 * standard getter
 *
 * @return strValue
 */
public String getStrValue() {
    return strValue;
}

/**
 * Standard setter
 *
 * @param strValue
 */
public void setStrValue(String strValue) {
    this.strValue = strValue;
}

/**
 * standard getter
 *
 * @return
 */
public Integer getIntValue() {
    return intValue;
}

/**
 * Standard setter
 *
 * @param intValue
 */
public void setIntValue(Integer intValue) {
    this.intValue = intValue;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

    // We just need to write each field into the
    // parcel. When we read from parcel, they
    // will come back in the same order
    dest.writeString(strValue);
    dest.writeInt(intValue);
}

/**
 *
 * Called from the constructor to create this
 * object from a parcel.
 *
 * @param in parcel from which to re-create object
 */
private void readFromParcel(Parcel in) {

    // We just need to read back each
    // field in the order that it was
    // written to the parcel
    strValue = in.readString();
    intValue = in.readInt();
}

/**
 *
 * This field is needed for Android to be able to
 * create new objects, individually or as arrays.
 *
 * This also means that you can use use the default
 * constructor to create the object and use another
 * method to hyrdate it as necessary.
 *
 * I just find it easier to use the constructor.
 * It makes sense for the way my brain thinks ;-)
 *
 */
public static final Parcelable.Creator CREATOR =
    new Parcelable.Creator() {
        public ObjectA createFromParcel(Parcel in) {
            return new ObjectA(in);
        }

        public ObjectA[] newArray(int size) {
            return new ObjectA[size];
        }
    };

}

于 2012-12-31T09:32:07.780 に答える
0

Codeこれはあなたに完全に役立つと思います

アクティビティに渡す:

Intent i = new Intent(getBaseContext(), NameOfActivity.class);
i.putExtra("my_boolean_key", myBooleanVariable);
startActivity(i)

2番目のアクティビティで取得:

Bundle bundle = getIntent().getExtras();
boolean myBooleanVariable = bundle.getBoolean("my_boolean_key");
于 2012-12-31T09:30:22.067 に答える