繁体   English   中英

在Android活动之间发送多个数据的问题

[英]problem with sending multiple data between activities android

我想从活动WelcomeScreen向OpenBook发送多个数据,这是我的代码

正在发送


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

接收中


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

我遇到运行时错误

03-21 16:29:34.989: ERROR/AndroidRuntime(10651): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mireader/com.mireader.OpenBook}: java.lang.NullPointerException

尝试Intent intent = new Intent(this, OpenBook.class);

Bundle b = getIntent();

无需使用包,只需使用以下命令:

Receiving:
Intent i=getintent();
String ab=i.getextra("db_name");
String bc=i.getextra("book_name");
....

你会得到你的结果

符合Intent intent = new Intent(getApplicationContext(), OpenBook.class); 用这个

Intent intent = new Intent(youractivity.class, OpenBook.class);

而不是使用getApplicationContext()使用MyActivityClass.this

我已经放弃尝试跟踪多个名称/值对。 您可以按以下方式创建不可变的类(或使用宗地):

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;
    }

然后将其传递给子活动:

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
}

最后,在子活动中检索有状态对象:

     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;
    }

日航

在您的问题中, activityclass.class表示您实际上在其中调用了用于将数据发送到第二个活动的意图和捆绑包的类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM