problem with sending multiple data between activities android
i want to send multiple data from activity WelcomeScreen to OpenBook here is my code
Sending
String a="pg558.sqlite";
String b="pg558";
Intent intent = new Intent(getApplicationContext(), OpenBook.class);
intent.putExtra("db_name",a);
intent.putExtra("book_name",b);
intent.putExtra("chapter_number",3);
intent.putExtra("page_number",1);
startActivity(intent);
Recieving
Bundle b = getIntent().getExtras();
DB_NAME= b.getString("db_name");
BOOK_NAME= b.getString("book_name");
CHAPTER_NUMBER= b.getInt("chapter_number",1);
PAGE_NUMBER= b.getInt("page_number",1);
i'm getting runtime error
03-21 开发者_C百科16:29:34.989: ERROR/AndroidRuntime(10651): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mireader/com.mireader.OpenBook}: java.lang.NullPointerException
try Intent intent = new Intent(this, OpenBook.class);
and Bundle b = getIntent();
no need to use bundle, just use this:
Receiving:
Intent i=getintent();
String ab=i.getextra("db_name");
String bc=i.getextra("book_name");
....
you will get your result
in line Intent intent = new Intent(getApplicationContext(), OpenBook.class);
use this
Intent intent = new Intent(youractivity.class, OpenBook.class);
instead of using getApplicationContext() use MyActivityClass.this
I have given up on trying to keep track of multiple name/value pairs. You can create an immutable class (or use Parcels) as:
public final class PasswordState implements Serializable {
private static final long serialVersionUID = 1L;
public static final int MIN_PASSWORD_LENGTH= 8;
public final int lengthKey; // in bytes
public final long timeExpire; // in milliseconds as a Calendar object
public final boolean isValidKey;
public final int timeoutType;
public final String password;
public final boolean isHashPassword;
public PasswordState(int lengthKey,
long timeExpire,
boolean isValidKey,
int timeoutType,
String password,
boolean isHashPassword){
this.lengthKey= lengthKey;
this.timeExpire= timeExpire;
this.isValidKey= isValidKey;
this.timeoutType= timeoutType;
this.password= password;
this.isHashPassword= isHashPassword;
}
Then pass this in an intent to the child activity:
private void launchManagePassword() {
Intent i= new Intent(this, ManagePassword.class); // no param constructor
PasswordState outState= new PasswordState(lengthKey,timeExpire,isValidKey,timeoutType,"",model.getIsHashPassword());
Bundle b= new Bundle();
b.putSerializable("jalcomputing.confusetext.PasswordState", outState);
i.putExtras(b);
startActivityForResult(i,REQUEST_MANAGE_PASSWORD); // used for callback
}
Finally, retrieve the stateful object in the child activity:
try {
inPWState= (PasswordState) getIntent().getSerializableExtra("jalcomputing.confusetext.PasswordState");
lengthKey= inPWState.lengthKey;
timeoutType= inPWState.timeoutType;
isValidKey= inPWState.isValidKey;
timeExpire= inPWState.timeExpire;
isHashPassword= inPWState.isHashPassword;
// password= inPWState.password; // not required
} catch(Exception e){
lengthKey= PasswordState.MIN_PASSWORD_LENGTH;
timeoutType= TIMEOUT_NEVER;
isValidKey= true;
timeExpire= LONG_YEAR_MILLIS;
isHashPassword= false;
}
JAL
In your problem, activityclass.class
means the class where you actually called the intent and bundle for sending data to the second activity.
精彩评论