传递对象在acitivity intent中返回null

时间:2017-05-16 07:58:20

标签: android android-intent android-activity bundle

我正在尝试通过意图将活动数据发送到其他人。发送字符串整数非常正常。但是当我在同一意图中添加 Parecelable 对象时,对于字符串整数,它总是返回 null 对象即可。以下是我将数据设置为意图的代码。

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("SomeText", "sometext");
intent.putExtra("itemId", itemId);
intent.putExtra("someuser",user);

startActivity(intent);

在其他活动中,我得到的价值为:

String som = getIntent().getStringExtra("SomeText");
int itemird = getIntent().getIntExtra("itemId", 0);
User user = getIntent().getParcelableExtra("someuser");

我还尝试了thisthis以及this

等不同的其他解决方案

这里有人可以告诉我我的错误或更好的解决方案吗?

所以这是我的Parcelable类

public class User implements Parcelable{
    private int mData;
    private String username;
    private String imagePath;
    private String userId;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public String getImagePath() {
        return imagePath;

    }

    public User() {

    }

    public User(String username, String userId) {
        this.username = username;
        this.userId = userId;
    }
    public User(String username) {
        this.username = username;
    }
    @Override
    public int describeContents() {
        return 0;
    }

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

    }

    public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

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

    private User(Parcel in) {
        mData = in.readInt();
    }

}

7 个答案:

答案 0 :(得分:0)

仔细阅读thisJeremy Logan回答。你应该:

1)为您的Parcelable课程实施User;

2)UserUser user = (User) getIntent().getParcelableExtra("someuser")类的演员表,如intent.putExtra("someuser", (Parcelable)user)$(".newsPostContainer").find('p').each(function(){ if(!$(this).hasClass('postDate')) $(this).addClass('posteExcerpt'); });

答案 1 :(得分:0)

像这样的结构传递对象

Bundle b = getIntent().getExtras();
if(b != null) {
User user = b.getParcelable("someuser");   
}

然后收到这样的

var markerCluster = new MarkerClusterer(map, markers);

markerCluster.clearMarkers();

答案 2 :(得分:0)

创建一个Parcelable类

Field tableField = ? // Here I should somehow cast 'this' to type Field
Class annotationValue = tableField.getAnnotation(MyAnnotation.class).value();

答案 3 :(得分:0)

尝试使用此解决方案并更改您的User类,如下所示:

public class User implements Parcelable {
private int mData;
private String username;
private String imagePath;
private String userId;

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public void setImagePath(String imagePath) {
    this.imagePath = imagePath;
}

public String getImagePath() {
    return imagePath;

}

public User() {
}

public User(String username, String userId) {
    this.username = username;
    this.userId = userId;
}
public User(String username) {
    this.username = username;
}
@Override
public int describeContents() {
    return 0;
}

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

    dest.writeInt(mData);
    dest.writeString(username);
    dest.writeString(imagePath);
    dest.writeString(userId);
}

private void readFromParcel(Parcel in ) {

    mData     = in .readInt();
    username  = in .readString();
    imagePath = in .readString();
    userId    = in .readString();
}

public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
    public User createFromParcel(Parcel in) {
        return new User(in);
    }

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

    private User(Parcel in) {
        readFromParcel( in );
    }

}

答案 4 :(得分:0)

我认为你没有正确实现你的用户模型类。 你可以试试这个

public class User implements Parcelable {

private int mData;
private String username;
private String imagePath;
private String userId;

public User(String username, String userId) {
    this.username = username;
    this.userId = userId;
}
public User(String username) {
    this.username = username;
}
public User() {} // empty constructor

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public void setImagePath(String imagePath) {
    this.imagePath = imagePath;
}

public String getImagePath() {
    return imagePath;

}

@Override
public int describeContents() {
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(mData);
    dest.writeString(username);
    dest.writeString(imagePath);
    dest.writeString(userId);
}

private User(Parcel in) {
    mData     = in.readInt();
    username  = in.readString();
    imagePath = in.readString();
    userId    = in.readString();.
}

public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
    public User createFromParcel(Parcel in) {
        return new User(in);
    }

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

}

然后在您的Splash活动中,您可以执行此操作。我假设您已经创建了一个要发送给MainActivity的用户对象

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("someuser",user); // using the (String name, Parcelable value) overload! 
intent.putExtra("SomeText", "sometext");
intent.putExtra("itemId", itemId);
intent.putExtra(bundle);

startActivity(intent);

然后您可以在MainActivity中接收它,就像这样

User user = (User) getIntent().getParcelableExtra("someuser");

答案 5 :(得分:0)

如果可序列化易于使用且易于理解和实施,您可以使用parcelable

在您的课程中实现serializable

Error: Error in ./MyApp class MyApp - inline template:17:0 caused by: this._config.plt.isRTL is not a function
TypeError: this._config.plt.isRTL is not a function
 at NavControllerBase._transitionInit (webpack:///~/ionic-angular/navigation/nav-controller-base.js:633:0 <- karma-test-shim.js:46642:37)
 at NavControllerBase._postViewInit (webpack:///~/ionic-angular/navigation/nav-controller-base.js:511:0 <- karma-test-shim.js:46520:14)
 at NavControllerBase._viewTest (webpack:///~/ionic-angular/navigation/nav-controller-base.js:607:0 <- karma-test-shim.js:46616:25)
 at NavControllerBase._nextTrns (webpack:///~/ionic-angular/navigation/nav-controller-base.js:350:0 <- karma-test-shim.js:46359:25)
 at NavControllerBase._queueTrns (webpack:///~/ionic-angular/navigation/nav-controller-base.js:300:0 <- karma-test-shim.js:46309:14)
 at NavControllerBase.push (webpack:///~/ionic-angular/navigation/nav-controller-base.js:73:0 <- karma-test-shim.js:46082:21)
 at Nav.ngAfterViewInit (webpack:///~/ionic-angular/components/nav/nav.js:116:0 <- karma-test-shim.js:64546:18)
 at CompiledTemplate.proxyViewClass.View_MyApp0.detectChangesInternal (/DynamicTestModule/MyApp/component.ngfactory.js:298:63)
 at CompiledTemplate.proxyViewClass.AppView.detectChanges (webpack:///~/@angular/core/src/linker/view.js:425:0 <- karma-test-shim.js:119333:14)
 at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (webpack:///~/@angular/core/src/linker/view.js:620:0 <- karma-test-shim.js:119528:44)
 at CompiledTemplate.proxyViewClass.AppView.internalDetectChanges (webpack:///~/@angular/core/src/linker/view.js:410:0 <- karma-test-shim.js:119318:18)
 at CompiledTemplate.proxyViewClass.View_MyApp_Host0.detectChangesInternal (/DynamicTestModule/MyApp/host.ngfactory.js:35:19)
 at CompiledTemplate.proxyViewClass.AppView.detectChanges (webpack:///~/@angular/core/src/linker/view.js:425:0 <- karma-test-shim.js:119333:14)
 at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (webpack:///~/@angular/core/src/linker/view.js:620:0 <- karma-test-shim.js:119528:44)
 at ViewRef_.detectChanges (webpack:///~/@angular/core/src/linker/view_ref.js:170:0 <- karma-test-shim.js:80579:20)
 at ComponentFixture._tick (webpack:///~/@angular/core/bundles/core-testing.umd.js:196:0 <- karma-test-shim.js:17119:36)
 at webpack:///~/@angular/core/bundles/core-testing.umd.js:210:45 <- karma-test-shim.js:17133:53
 at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:229:0 <- karma-test-shim.js:106054:26)
 at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:79:0 <- karma-test-shim.js:105721:39)
 at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:228:0 <- karma-test-shim.js:106053:32)
 at Object.onInvoke (webpack:///~/@angular/core/src/zone/ng_zone.js:273:0 <- karma-test-shim.js:39578:37)
 at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:228:0 <- karma-test-shim.js:106053:32)
 at Zone.run (webpack:///~/zone.js/dist/zone.js:113:0 <- karma-test-shim.js:105938:43)
 at NgZone.run (webpack:///~/@angular/core/src/zone/ng_zone.js:142:42 <- karma-test-shim.js:39447:62)
 at ComponentFixture.detectChanges (webpack:///~/@angular/core/bundles/core-testing.umd.js:210:0 <- karma-test-shim.js:17133:29)
 at Object.<anonymous> (webpack:///src/app/app.component.spec.ts:62:14 <- karma-test-shim.js:135767:17)
 at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:229:0 <- karma-test-shim.js:106054:26)
 at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:79:0 <- karma-test-shim.js:105721:39)
 at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:228:0 <- karma-test-shim.js:106053:32)
 at Zone.run (webpack:///~/zone.js/dist/zone.js:113:0 <- karma-test-shim.js:105938:43)
 at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:102:0 <- karma-test-shim.js:105436:34)
 at webpack:///~/@angular/core/bundles/core-testing.umd.js:96:0 <- karma-test-shim.js:17019:21
 at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:229:0 <- karma-test-shim.js:106054:26)
 at AsyncTestZoneSpec.onInvoke (webpack:///~/zone.js/dist/async-test.js:49:0 <- karma-test-shim.js:105026:39)
 at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:76:0 <- karma-test-shim.js:105718:39)
 at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:228:0 <- karma-test-shim.js:106053:32)
 at Zone.run (webpack:///~/zone.js/dist/zone.js:113:0 <- karma-test-shim.js:105938:43)
 at AsyncTestZoneSpec._finishCallback (webpack:///~/@angular/core/bundles/core-testing.umd.js:91:0 <- karma-test-shim.js:17014:29)
 at webpack:///~/zone.js/dist/async-test.js:38:0 <- karma-test-shim.js:105015:31
 at ZoneDelegate.invokeTask (webpack:///~/zone.js/dist/zone.js:262:0 <- karma-test-shim.js:106087:35)
 at Zone.runTask (webpack:///~/zone.js/dist/zone.js:151:0 <- karma-test-shim.js:105976:47)
 at ZoneTask.invoke (webpack:///~/zone.js/dist/zone.js:332:0 <- karma-test-shim.js:106157:33)
 at data.args.(anonymous function) (webpack:///~/zone.js/dist/zone.js:1149:0 <- karma-test-shim.js:106974:25)

然后你可以在意图中传递这个对象

public class Place implements Serializable {
        private int id;
        private String name;

        public void setId(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    } 

在第二个活动中,你可以获得这样的数据

     Intent intent = new Intent(this, SecondAct.class);
     intent.putExtra("PLACE", Place);
     startActivity();
当有大量数据需要发送时,使用

parcelable可以使用可序列化

如果不能让我知道

答案 6 :(得分:0)

我尝试了所有解决方案,但没有人提到此解决方案,writeToParcel()有问题。包裹的读写顺序应相同。