单击列表视图时,应用程序崩溃!为什么?

When the list view is clicked, the application crashes! Why?

我创建了一个列表视图。并为其添加了 '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);
     }
 });

我正在另一个 class 中检索数据。

    Intent intent = getIntent();
    Bundle cBundle = intent.getExtras();

    String ContactInfo = cBundle.getString("ParseContactID");

但是当我尝试单击任何列表时,应用程序崩溃了。这里是 image of the app and 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.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.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)

求助!

在您的 class Contact 中实现了 Serializable.

public class Contact implements Serializable{ 

}

您正在将一个 Contact 对象放入 Extra,但是当您提取时,您试图获得一个 StringContact 对象和 String 对象不一样。对吗?

您可以让您的联系人 class 实现 Serializable 接口,然后,您可以执行此操作。

Bundle bundle = new Bundle();
bundle.putSerializable("ParseContactID", ParseContactID);
intent.putExtras(bundle);

正在另一个中检索数据class

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"));


}
}
  1. 确保您的 Contact class 实现了 Parcelable.

    public class Contact implements Parcelable {
    
        ...........
        ............... 
    }
    
  2. 发送ContactContactInfoActivity如下:

    Intent intent = new Intent(MainActivity.this, ContactInfo.class);
    Contact contact = contactAdapter.getItem(position);
    
    intent.putExtra("ParseContactID", contact);
    startActivity(intent);
    
  3. 正在检索 ContactInfo Activity 中的 Contact

    Contact contact = (Contact) getIntent().getParcelableExtra("ParseContactID");
    

Tutorial 关于使用 Parcelable

希望对你有所帮助~

联系信息class -

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"));


}
}