我创建了一个列表视图。并添加了一个OnItemClickListener()'它。我传递了一个意图并通过意图传递了一些数据。
contactListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, ContactInfo.class);
Contact ParseContactID = contactAdapter.getItem(position);
intent.putExtra("ParseContactID", (Parcelable) ParseContactID);
startActivity(intent);
}
});
我正在检索另一个班级的数据。
Intent intent = getIntent();
Bundle cBundle = intent.getExtras();
String ContactInfo = cBundle.getString("ParseContactID");
但是当我尝试单击任何列表时,应用程序崩溃了。这是 image of the app和app crash
日志猫有一些致命的错误,不知道如何修复它们。
04-18 10:33:22.547 3528-3528/cm0573.contactsapp.user.contactsapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: cm0573.contactsapp.user.contactsapp, PID: 3528
java.lang.ClassCastException: cm0573.contactsapp.user.contactsapp.Contact cannot be cast to android.os.Parcelable
at cm0573.contactsapp.user.contactsapp.MainActivity$4.onItemClick(MainActivity.java:135)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
at android.widget.AbsListView$3.run(AbsListView.java:3638)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
HELP!
答案 0 :(得分:0)
在您的班级Contact
中,它实现了Serializable
。
public class Contact implements Serializable{
}
答案 1 :(得分:0)
您正在将一个Contact
对象放入Extra中,但在您解压缩时,您正试图获得String
。 Contact
对象和String
对象不一样。正确?
您可以让您的Contact类实现Serializable
接口,之后,您可以执行此操作。
Bundle bundle = new Bundle();
bundle.putSerializable("ParseContactID", ParseContactID);
intent.putExtras(bundle);
检索另一个类中的数据
Bundle bundle = getIntent().getExtras();
Contact contact = (Contact)bundle.getSerializable("ParseContactID");
或者,如果您要实施parcelable
,可以阅读This
public class ContactInfo extends AppCompatActivity {
TextView ContactInfoFamily_Name;
TextView ContactInfoFirst_Name;
TextView ContactInfoHouse_Number_ ;
TextView ContactInfoStreet_Name;
TextView ContactInfoTown_;
TextView ContactInfoCounty_;
TextView ContactInfoPost_Code;
TextView ContactInfoPhone_Number;
DatabaseHandler dbHandler = new DatabaseHandler(getApplicationContext());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_info);
ContactInfoFamily_Name = (TextView) findViewById(R.id.ContactInfoFamilyName);
ContactInfoFirst_Name = (TextView) findViewById(R.id.ContactInfoFirstName);
ContactInfoHouse_Number_ = (TextView) findViewById(R.id.ContactInfoHouseNumber);
ContactInfoStreet_Name = (TextView) findViewById(R.id.ContactInfoStreetName);
ContactInfoTown_ = (TextView) findViewById(R.id.ContactInfoTown);
ContactInfoCounty_ = (TextView) findViewById(R.id.ContactInfoCounty);
ContactInfoPost_Code = (TextView) findViewById(R.id.ContactInfoPostCode);
ContactInfoPhone_Number = (TextView) findViewById(R.id.ContactInfoPhone);
Intent intent = getIntent();
Bundle cBundle = intent.getExtras();
Contact ContactInfo = (Contact) cBundle.getSerializable("ParseContactID");
ContactInfoFamily_Name.setText(ContactInfo.getID());
ContactInfoFirst_Name.setText(getIntent().getExtras().getString("FirstNameKey"));
ContactInfoHouse_Number_.setText(getIntent().getExtras().getString("HouseNumberKey"));
ContactInfoStreet_Name.setText(getIntent().getExtras().getString("StreetNameKey"));
ContactInfoTown_.setText(getIntent().getExtras().getString("TownKey"));
ContactInfoCounty_.setText(getIntent().getExtras().getString("CountyKey"));
ContactInfoPost_Code.setText(getIntent().getExtras().getString("TownKey"));
ContactInfoPhone_Number.setText(getIntent().getExtras().getString("PhoneNumberKey"));
}
}
答案 2 :(得分:0)
确保您的Contact
班级实施Parcelable
。
public class Contact implements Parcelable {
...........
...............
}
将Contact
发送至ContactInfo
活动,如下所示:
Intent intent = new Intent(MainActivity.this, ContactInfo.class);
Contact contact = contactAdapter.getItem(position);
intent.putExtra("ParseContactID", contact);
startActivity(intent);
检索Contact
活动中的ContactInfo
。
Contact contact = (Contact) getIntent().getParcelableExtra("ParseContactID");
关于使用Parcelable
,<{3}}很好。
希望这会有所帮助〜
答案 3 :(得分:0)
ContactInfo类 -
package cm0573.contactsapp.user.contactsapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ContactInfo extends AppCompatActivity {
TextView ContactInfoFamily_Name = (TextView) findViewById(R.id.ContactInfoFamilyName);
TextView ContactInfoFirst_Name = (TextView) findViewById(R.id.ContactInfoFirstName);
TextView ContactInfoHouse_Number_ = (TextView) findViewById(R.id.ContactInfoHouseNumber);
TextView ContactInfoStreet_Name = (TextView) findViewById(R.id.ContactInfoStreetName);
TextView ContactInfoTown_ = (TextView) findViewById(R.id.ContactInfoTown);
TextView ContactInfoCounty_ = (TextView) findViewById(R.id.ContactInfoCounty);
TextView ContactInfoPost_Code = (TextView) findViewById(R.id.ContactInfoPostCode);
TextView ContactInfoPhone_Number = (TextView) findViewById(R.id.ContactInfoPhone);
DatabaseHandler dbHandler = new DatabaseHandler(getApplicationContext());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_info);
Intent intent = getIntent();
Bundle cBundle = intent.getExtras();
Contact ContactInfo = (Contact) cBundle.getSerializable("ParseContactID");
ContactInfoFamily_Name.setText(ContactInfo.getID());
ContactInfoFirst_Name.setText(getIntent().getExtras().getString("FirstNameKey"));
ContactInfoHouse_Number_.setText(getIntent().getExtras().getString("HouseNumberKey"));
ContactInfoStreet_Name.setText(getIntent().getExtras().getString("StreetNameKey"));
ContactInfoTown_.setText(getIntent().getExtras().getString("TownKey"));
ContactInfoCounty_.setText(getIntent().getExtras().getString("CountyKey"));
ContactInfoPost_Code.setText(getIntent().getExtras().getString("TownKey"));
ContactInfoPhone_Number.setText(getIntent().getExtras().getString("PhoneNumberKey"));
}
}