简体   繁体   English

OnActivityResult()Intent数据始终为null,没有任何理由

[英]OnActivityResult() Intent data is always null with no reason

First of all: 首先:

ALL of the answers of the related questions did not solved my problem, thats why i am creating a new question. 所有相关问题的答案都没有解决我的问题,这就是为什么我要创建一个新问题。

Explanation: 说明:

I have a huge android app, i use Eclipse-ADT plugin and i am using a smartphone that have android 4.0.4 to do the tests, i have a lot of Activity's and i pass values between Activity's via Bundle getExtras in onCreate() and too i get some values on onActivityResult 's. 我有一个巨大的Android应用程序,我使用Eclipse-ADT插件,我使用的是具有android 4.0.4的智能手机进行测试,我有很多活动,我通过onCreate() Bundle getExtras在Activity之间传递值我也在onActivityResult上获得了一些价值。

Obs: OBS:

(1) = Activity A that calls for result -> Activity B (1) =要求结果的活动A - >活动B.

(2) = Activity B that get values from Activity A and returns other values to -> Activity A. (2) =活动B从活动A获取值并将其他值返回到 - >活动A.

Problem: 问题:

  1. I have ProdutosForm.java (1) activity and PedidoItemx.java (2) activity. 我有ProdutosForm.java (1)活动和PedidoItemx.java (2)活动。
  2. Inside ProdutosForm.java (1), i am starting PedidoItemx.java (2) activity to get a result: ProdutosForm.java (1)中,我正在启动PedidoItemx.java (2)活动以获得结果:

Code for item 2: 第2项的代码:

<!-- language: lang-java -->

Intent i = new Intent();
i.setClass(this, PedidoItemx.class);
i.putExtra("CODE", code );

...
... (a LOT of putExtras....)
...

//and after putExtras, thats the relevant part
startActivityForResult(i, 1); //i its the Intent setted as PedidoItemx.class
  1. Inside PedidoItemx.java (2) onCreate method i have: PedidoItemx.java (2) onCreate方法里面我有:

Code for item 3: 第3项的代码:

<!-- language: lang-java -->

Bundle extras    = getIntent().getExtras();
code             = extras.getString("CODE");

4. Too, still inside PedidoItemx.java (2) i set some new values and i set the result to RESULT_OK: 4.太,仍然在PedidoItemx.java (2)里面我设置了一些新值,我将结果设置为RESULT_OK:

Code for item 4 第4项的代码

<!-- language: lang-java -->

Intent i = getIntent();
i.putExtra("PEDIDO", cPedPedido);
i.putExtra("VLRFRETE", valorFrete);
setResult(RESULT_OK, i); //RESULT_OK is a native constant that have value -1
finish();
  1. Now, returning to ProdutosForm.java (1) i have the following onActivityResult code to get the values returned by PedidoItemx.java (2): 现在,返回ProdutosForm.java (1)我有以下onActivityResult代码来获取PedidoItemx.java (2)返回的值:

Code for item 5: 第5项的代码:

<!-- language: lang-java -->

public void onActivityResult(int requestCode,int resultCode,Intent data){
    if(resultCode==RESULT_OK && requestCode==1){
      Bundle MBuddle = data.getExtras();
      Intent i = getIntent();
      String cPedidoExtra = MBuddle.getString("PEDIDO");
      i.putExtra("PEDIDO", cPedidoExtra);
      i.putExtra("VLRFRETE", MBuddle.getFloat("VLRFRETE", 0));
      cPedPedido = cPedidoExtra; //this does nothing important
      setResult(RESULT_OK, i);
    }
}
  1. Everything looks OK and should work, but it doesn't. 一切看起来都很好,应该有效,但事实并非如此。 BECAUSE my requestCode is always 0 and my data is always null thats the real problem. 因为我的requestCode始终为0 ,我的data总是为null这就是真正的问题。

My manifest: 我的清单:

<!-- language: lang-xml -->

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bee.mobile"
    android:versionCode="2"
    android:versionName="1.50" >
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" /> 
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCOUNT_MANAGER" />           
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <!-- <uses-permission android:name="android.permission.RECEIVE_SMS" /> -->

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:screenOrientation="user"
        android:debuggable="true"> <!-- mudar para false ao gerar depois -->

        <activity
            android:name="bee.mobile.form.outros.SplashActivity"
            android:label="@string/app_name"            
            android:screenOrientation="sensor">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Outros -->
        <activity android:name="bee.mobile.form.outros.MainActivity"/>
        <activity android:name="bee.mobile.form.outros.HistoricoClienteForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoListaNotasForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoNotaForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoProdutoForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoMesProdForm"/>
        <activity android:name="bee.mobile.form.outros.HistoricoQuemForm"/>
        <activity android:name="bee.mobile.form.outros.PedidoItemx"/>
        <activity android:name="bee.mobile.form.outros.RelatoriosForm"/>
        <activity android:name="bee.mobile.form.outros.EstufaDetalhes"
            android:windowSoftInputMode="stateHidden"/>
        <!-- Lista -->             
        <activity android:name="bee.mobile.form.lista.ClientesForm"/>
        <activity android:name="bee.mobile.form.lista.ProdutosForm"/>
        <activity android:name="bee.mobile.form.lista.PedidoListaForm"/>
        <activity android:name="bee.mobile.form.lista.ContatosListaForm"/>
        <activity android:name="bee.mobile.form.lista.GruposForm"/>
        <activity android:name="bee.mobile.form.lista.NotaListaForm"/>
        <!-- Cadastro -->
        <activity android:name="bee.mobile.form.cadastro.PedidoCadForm"/>
        <activity android:name="bee.mobile.form.cadastro.ClientesCadastroForm"
            android:windowSoftInputMode="stateHidden"/>        
        <activity android:name="bee.mobile.form.cadastro.ContatosCadForm"
            android:windowSoftInputMode="stateHidden"/>
        <activity android:name="bee.mobile.form.cadastro.PedidoCadForm_Financeiro"/>
        <!-- Sem destino -->        
        <activity android:name="bee.mobile.ConfigurarForm"/>
        <activity android:name="bee.mobile.ConfiguracaoLocalForm"/>
        <activity android:name="bee.mobile.AtualizaManual"/>                        
        <activity android:name="bee.mobile.ReceberForm"/>                              
        <activity android:name="bee.mobile.ResolucaoForm"/>
        <activity android:name="bee.mobile.AtualizarForm"/>
        <activity android:name="bee.mobile.ParticipantesVendaForm"/>       
    </application>

</manifest>

Question: 题:

Why i cant get the data returned from Activity B?? 为什么我不能从活动B返回数据? i am doing something wrong? 我做错了什么?

I figured out the solution for my problem: 我找到了解决问题的方法:

I dont know WHY but if i MOVE this piece of code that is actually on buttonClick event on Activity B(2): 我不知道为什么,但如果我移动这段代码实际上是活动B(2)上的buttonClick事件:

Intent i = getIntent();
i.putExtra("PEDIDO", cPedPedido);
i.putExtra("VLRFRETE", valorFrete);
setResult(RESULT_OK, i); //RESULT_OK is a native constant that have value -1
finish();

To -> right after onCreate , it works ok. 要 - >在onCreate之后,它可以正常工作。

But if i move back this code "to the right place" on a button click, it doesnt work, i really dont know why the activity is having this very weird behaviour, but i'll be happy to share when i figure it out. 但是,如果我将这个代码“移动到正确的位置”按钮点击,它不起作用,我真的不知道为什么活动有这种非常奇怪的行为,但我很乐意分享,当我弄清楚。

In other words, i have to set the extras and the RESULT_OK before doing anything, otherwise it didnt work. 换句话说,我必须在做任何事情之前设置额外的和RESULT_OK ,否则它不起作用。

I think you should add an action ACTION_SEND to your intent to return to the other activity. 我认为你应该在你的意图中添加一个动作ACTION_SEND以返回到其他活动。 Something like this: 像这样的东西:

Intent i = getIntent();
**i.setAction(Intent.ACTION_SEND);**
i.putExtra("PEDIDO", cPedPedido);
i.putExtra("VLRFRETE", valorFrete);
setResult(RESULT_OK, i);
finish();

Use: 采用:

Intent i = this.getIntent();

instead of 代替

Intent i = getIntent();

in the activity where you need to receive data of the intent, which has the values binded to some keys. 在您需要接收意图数据的活动中,该数据具有绑定到某些键的值。

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

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