Android 台灣中文網

標題: startActivityForResult 接收失敗 [打印本頁]

作者: peggy02011187    時間: 2013-4-8 07:42
標題: startActivityForResult 接收失敗
程式碼如下
A_pay.java 部分

...
private static final int EDIT = 1;
protected void onStart() {
                super.onStart();
                setContentView(R.layout.A_pay);
                TextView to_set_money = (TextView) findViewById(R.id.txv_money);               
                to_set_money.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                Intent intent = new Intent();
                                intent.setClass(A_pay.this, .Com.class);
                                A_pay.this.startActivityForResult(intent, EDIT);
                        }
                });
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                TextView to_set_money = (TextView) findViewById(R.id.txv_money);               
                switch (requestCode) {
                case EDIT:
                        Bundle bundle = data.getExtras();
                        int aaa = bundle.getInt("money1"); //只要在 onActivityResult 有執行動作,程式就會在這裡崩潰
                        to_set_money.setText(aaa);      
                }
        }
....


Com.java 部分

...
        u_com_show.setText(String.valueOf(u_com_show_result));
        Bundle Deliver = new Bundle();
        Intent intent = new Intent();                                
        //intent.setClass(ComputerActivity.this, Add_pay.class);       <<<這裡試過不管有沒有打結果依樣會崩潰                                
        Deliver.putInt("money1", u_com_show_result);
        intent.putExtras(Deliver);
        setResult(RESULT_OK,intent);        
        finish();
...
作者: peggy02011187    時間: 2013-4-8 15:05
實在是找不出錯誤在哪
不知道有沒有大神能幫忙
或者給一份完整的範例檔
我自己在試試

拜託了~~~~~~
作者: whl    時間: 2013-4-8 18:06
本帖最後由 whl 於 2013-4-8 18:10 編輯

貼一下 Log 來看看。

試試看
intent.putExtra("money1", u_com_show_result);

String aaa = bundle.getString("money1");
作者: lung6660    時間: 2013-4-8 23:15
不專業回答
1.  A_pay.this.startActivityForResult(intent, EDIT); 這一行是送出requestCode也就是EDIT
     所以在onActivityResult就會馬上執行
                    switch (requestCode) {
                 case EDIT:
                 Bundle bundle = data.getExtras();
                         int aaa = bundle.getInt("money1");  <----沒有值 所已崩潰了
                                  to_set_money.setText(aaa); <----還有這一行應該會出錯吧 aaa是int不是String
               }

         你可以先試試改為看看會不會崩潰
                                int aaa = bundle.getInt("money1",1);
                        to_set_money.setText(aaa.toString());
作者: peggy02011187    時間: 2013-4-9 00:47
本帖最後由 peggy02011187 於 2013-4-9 00:51 編輯

一樣會崩潰
不過還是謝謝
不過問一下
為什麼你會要我在  
int aaa = bundle.getInt("money1",1);
的尾巴加1??
----------------
另外
No package identifier when getting value for resource number 0x000012a2   <<好像getting時會出錯?(不確定

Shutting down VM

threadid=1: thread exiting with uncaught exception (group=0x40a71930)

FATAL EXCEPTION: main

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.fm2/com.example.fm2.Add_pay}: android.content.res.Resources$NotFoundException: String resource ID #0x12a2

at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)

at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)

明明有設定result,但不知為啥還是-1....
RESULT_OK不是預設為正數嗎?
可是還是有傳輸
但理論是我應該有設吧(不知道哪邊出錯)
另外想問
有辦法查錯誤裡面的 JAVA:XXX  是什麼意思嗎??
作者: lung6660    時間: 2013-4-9 17:05
int aaa = bundle.getInt("money1",1);
加1是沒有值時以1取代

附件是我有試成功的程式碼

text2.rar (1.35 MB, 下載次數: 0)