我有一個名爲MyPrimaryClass的類,這個類在按下時有一個按鈕,用類myClassForResult創建一個Intent。來自資源字符串的Toast.makeText
我使用它來啓動它:
startActivityForResult(myIntentOfMyClassForResult, ACTIVITY_EDIT_BTEXT);
兩個MyPrimaryClass和myClassForResult延伸活動。
因此,當我在myClassForResult中調用Toast.makeText時,使用R.string.my_resource_string的文本參數,它給了我強制關閉!
我曾嘗試這樣的:
Context c = myClassForResult.this;
Toast toast = Toast.makeText(c,
c.getResources().getString(R.string.my_resource_string),
Toast.LENGTH_SHORT);
toast.show();
同樣在此:C = getApplicationContext()
同樣在此:C = getBaseContext()
同樣在此:
Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
R.string.my_resource_string,
Toast.LENGTH_SHORT);
toast.show();
如果我使用內聯字符串,比如「My Toast Text!」,它就可以工作。但我需要從資源中獲取一個字符串。
- 問題解決:
爲了解決我改變了敬酒的持續時間Toast.LENGTH_LONG
字符串R.string.my_resource_string值是問題「的標題爲空」
當我將它的值更改爲「The title」時,它工作正常,所以我猜這個字符串Toast.LENGTH_SHORT持續時間太長。
但是,當我將持續時間更改爲Toast.LENGTH_LONG時,我可以使用長字符串。
Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
R.string.my_resource_string,
Toast.LENGTH_LONG);
toast.show();
發佈強制關閉的logcat錯誤。 – JoxTraex 2012-02-05 20:28:54
這是logcat錯誤: android.content.res.Resources $ NotFoundException:String資源ID#0x7f04000a – DMF 2012-02-05 20:36:50
你能解釋爲什麼你要使用這兩個類的意義? – JoxTraex 2012-02-05 20:38:25