2012-06-06 174 views
9

我正在創建一個應用程序,玩家在編輯文本中輸入他們的名字。這些信息將被檢索並通過附加功能發送給第二項活動。每當我運行這個,我得到一個資源$ NotFoundException。我堅信如果這是從我的代碼或從我的資源文件夾。

這裏是我的代碼(更新,而TOAST報表)

private void getNames(int number){  

    Intent intent = new Intent(Setup.this, Test.class); 
    intent.putExtra("numberofplayers", number); 

    try { 

    if(!player1.getText().toString().contentEquals("")) 
     p1 = player1.getText().toString(); 
    else 
     p1= "Player 1"; 
    if(!player2.getText().toString().contentEquals("")) 
     p2 = player2.getText().toString(); 
    else 
     p2= "Player 2"; 
    if(!player3.getText().toString().contentEquals("")) 
     p3 = player3.getText().toString(); 
    else 
     p3= "Player 3"; 
    if(!player4.getText().toString().contentEquals("")) 
     p4 = player4.getText().toString(); 
    else 
     p5= "Player 5"; 
    if(!player6.getText().toString().contentEquals("")) 
     p6 = player6.getText().toString(); 
    else 
     p6= "Player 6"; 
    if(!player7.getText().toString().contentEquals("")) 
     p7 = player7.getText().toString(); 
    else 
     p7= "Player 7"; 
    if(!player8.getText().toString().contentEquals("")) 
     p8 = player8.getText().toString(); 
    else 
     p8= "Player 8"; 

    switch (number) { 
    case 2: 
     String[] a = {p1,p2}; 
     names=a; 
     break; 
    case 3: 
     String[] b = {p1,p2,p3}; 
     names=b; 
     break; 
    case 4: 
     String[] c = {p1,p2,p3,p4}; 
     names=c; 
     break; 
    case 5: 
     String[] d= {p1,p2,p3,p4,p5}; 
     names=d; 
     break; 
    case 6: 
     String[] e = {p1,p2,p3,p4,p5,p6}; 
     names=e; 
     break; 
    case 7: 
     String[] f = {p1,p2,p3,p4,p5,p6,p7}; 
     names=f; 
     break; 
    case 8: 
     String[] g = {p1,p2,p3,p4,p5,p6,p7,p8}; 
     names=g; 
     break; 
    } 
    } catch (Exception e) { 
     Log.e("Setup.class Error:", e.getMessage()); 
    } 
    //Toast.makeText(this, number + "", Toast.LENGTH_SHORT).show(); 

    //for(int q =0;q<names.length;q++) 
    // Toast.makeText(this, names[q], Toast.LENGTH_SHORT).show(); 

    intent.putExtra("namearray", names); 
    startActivity(intent); 
} 

下面是接收活動代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Bundle ext = getIntent().getExtras(); 
    int i= ext.getInt("numberofplayers"); 
    String[] names = ext.getStringArray("namearray"); 

    Toast.makeText(this, i, Toast.LENGTH_SHORT).show(); 

    for(int q =0;q<names.length;q++) 
     Toast.makeText(this, names[q], Toast.LENGTH_SHORT).show(); 
} 

這裏是logcat的錯誤(修訂logcat的報告):

06-06 21:21:07.267: E/AndroidRuntime(2538): FATAL EXCEPTION: main 
06-06 21:21:07.267: E/AndroidRuntime(2538): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brightdesign.truthordare/com.brightdesign.truthordare.Test}: android.content.res.Resources$NotFoundException: String resource ID #0x2 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.app.ActivityThread.access$600(ActivityThread.java:123) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.os.Looper.loop(Looper.java:137) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at java.lang.reflect.Method.invoke(Method.java:511) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at dalvik.system.NativeStart.main(Native Method) 
06-06 21:21:07.267: E/AndroidRuntime(2538): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.content.res.Resources.getText(Resources.java:247) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.widget.Toast.makeText(Toast.java:260) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at com.brightdesign.truthordare.Test.onCreate(Test.java:16) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.app.Activity.performCreate(Activity.java:4465) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
06-06 21:21:07.267: E/AndroidRuntime(2538):  ... 11 more 
+0

這個錯誤的原因是荒謬的..至少android應該給出適當的合理的錯誤信息。我在這個錯誤上浪費了2-3個小時,我應該早點訪問這裏:( –

回答

39

這是您的第一個吐司電話:

Toast.makeText(this, number, Toast.LENGTH_SHORT).show(); 

這裏,第二個參數是一個整數(number),所以它認爲這是一個字符串ID。嘗試用number+""代替number

+15

更好的方法是使用'String.valueOf(number)'。 –

+0

好的...謝謝,那是一個錯誤,我沒有抓到,但我只把Toast語句放在Toast語句之前或之後看是否出現問題,當我刪除Toast語句時,應用程序強制關閉,任何建議? – Drew

+0

請更正/刪除Toast行併發布新的LogCat輸出,所以我們可以看到強制關閉錯誤的位置 – Eric

相關問題