这是我的主要活动。我有一个由成员组成的清单。我想单击一个客户并启动MemberInfo活动。

public class MemberMainActivity extends AppCompatActivity implements MemberListAdapter.MemberClickListener{

private MemberViewModel mMemberViewModel;
private List<Member> mMember;

void setMember(List<Member> members) {
    mMember = members;
}

public static final int NEW_MEMBER_ACTIVITY_REQUEST_CODE = 1;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_member);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MemberMainActivity.this, NewMemberActivity.class);
            startActivityForResult(intent, NEW_MEMBER_ACTIVITY_REQUEST_CODE);
        }
    });

    RecyclerView recyclerView = findViewById(R.id.recyclerviewcard_member);
    final MemberListAdapter adapter = new MemberListAdapter(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    mMemberViewModel = ViewModelProviders.of(this).get(MemberViewModel.class);

    mMemberViewModel.getAllMember().observe(this, new Observer<List<Member>>() {
        @Override
        public void onChanged(@Nullable final List<Member> members) {
            mMember = members;
            // Update the cached copy of the words in the adapter.
            adapter.setMember(members);
        }
    });

}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == NEW_MEMBER_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
        Member member = new Member(data.getStringExtra(NewMemberActivity.EXTRA_REPLY), data.getStringExtra(NewMemberActivity.EXTRA_REPLY2));
        mMemberViewModel.insert(member);
    } else {
        Toast.makeText(
                getApplicationContext(),
                R.string.empty_not_saved,
                Toast.LENGTH_LONG).show();
    }
}


public void onMemberClick(int position) {
    Log.i("what", "gotpositionroger");
        Member member = mMember.get(position);
        Log.i("the", "aftergotpositionroger");
        MemberInfo.open(this, member.getId());
  }
}


这是我的MemberInfo活动。

public class MemberInfo extends AppCompatActivity {

public static void open(Activity activity, long memberid) {
    Intent intent = new Intent(activity, MemberInfo.class);
    intent.putExtra("MemberID", memberid);
    activity.startActivity(intent);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memberinfo);

    -------------Log.i("okay", "memberinfo");---------------
    Intent intent = getIntent();
    if (intent != null && intent.hasExtra("MemberID")) {
        long memberid = intent.getLongExtra("MemberID", -1);
        // TODO: get customer details based on customer id
        intent.putExtra("MemberID", memberid);
        startActivity(intent);
    }
    else {
        Toast.makeText(
                getApplicationContext(),
                R.string.empty_not_saved,
                Toast.LENGTH_LONG).show();
    }
  }
}


问题是:当我单击一个成员时,它正在加载,几秒钟后,该应用程序崩溃。

在我的MemberInfo活动中,我用Log.i标记了一行。当我单击一个成员时,logcat一次又一次地向我显示相同的Log.i-Log.i会无限重复。

我的应用崩溃后,logcat显示如下:


  06-18 19:36:33.662 5041-5070 / com.example.mainbuchhaltung
  E / GraphicBuffer:展平:registerBuffer失败:未知错误-2
  (2)
  
  06-18 19:36:33.662 5041-5070 / com.example.mainbuchhaltung E / Surface:
  dequeueBuffer:IGraphicBufferProducer :: requestBuffer失败:2
  
  06-18 19:36:33.662 5041-5070 / com.example.mainbuchhaltung
  E / EGL_emulation:tid 5070:swapBuffers(550):错误0x300d
  (EGL_BAD_SURFACE)
  
  06-18 19:36:33.662 5041-5070 / com.example.mainbuchhaltung
  W / OpenGLRenderer:swapBuffers在遇到EGL错误12301
  0x942b8a20,暂停渲染...
  
  06-18 19:36:33.663 5041-5070 / com.example.mainbuchhaltung
  D / EGL_emulation:eglMakeCurrent:0xa33983c0:ver 3 0(tinfo
  0xa322a560)
  
  06-18 19:36:33.729 5041-5070 / com.example.mainbuchhaltung
  E / gralloc_ranchu:map_buffer:无法映射ashmem区域!
      gralloc_register_buffer(0x20a540):映射失败:内存不足
  
  06-18 19:36:33.729 5041-5070 / com.example.mainbuchhaltung
  W / GraphicBufferMapper:importBuffer(0x20a4a0)失败:2
  
  06-18 19:36:33.729 5041-5070 / com.example.mainbuchhaltung
  E / GraphicBuffer:展平:registerBuffer失败:未知错误-2
  (2)
  
  06-18 19:36:33.729 5041-5070 / com.example.mainbuchhaltung E / Surface:
  dequeueBuffer:IGraphicBufferProducer :: requestBuffer失败:2
  
  06-18 19:36:33.729 5041-5070 / com.example.mainbuchhaltung
  E / EGL_emulation:tid 5070:init(382):错误0x3003(EGL_BAD_ALLOC)
      tid 5070:eglCreateWindowSurface(922):错误0x3003(EGL_BAD_ALLOC)
  
  06-18 19:36:33.732 5041-5070 / com.example.mainbuchhaltung
  A / OpenGLRenderer:无法为窗口0xbf1d4008创建EGLSurface,
  eglErr = EGL_BAD_ALLOC
  
  06-18 19:36:33.733 5041-5070 / com.example.mainbuchhaltung A / libc:致命的
  信号6(SIGABRT),TID 5070(RenderThread)中的代码-6


我认为问题出在MemberInfo代码中,但我无法弄清错误。有人可以帮我解决这个问题吗?

最佳答案

您正在使用MemberInfo活动onCreate方法中的“空”意图打开活动:

Intent intent = getIntent();

if (intent != null && intent.hasExtra("MemberID")) {
    long memberid = intent.getLongExtra("MemberID", -1);
    // TODO: get customer details based on customer id
    intent.putExtra("MemberID", memberid);
    startActivity(intent);
}
else {
    Toast.makeText(
            getApplicationContext(),
            R.string.empty_not_saved,
            Toast.LENGTH_LONG).show();
}


从onCreate删除它:

        intent.putExtra("MemberID", memberid);
        startActivity(intent);


启动会员信息活动后,您需要获得的会员ID额外需要传递:

long memberid = intent.getLongExtra("MemberID", -1);

08-18 18:48