【问题标题】:Using parcelable and intent in the same activity在同一个活动中使用 Parcelable 和 Intent
【发布时间】:2013-11-22 20:05:19
【问题描述】:

我在同一个活动中使用了 2 个意图。其中一个用于输入 Firstname、Lastname 和 Id。其他意图用于将数据发送到另一个活动。 但是当我使用类似代码的意图时,它不起作用。 我可以使用这样的意图吗

intent.putExtra("lastName", intent2.getString("lastName"));

搜索活动

Intent intent  = new Intent(getApplicationContext(),SearchActivity.class);
Bundle b=new Bundle();
b.putString("firstName", firstName);
b.putString("lastName", lastName);
b.putString("id", id);
b.putParcelable("LoginPartClass",lp2);
intent.putExtras(b);
startActivity(intent);

搜索活动代码

        final Bundle intent2 = getIntent().getExtras();
        Intent intent = new Intent(getApplicationContext(),AnotherActivity.class);
        intent.putExtra("keywords", keywords);
        if(intent2!=null) {
           final String Firstname=intent2.getString("firstName");
           intent.putExtra("firstName", Firstname);  }
           intent.putExtra("lastName", intent2.getString("lastName"));
           intent.putExtra("id", intent2.getString("id"));
        }
        startActivity(intent);

Parceable 类

public class LoginPart implements Parcelable{

private Token requestoken;
private OAuthService s;
private String authURL;


public LoginPart(Token Token, OAuthService S, String AuthURL) {
    requestoken = Token;
    s = S;
    authURL = AuthURL;
}

public LoginPart( ) {

}


public Token getRequestoken() {
    return requestoken;
}

public void setRequestoken(Token requestoken) {
    this.requestoken = requestoken;
}

public OAuthService getS() {
    return s;
}

public Parcelable getSParcelable() {
    return (Parcelable) s;
}
public void setS(OAuthService s) {
    this.s = s;
}

public String getAuthURL() {
    return authURL;
}

public void setAuthURL(String authURL) {
    this.authURL = authURL;
}

public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeValue(requestoken);
    dest.writeValue(s);
    dest.writeValue(authURL);

}

 }

问题与 Parcable 类有关。当我使用 Parceable 类时会崩溃。我该如何解决?

【问题讨论】:

  • getIntent().getExtras(); 包含上一个活动发送的参数。
  • 您应该重新考虑如何命名变量。 intent2 不是 Intent 而是 Bundle 所以有点令人困惑,恕我直言。你有什么问题?如果它崩溃然后发布你的logcat。如果这是问题,您可以将数据从 Bundle 添加到另一个 Intent
  • 我编辑了问题。因为问题与 parceable 类有关。如果我删除意图它会成功。所以问题与 parceable 类有关

标签: java android android-intent android-activity parcelable


【解决方案1】:

在您的第一个活动中:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    Bundle b = new Bundle();
    b.putInt("id", 1);
    b.putString("firstName", "this is my first name");
    b.putString("lastName", "this is my last name");
    intent.putExtras(b);
    startActivity(intent);

在第二个活动中:

Bundle b =getIntent().getExtras();



    String name  =b.getString("firstName");
    String lastname  =b.getString("lastName");

【讨论】:

  • 我编辑了问题。因为问题与 parceable 类有关。如果我删除意图它会成功。所以问题与 parceable 类有关
  • 你必须使用 putParcelable 的 putExtra insted。像这样 Parcelableobject parcelableobj = new Parcelableobject(); //将Parcelable对象存入Intent intent.putExtra("myparcableobject", parcelableobj);
  • 当我转换为 putParceable 时,IDE 不允许。
  • 使用 putExtra : Intent intent.putExtra("myparcableobject", parcelableobj);
【解决方案2】:

像这样改变你的 LoginPart 类...

public class LoginPart implements Parcelable {
    private Token requestoken;
    private OAuthService s;
    private String authURL;


    public LoginPart(Token Token, OAuthService S, String AuthURL) {
        requestoken = Token;
        s = S;
        authURL = AuthURL;
    }

    public LoginPart( ) {

    }

    public LoginPart(Parcel parcel) {
        requestoken = parcel.readValue(getClassLoader());
        s = parcel.readValue(getClassLoader());
        authURL = parcel.readValue(getClassLoader());
    }


    public Token getRequestoken() {
        return requestoken;
    }

    public void setRequestoken(Token requestoken) {
        this.requestoken = requestoken;
    }

    public OAuthService getS() {
        return s;
    }

    public Parcelable getSParcelable() {
        return (Parcelable) s;
    }
    public void setS(OAuthService s) {
        this.s = s;
    }

    public String getAuthURL() {
        return authURL;
    }

    public void setAuthURL(String authURL) {
        this.authURL = authURL;
    }

    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        dest.writeValue(requestoken);
        dest.writeValue(s);
        dest.writeValue(authURL);
    }

    public static final Parcelable.Creator<LoginPart> CREATOR = new Creator<SecondActivity.LoginPart>() {

        @Override
        public LoginPart[] newArray(int size) {
            return new LoginPart[size];
        }

        @Override
        public LoginPart createFromParcel(Parcel source) {
            return new LoginPart(source);
        }
    };
}

为此,您的 Token 类和 OAuthService 类应该实现 Parcelable

【讨论】:

  • requestoken = parcel.readValue(getClassLoader());这部分有问题。IDE 不允许并在 getClassLoader() 下划线
  • 否则你可以使用getClass().getClassLoader()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
  • 2021-09-23
  • 2019-01-13
  • 2018-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多