2012-09-29 146 views
2

這裏是我的代碼段:StarActivityForResult(),導致強制關閉在返回意向數據

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 

    case R.id.menu_settings: 

     startActivityForResult(new Intent(MainActivity.this,Settings.class),1); 
     break; 
    } 
    return true; 
} 


@Override 
public void onActivityResult(int requestCode,int resultCode,Intent data) { 

    Toast.makeText(getBaseContext(),requestCode, Toast.LENGTH_SHORT); // This was causing the error. See the fantastic answer that's been provided for details. Comment out this this and it works ! 

    if(requestCode==1) { 
     if(resultCode==RESULT_OK) 
     { 
      String extraData=data.getData().toString(); 

      minD=Integer.parseInt(extraData); 
      Toast.makeText(getBaseContext(),minD, Toast.LENGTH_SHORT); 
     } 

    } 

} 

下面是Settings.java

package com.example.com.draft1; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 



public class Settings extends Activity { 



@Override 

public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.settings); 


    Button setbtn = (Button) findViewById(R.id.SetminDistance); 


    setbtn.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

     //Intent data=new Intent(); 
     EditText mind=(EditText) findViewById(R.id.minDistance); 
        String minDis =mind.getText().toString(); 
        Intent data = new Intent(); 
        data.setData(Uri.parse(minDis)); 

     Toast.makeText(getBaseContext(), minDis, Toast.LENGTH_SHORT).show(); 
        // intent.putExtra("minD", minDis); 





     if (getParent() == null) { 
      setResult(RESULT_OK, data); 
     } else { 
      getParent().setResult(RESULT_OK, data); 
     } 
     finish(); 



     /* setResult(RESULT_OK, data); 
     finish();*/ 
      } 

}); 

} 
} 

現在的代碼時,我選擇的菜單選項設置,所需的活動打開完美,但是,當我試圖返回數據時,它導致一個力量關閉

這裏是L ogcat讀數顯示錯誤:

09-29 16:03:29.366: E/AndroidRuntime(766): FATAL EXCEPTION: main 
09-29 16:03:29.366: E/AndroidRuntime(766): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=65 }} to activity {com.example.com.draft1/com.example.com.draft1.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574) 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.app.ActivityThread.access$2000(ActivityThread.java:117) 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961) 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.os.Looper.loop(Looper.java:130) 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.app.ActivityThread.main(ActivityThread.java:3683) 
09-29 16:03:29.366: E/AndroidRuntime(766): at java.lang.reflect.Method.invokeNative(Native Method) 
09-29 16:03:29.366: E/AndroidRuntime(766): at java.lang.reflect.Method.invoke(Method.java:507) 
09-29 16:03:29.366: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
09-29 16:03:29.366: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
09-29 16:03:29.366: E/AndroidRuntime(766): at dalvik.system.NativeStart.main(Native Method) 
09-29 16:03:29.366: E/AndroidRuntime(766): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.content.res.Resources.getText(Resources.java:201) 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.widget.Toast.makeText(Toast.java:258) 
09-29 16:03:29.366: E/AndroidRuntime(766): at com.example.com.draft1.MainActivity.onActivityResult(MainActivity.java:570) 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.app.Activity.dispatchActivityResult(Activity.java:3908) 
09-29 16:03:29.366: E/AndroidRuntime(766): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528) 
09-29 16:03:29.366: E/AndroidRuntime(766): ... 11 more 

爲什麼我遇到強制關閉嗎?

我真的很感激幫助這裏的問題

按照建議

if(requestCode==1) { 
     if(resultCode==RESULT_OK) 
     { 
      //String extraData=data.getData().toString(); 

      //minD=Integer.parseInt(extraData); 
      Toast.makeText(getBaseContext(),String.valueOf(requestCode), Toast.LENGTH_SHORT).show(); 
     } 
+0

'$資源NotFoundException:字符串資源ID#爲0x1" ' – keyser

+0

@Keyser感謝您的回覆。你能解釋一下嗎?我是新來的機器人,因此我在理解Logcat條目時遇到了一些困難。 – Anant

+0

對不起,不能給你更多的東西,但這就是力量關閉的原因。查看所有的字符串資源代碼。 – keyser

回答

5

如果你看到Toast.makeText

public static Toast makeText (Context context, int resId, int duration) 

所以利息

謝謝如果你給第二參數作爲int然後系統會查找此ID在R.java但具有諷刺意味的是其實你想打印的數量,以便只投你的第二個參數Stringthis

public static Toast makeText (Context context, CharSequence text, int duration) 

變化吐司行代碼

Toast.makeText(getBaseContext(),requestCode+"", Toast.LENGTH_SHORT).show(); 

Toast.makeText(getBaseContext(),String.valueOf(requestCode), Toast.LENGTH_SHORT).show(); 
+0

感謝您的回答。但是,我仍然得到相同的力量關閉錯誤 – Anant

+1

它肯定會工作。 –

+0

對不起,但不幸的是,它仍然給我一個力量關閉錯誤。根據您的建議,我編輯了我的問題以反映代碼。 – Anant

0

你應該使用,而不是URI mIntent.putExtra ... 檢查這個鱈魚Ë....

MainActivity.java

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_settings: 
     startActivityForResult(new Intent(MainActivity.this, Settings.class), 1); 
     break; 
    } 
    return true; 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 1) { 
     if (resultCode == RESULT_OK) { 
      minD = Integer.parseInt(data.getStringExtra("data")); 
      Toast.makeText(MainActivity.this, String.valueOf(minD), Toast.LENGTH_SHORT); 
     } 

    } 

}` 

Setting.java

public class Settings extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.settings); 

    Button setbtn = (Button) findViewById(R.id.SetminDistance); 

    setbtn.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      EditText mind = (EditText) findViewById(R.id.minDistance); 
      Intent data = getIntent(); 
      data.putExtra("data", mind.getText().toString()); 
      Toast.makeText(getBaseContext(), mind.getText().toString(), Toast.LENGTH_SHORT).show(); 

      if (getParent() == null) { 
       setResult(RESULT_OK, data); 
      } else { 
       getParent().setResult(RESULT_OK, data); 
      } 
      finish(); 
     } 

    }); 

} 

}

相關問題