7

में चेतावनी डिलीग बनाने में असमर्थ मेरे पास Activity है जो ActionBarActivity बढ़ाता है। जब भी मैं उस में एक AlertDialog बनाने का प्रयास करें, लाइन जहां संवाद इस त्रुटिएक्शनबैरएक्टिविटी

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 

देने बनाई गई है लेकिन मैं पहले से ही Appcompat विषय Theme.AppCompat.Light.NoActionBar उपयोग कर रहा हूँ के रूप में मैं उपकरण पट्टी का उपयोग कर रहा पर दुर्घटनाओं। इसका क्या कारण रह सकता है?

import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 



public class MyActivity extends ActionBarActivity { 

    Toolbar toolbar; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_comments); 

     toolbar = (Toolbar)findViewById(R.id.tool_bar_comment); 
     setSupportActionBar(toolbar); 
       AlertDialog alertDialog; 
       alertDialog = new AlertDialog.Builder(getApplicationContext()).create(); 
       alertDialog.setTitle("Network error"); 
       alertDialog.setMessage("Check network connection and try again."); 
       alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         finish(); 
} 
     }); 
        alertDialog.show(); 


     } 
    } 

यहाँ मेरी mainfest फ़ाइल है::

<application 
     android:name=".Networking.AppController" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

    ... 



     <activity 
     android:name=".MyActivity" 
     android:label="@string/myactivity" 
     > 
</activity> 
</application> 

और, यहाँ styles.xml

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
</style> 

android:theme में गतिविधि गुण जोड़ना है यहाँ मेरी गतिविधि है मेनफेस्ट ने बिल्कुल मदद नहीं की।

<activity 
     android:name=".MyActivity" 
     android:label="@string/myactivity" 
     android:theme="@style/Theme.AppCompat.NoActionBar" 
     > 
+0

संभव डुप्लिकेट के साथ यह गतिविधि] (http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity) –

+1

आपको उपयोग करने की आवश्यकता नहीं है एंड्रॉइड: थीम = "@ शैली/थीम.एप कॉम्पैक्ट.एक्शनएक्शनबार" बस एंड्रॉइड का उपयोग करें: थीम = "@ स्टाइल/ऐपथीम" – arjunkn

उत्तर

26

मैं एक ही सटीक त्रुटि पुन: पेश नहीं कर सका। हालांकि, मुझे लगता है कि समस्या AlertDialog.builder कन्स्ट्रक्टर को पारित संदर्भ है। वास्तव में, एक गतिविधि संदर्भ इसे पारित किया जाना चाहिए।

यह एक

   alertDialog = new AlertDialog.Builder(this).create(); 

के साथ इस लाइन

   alertDialog = new AlertDialog.Builder(getApplicationContext()).create(); 

के बजाय ऐसे कृपया मुझे पता इस समस्या का हल है, तो चलो।

+0

ऐसा सुनकर खुशी हुई! गतिविधि या एप्लिकेशन संदर्भ का उपयोग करने के बारे में बेहतर समझ प्राप्त करने के लिए आप यह [पोस्ट] (http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-plication-context) भी पढ़ सकते हैं। – Ajeje

-1

उपयोग AppCompatActivity बजाय ActionBarActivity

public class MyActivity extends AppCompatActivity 
+0

यह कुछ भी नहीं बदलेगा :-( –

10

मैं आयात बदलकर इस समस्या हल हो जाती:

android.support.v7.app.AlertDialog 

to 

android.app.AlertDialog 
2

बदलें

new AlertDialog.Builder(this) 

आप के रूप में (अनुकूलक में और OnBindViewHolder में) विधि alertDialog उपयोग कर रहे हैं

new AlertDialog.Builder(MainActivity.this) 
1

को नीचे दिए गए कोड में दिखाया गया है:

@Override 
public void onBindViewHolder(final UserListAdapter.ViewHolder holder, final int position) { 

//long click for delete the record 
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { 
    @Override 
    public boolean onLongClick(View view) { 

     new AlertDialog.Builder(mContext) 

       .setMessage("Are you want to delete?") 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         Log.d(TAG, "onClick: yes"); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         Log.d(TAG, "onClick: no"); 
        } 
       }) 
       .setCancelable(false) 
       .show(); 
     return true; 
    } 
}); 

}

इस मामले नई AlertDialog.Builder (संदर्भ) नहीं है में ठीक से काम करता

तो आप नई AlertDialog.Builder (view.getContext()) से बदलने की जरूरत है । यह मेरे लिए काम करता है, ty।

0

इसे पारित करके हल किया जा सकता है।getApplicationContext(); के स्थान पर Activityname.this अपने मामले में की तरह alertDialog = new AlertDialog.Builder(MyActivity.this)).create();

0

AndroidManifest.xml फ़ाइल में अपने <application> को यह पंक्ति जोड़ें

android:theme="@style/Theme.AppCompat.Light" 
की [आप एक Theme.AppCompat विषय (या वंशज) उपयोग करने की आवश्यकता