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

时间:2011-03-21 11:12:04

标签: android android-activity android-intent

我想将活动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);

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

我收到运行时错误

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

5 个答案:

答案 0 :(得分:4)

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

Bundle b = getIntent();

答案 1 :(得分:1)

无需使用捆绑包,只需使用:

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

你会得到你的结果

答案 2 :(得分:0)

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

使用此

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

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

答案 3 :(得分:0)

我已经放弃尝试跟踪多个名称/值对。您可以创建一个不可变类(或使用Parcel):

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

JAL

答案 4 :(得分:0)

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