小编Sim*_*n k的帖子

使用广播接收器在应用程序之间发送可序列化对象

我试图通过广播接收器将一个实现“可序列化”的对象从一个应用程序发送到另一个应用程序。

我想发送一个类,如:

public class MyObject implements Serializable{
    //basic properties
}
Run Code Online (Sandbox Code Playgroud)

在应用程序 A 中,我这样做:

Intent i = new Intent("myApplication.string");
Bundle b = new Bundle();
b.putSerializable("myclass",obj);
i.putExtras(b);
sendBroadcast(i);
Run Code Online (Sandbox Code Playgroud)

当我调试这个时,我可以验证对象是否正确存储在意图中的包中。

在应用程序 B 中,我这样做:

显现

 <receiver
        android:name="com.example.myapplication.myreceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="myApplication.string"/>
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

广播接收器

@Override
public void onReceive(Context context, Intent intent) {
    try{
        Bundle b = intent.getExtras();
        MyObject s = (MyObject)b.getSerializable("myclass");
    }catch (Exception e){
        Log.d(TAG,e.getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)

在应用程序 B 中,意图不保存它在应用程序 A 中所做的数据。

当我尝试投射数据时,它会抛出:

Parcelable encountered ClassNotFoundException reading a Serializable object (name = com.example.myApplication.MyObject) …
Run Code Online (Sandbox Code Playgroud)

java android

3
推荐指数
1
解决办法
1325
查看次数

iOS在Swift中以编程方式添加约束

所以,我已经完成了使用IBin Xcode并希望UI在Swift中编写所有内容.

所以我做的是:

  • 创建一个新UIView的包含我想写的元素 - 让我们称之为"TestView"
  • 我已添加TestView到a VC作为子视图.
  • TestView课堂上我添加了这样的元素:

    class TestView: UIView {
          var someLabel:UILabel!
          override init(frame: CGRect) {
                super.init(frame: frame)
    
                self.someLabel = UILabel(frame: CGRect(x: self.frame.midX, y: oneSixthHeight, width: 100, height: 22))
                self.someLabel.text = "test"
    
                var constraints:[NSLayoutConstraint] = []
                self.someLabel.translatesAutoresizingMaskIntoConstraints = false
                let rightsideAnchor:NSLayoutConstraint = NSLayoutConstraint(item: self.someLabel, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 1)
    
                 constraints.append(rightsideAnchor)
                 NSLayoutConstraint.activateConstraints(constraints)
          }
    }
    
    Run Code Online (Sandbox Code Playgroud)

有了这个,我希望将UILabel它固定在视图的右侧.

但是,我确实收到此错误:

由于未捕获的异常'NSGenericException'而终止应用程序,原因:'无法激活带有项的约束>和>因为它们没有共同的祖先. …

ios nslayoutconstraint swift

0
推荐指数
2
解决办法
7236
查看次数

身份用户管理器DeleteAsync DbUpdateConcurrencyException

我试图通过webapi后面的aspnetcore.identity UserManager删除用户。

    [HttpPost("Delete", Name = "DeleteRoute")]
    [Authorize(Roles = "SuperUser")]
    public async Task<IActionResult> DeleteAsync([FromBody] User user)
    {
        Console.WriteLine("Deleting user: " + user.Id);
        try {
            await _userManager.DeleteAsync(user);
            return Ok();
        } catch(Exception e) {
            return BadRequest(e.Message);
        }

    }
Run Code Online (Sandbox Code Playgroud)

这引发了 DbUpdateConcurrencyException

   Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32 commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected)
   at …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core asp.net-core asp.net-core-identity

0
推荐指数
1
解决办法
428
查看次数