会议室:无法启动活动,将ID添加到意图

时间:2018-06-18 17:42:42

标签: java android android-room

这是我的主要活动。我有一个包含成员的列表。我想点击一个客户并启动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:unflatten:registerBuffer failed:未知错误-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:unflatten:registerBuffer failed:未知错误-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中的代码-6(RenderThread)

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

2 个答案:

答案 0 :(得分:1)

您在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);

答案 1 :(得分:0)

当会员点击

时,您不会调用活动
public void onMemberClick(int position) {
    Log.i("what", "gotpositionroger");
    Member member = mMember.get(position);
    Log.i("the", "aftergotpositionroger");

  Intent intent=new Intent(getApplicationContext(),MemberInfo.class);
  intent.putExtra("id",member.getId());
  startActivity(intent);
   }

在Meminfoactivity中你应该这样做

 Intent intent = getIntent();
if (intent != null && intent.hasExtra("MemberID")) {
    long memberid = intent.getLongExtra("MemberID", -1);

   // intent.putExtra("MemberID", memberid);
   //you cant able to start the activity without new Intent(thid,Example.class)
   // startActivity(intent);
}
else {
    Toast.makeText(
            getApplicationContext(),
            R.string.empty_not_saved,
            Toast.LENGTH_LONG).show();
}