无法解析方法putExtra

时间:2017-07-09 03:37:46

标签: android

我正在使用Android Studio中的应用程序,该应用程序使用针对学校的意图传递数据。我已经创建了我的对象来传递数据并启动Intent,但我不断收到警告,说明我的putExtra方法无法解决。有任何想法吗? 提前致谢。

public class MainActivity extends AppCompatActivity {

private ContactInfo contactobject;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Bundle extra = getIntent().getExtras();
    contactobject = (ContactInfo)extra.get("contact");
    if (contactobject == null )
        contactobject = new ContactInfo();

    TextView name1 = (TextView) findViewById(R.id.Name1);
    name1.setText(contactobject.getOneName());

    TextView phone1 = (TextView) findViewById(R.id.Phone1);
    phone1.setText(contactobject.getOnePhone());

    TextView email1 = (TextView) findViewById(R.id.Email1);
    email1.setText(contactobject.getOneEmail());

    TextView kin1 = (TextView) findViewById(R.id.Kin1);
    kin1.setText(contactobject.getOneKin());

    TextView name2 = (TextView) findViewById(R.id.Name2);
    name2.setText(contactobject.getTwoName());

    TextView phone2 = (TextView) findViewById(R.id.Phone2);
    phone2.setText(contactobject.getTwoPhone());

    TextView email2 = (TextView) findViewById(R.id.Email2);
    email2.setText(contactobject.getTwoEmail());

    TextView kin2 = (TextView) findViewById(R.id.Kin2);
    kin2.setText(contactobject.getTwoEmail());

}


public void EditPrimary(View view)
{
    Intent intent1 = new Intent(getApplicationContext(), Edit1.class);
    Intent intent = intent1.putExtra("contact", contactobject);
    startActivity(intent1);
}

}

3 个答案:

答案 0 :(得分:3)

有两点:

第1点:不需要将intent1.putExtra("contact", contactobject);值分配给其他变量

第2点ContactInfo必须实现课程ParcelableSerializable ,同时将其传递给{{ 1}}。

请参阅this post了解如何制作课程Intent

我相信你错过了第二点。

答案 1 :(得分:0)

您无法将putExtra调用分配给新的intent变量:

public void EditPrimary(View view)
{
    Intent intent1 = new Intent(getApplicationContext(), Edit1.class);
    intent1.putExtra("contact", contactobject);
    startActivity(intent1);
}

答案 2 :(得分:0)

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>