2015-10-08 7 views
15

में संसाधन आईडी # 0x0 है, मेरे पास RecyclerView है, और इसके एडाप्टर में, मैंने OnLongClickListener के समान कुछ बनाया है, जिसे मैं भ्रम से बचने के लिए OnEntryLongClickListener पर कॉल कर रहा हूं।

मैं विभिन्न कार्यों के लिए सूची आइटम के साथ एक संवाद प्रदर्शित करने के लिए एक AlertDialog उपयोग कर रहा हूँ। हालांकि, मैं निम्न त्रुटि हो रही है:

adapter.setOnEntryLongClickListener(new RVAdapter.OnEntryLongClickListener() { 
    @Override 
    public void onEntryLongClick(View view, int position) { 
     final MiniEntry thisEntry = entryList.get(position); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext()); 
     builder.setTitle(thisEntry.getEntryName());); 
     builder.setItems(R.array.quickActions, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // Other code here 
      } 
     }); 
     AlertDialog alert = builder.create(); // The error log points to this line 
     alert.show(); 
    } 
}); 
mRecyclerView.setAdapter(adapter); 

साथ ही एक्सएमएल मैं सरणी के लिए उपयोग कर रहा हूँ:

<string-array name="quickActions"> 
    <item>Add to Favourites</item> 
    <item>More information</item> 
</string-array> 

E/AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0x0 
    at android.content.res.Resources.getValue(Resources.java:2345) 
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:3910) 
    at android.content.res.Resources.getLayout(Resources.java:2161) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:413) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
    at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:734) 
    at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:711) 
    at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:883) 
    at com.mycompany.myapp.ThisActivity$2.onEntryLongClick(ThisActivity.java:135) 
    at com.mycompany.myapp.adapter.RVAdapter$RVViewHolder.onLongClick(RVAdapter.java:41) 
    at android.view.View.performLongClick(View.java:5236) 

नीचे प्रासंगिक कोड मैं का उपयोग कर रहा है

मुझे यकीन है कि अगर यह मायने रखती है नहीं कर रहा हूँ, लेकिन मैं android.support.v7.app.AlertDialog से AlertDialog (v7 समर्थन लाइब्रेरी से) का आयात कर रहा हूँ।

मैं कैसे इस समस्या को हल कर सकते हैं?

+1

बदलें 'getBaseContext()' वर्तमान गतिविधि उदाहरण के लिए 'AlertDialog.Builder' इन्स्टेन्शियशन में। उदाहरण के लिए, 'नया अलर्टडिअलॉग.बिल्डर (यह एक्टिविटी.थिस) '। –

+0

@ माइकएम। यह काम किया! क्या आप इसे एक उत्तर के रूप में पोस्ट कर सकते हैं और * समझाएं कि समस्या को हल क्यों किया गया है - धन्यवाद। –

उत्तर

21

में Activity इंस्टेंसेशन में बदलें। उदाहरण के लिए:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

एक AlertDialog कुछ संसाधनों जिनके मान विषयों और शैलियों Context इसे इस्तेमाल करता है से जुड़ी द्वारा प्रदान की जाती हैं की आवश्यकता है। ContextgetBaseContext() द्वारा लौटाए गए उन संलग्न नहीं है, लेकिन Activity करता है। दरअसल, जब भी यूआई घटक के लिए Context की आवश्यकता होती है - उदाहरण के लिए, Dialog एस, View एस, Adapter एस, आदि - वर्तमान Activity आमतौर पर आप उपयोग करना चाहते हैं। कि Theme.AppCompat.Light.Dialog.Alert

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert" />

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialogTheme); 

फैली अपने संवाद के लिए एक शैली डाल

+0

अगर मैं एक टुकड़ा के भीतर से इस फोन का उपयोग 'getActivity()' मैं अभी भी इस त्रुटि मिलती है। कोई विचार क्यों? – AdamMc331

+0

ने बहुत समय बचाया। धन्यवाद –

+0

तो एक जब पता है जब * गतिविधि * या जब उपयोग करने के लिए * BaseContext * – Relm

16

कोशिश यह मेरे लिए काम करता है।

अभिवादन

+0

मेरे लिए यह तय हो गई है, धन्यवाद का उपयोग करने के! – Nickmccomb

+0

मेरे लिए भी काम करता है। मुझे समर्थन lib में एक बग की तरह लग रहा है। –

+0

मेरे लिए कुंजी यह थी कि, यदि मैं आपके जैसे विषय निर्दिष्ट नहीं करता हूं, तो गतिविधि विषय को एक AppCompat विषय का विस्तार करना था – rupps

संबंधित मुद्दे