0

使用StartActivity我从 FirstActivity 到达 SecondActivity..现在我想访问在前一个活动中声明的对象。

有没有我遗漏的明显机制。我不能在我想传递的对象(DropboxAPI)上使用 Parceable,因为我没有它的源(我不能使它成为 parceable)。

我可以通过FirstActivity.this,因为我可以使用意图使其 Parceable 吗?或喜欢

    Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
    intent.putExtra("MyClass",FirstActivity.this);
    startActivity (intent);

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;

import android.app.Application;
import android.hardware.Camera.Parameters;
import android.os.Parcel;
import android.os.Parcelable;
public class temp extends Application {

    DropboxAPI<AndroidAuthSession> mApi;
    public void onCreate() {  
        super.onCreate();  
    }

    temp(DropboxAPI<AndroidAuthSession> Api)
    {
        mApi=Api;
    }
    public DropboxAPI<AndroidAuthSession> getName() {    
        return mApi;  
} 
    public void setName(DropboxAPI<AndroidAuthSession> dropboxclient) {  
        this.mApi = dropboxclient;  
}
}

一级代码:

t = (temp)getApplication();
                t.setName(mApi);
                //basket.putParcelable("key", MCActivity.this);
                Intent intent=new Intent(MCActivity.this,DownActivity.class);
                //intent.putExtra("MyClass",t);
                startActivity (intent);

二级代码:

public void onCreate(Bundle bun) {

    // TODO Auto-generated method stub
    super.onCreate(bun);
    setContentView(R.layout.download);
    mImage = (ImageView)findViewById(R.id.image_view);
    //Bundle gotit=getIntent().getExtras();
    t = (temp)getApplication();
    int i=2;
    i=5;
    dApi=t.getName();
}

日志猫:

06-28 02:24:31.979: I/System.out(729): debugger has settled (1308)
06-28 02:25:08.387: D/AndroidRuntime(729): Shutting down VM
06-28 02:25:08.387: W/dalvikvm(729): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-28 02:25:08.705: E/AndroidRuntime(729): FATAL EXCEPTION: main
06-28 02:25:08.705: E/AndroidRuntime(729): java.lang.ClassCastException: android.app.Application
06-28 02:25:08.705: E/AndroidRuntime(729):  at cloud.mobile.MCActivity$3.onClick(MCActivity.java:164)
06-28 02:25:08.705: E/AndroidRuntime(729):  at android.view.View.performClick(View.java:2408)
06-28 02:25:08.705: E/AndroidRuntime(729):  at android.view.View$PerformClick.run(View.java:8816)
06-28 02:25:08.705: E/AndroidRuntime(729):  at android.os.Handler.handleCallback(Handler.java:587)
06-28 02:25:08.705: E/AndroidRuntime(729):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 02:25:08.705: E/AndroidRuntime(729):  at android.os.Looper.loop(Looper.java:123)
06-28 02:25:08.705: E/AndroidRuntime(729):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 02:25:08.705: E/AndroidRuntime(729):  at java.lang.reflect.Method.invokeNative(Native Method)
06-28 02:25:08.705: E/AndroidRuntime(729):  at java.lang.reflect.Method.invoke(Method.java:521)
06-28 02:25:08.705: E/AndroidRuntime(729):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-28 02:25:08.705: E/AndroidRuntime(729):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-28 02:25:08.705: E/AndroidRuntime(729):  at dalvik.system.NativeStart.main(Native Method)
4

4 回答 4

2

尝试使用应用程序类:

public class MyApplication extends Application {
    //Variable we want to share to All Activities in Application

    private DropboxClient dropboxclient;  

    @Override 
    public void onCreate() {  
            super.onCreate();  

    }  
    //Getter Method
    public DropboxClient getName() {    
            return dropboxclient;  
    }  
    //Setter Method
    public void setName(DropboxClient dropboxclient) {  
            this.dropboxclient = dropboxclient;  
    }  
}

在 MyFirstActivity 活动中:

private MyApplication app;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        app = (MyApplication)getApplication();  //Get  Application
        app.setName(dropboxclient);
        Intent intent = new Intent(); 
        intent.setClass(this, MySecondActivity.class);  
        startActivity(intent); 

在 MySecondActivity 活动中:

private MyApplication app;  
    @Override 
    protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);   
            app = (MyApplication)getApplication();  //Get  Application
            DropboxClient dropboxclient=app.getName();//Access global value
于 2012-06-27T20:12:05.730 回答
1

请执行下列操作,

开始活动startActivityForResult()

startActivityForResult(0, MyActivity.class);

在您开始的活动完成之前,在开始的活动中,创建一个意图,并将您想要传回的任何数据作为额外的数据放入意图中。现在打电话setResult(..., intent)

Intent resultIntent = new Intent();
resultIntent.putExtra("myKey", myVal);
setResult(0, resultIntent);

在您的启动活动中,实施onActivityResult()并处理启动活动的结果。您将收到一个Intent,这是您在开始的活动中添加额外内容的意图。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //TODO handle here. 
}
于 2012-06-27T20:06:12.750 回答
1

扩展Application类并在该类中声明和初始化您的交叉活动对象,并为这些对象编写 getter。

现在在活动中使用以下代码,您可以访问这些对象。

getApplication().getMyObject().doSomeAction();

请记住,您必须修改您的 androidManifest.xml 以告知 android 您的扩展应用程序类。

于 2012-06-27T20:14:01.207 回答
0

解决方案:使用 StartActivityforResult 将数据移回第二个活动到第一个活动

一个例子有数据移动到第二个活动到第一个活动 -http://micropilot.tistory.com/1577

于 2012-06-27T20:03:42.937 回答