블 로그 출처:http://blog.csdn.net/liuxian13183출처All Rights Reserved !
여러분 이 listview 전달 대상 이나 activity 간 대상 의 전달 을 잘못 처리 하면 Exception 이 나타 납 니 다.
우 리 는 다음 과 같은 방법 으로 해결 할 수 있다.
방법 1: List < String > 이나 List < Integer > 만 전달 하면 바로 사용 할 수 있 습 니 다. intent.putStringArrayListExtra(name, value) intent.putIntegerArrayListExtra(name, value) 방법 2: List < Object > 를 전달 하면 list 를 Serializable 형식 으로 강하 게 바 꿀 수 있 으 며, 동시에 Object 를 전달 하 는 것 도 Serializable 인터페이스 구 조 를 실현 한 다음 에 통과 해 야 합 니 다. putExtras(key, (Serializable)list) 방법 전달 (List
) getIntent().getSerializable(key) List < Your Object > 데 이 터 를 받 았 습 니 다. 방법 3: 하 나 는 Serializable 인 터 페 이 스 를 실현 하 는 것 입 니 다. 간단 한 Bundle. putSerializable (Key, Object) 입 니 다. 다른 하 나 는... Parcelable 인터페이스 구현 Bundle.putParcelable(Key, Object); 코드 는 다음 과 같 습 니 다:public class Bean implements Parcelable {
private int id;
private String username;
private String password;
public static final Parcelable.Creator<Bean> CREATOR = new Creator<Bean>() {
@Override
public Bean createFromParcel(Parcel source) {
Bean bean = new Bean();
bean.id = source.readInt();
bean.username = source.readString();
bean.password = source.readString();
return bean;
}
@Override
public Bean[] newArray(int size) {
return new Bean[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeString(username);
dest.writeString(password);
}
}
직렬 화:public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public int age;
public String name;
}
발송: Student stu = new Student();
stu.name = tmall_name.getText().toString();
stu.age = Integer.valueOf(tmall_age.getText().toString());
Intent intent = new Intent();
intent.setClass(getApplicationContext(), StudentInfo.class);
intent.putExtra("student", stu);
startActivity(intent);
수신: Intent intent = this.getIntent();
Student stu = (Student) intent.getSerializableExtra("student");
student_name.setText(stu.name);
student_age.setText(stu.age+"");
방법 5: intent 로 전달 되 는 게 불편 해서 애플 리 케 이 션 에 있 는 전역 데 이 터 를 쓸 수 있 습 니 다. 1. 자신 만 의 android. app. application 하위 클래스 만 들 기 2. manifest 에서 이 종 류 를 설명 합 니 다. 3. 이때 안 드 로 이 드 는 이 를 위해 전역 적 으로 사용 할 수 있 는 인 스 턴 스 를 만 듭 니 다. 다른 곳 에서 Context. getapplicationContext () 방법 으로 이 인 스 턴 스 를 가 져 와 상태 (변수) 를 가 져 올 수 있 습 니 다. 상속 신청 class MyApp extends Application { private String myState; public String getState(){ return myState; } public void setState(String s){ myState = s; } } AndroidManifest. xml 의 설정 에 대해 서 는 애플 리 케 이 션 에 name 을 직접 추가 하면 됩 니 다. 아래 와 같 습 니 다. android:icon="@drawable/icon" android:label="@string/app_name"> 쓰다 class Blah extends Activity { @Override public void onCreate(Bundle b){ ... MyApp appState = ((MyApp)getApplicationContext()); String state = appState.getState(); ... } } 감사합니다!
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부
지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다.
유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.