就像标题所说,即使我正在使用 You need to use a Theme.AppCompat theme (or descendant) with this
,我仍然会收到错误Theme.AppCompat
。
首先,这是得到错误的类(注意:我用注释标记了错误发生的行):
public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {
private Context context;
private Cursor cursor;
private MainActivity mainActivity;
public CustomSimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags, MainActivity mainActivity) {
super(context, layout, cursor, from, to, flags);
this.context = context;
this.cursor = cursor;
this.mainActivity = mainActivity;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
// Grab each row as it is pulled from the db.
DatabaseHelper dbHelper = new DatabaseHelper(context);
View row = super.getView(position, convertView, parent);
cursor.moveToPosition(position);
final Integer id = cursor.getInt(cursor.getColumnIndex("_id"));
final String personID = cursor.getString(cursor.getColumnIndex("personid"));
// Set alternating rows to different colors.
if(position % 2 == 0){
row.setBackgroundColor(Color.parseColor(Constants.WHITE));
} else {
row.setBackgroundColor(Color.parseColor(Constants.LIGHTGREY));
}
// Make the delete button clickable.
Button deletePersonButton = (Button)row.findViewById(R.id.remove_person);
deletePersonButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder deleteDialogBuilder = new AlertDialog.Builder(context);
deleteDialogBuilder.setTitle("Delete Person " + personID + "?");
deleteDialogBuilder.setMessage("This process is IRREVERSABLE!");
deleteDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DatabaseHelper databaseHelper = new DatabaseHelper(context);
databaseHelper.deletePerson(personID);
mainActivity.dataChanged();
Toast.makeText(context, "Person " + personID + " (ID: " + Integer.toString(id) + ") Deleted", Toast.LENGTH_LONG).show();
}
});
deleteDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog deleteDialog = deleteDialogBuilder.create();
deleteDialog.setIcon(R.drawable.warning);
deleteDialog.show(); // This is where I get the error
}
});
return row;
}
}
这是我调用CustomSimpleCursorAdapter的地方:
public class Home extends Fragment{
private MainActivity mainActivity;
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.home, container, false);
mainActivity = (MainActivity)getActivity();
return rootView;
}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
drawThePersonView();
}
public void drawThePersonView(){
Context context = mainActivity.getApplicationContext();
DatabaseHelper myDBHelper = new DatabaseHelper(context);
Cursor personCursor = myDBHelper.getUndeletedCasualtiesCursor();
String[] fromColumns = {"_id","personID","location","status"};
int[] toViews = {R.id.person_number_textview, R.id.person_personID_textview, R.id.person_location_textview, R.id.person_status_textview};
CustomSimpleCursorAdapter mySimpleCursorAdapter = new CustomSimpleCursorAdapter(context, R.layout.person_layout, personCursor, fromColumns, toViews, 0, mainActivity);
ListView myListView = (ListView) rootView.findViewById(R.id.person_row);
// Draw the list
myListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
}
}
这是我的 AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name="com.domain.MyApplication">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".forms.Form01"
android:label="@string/form_01_title"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan|stateHidden" />
<activity
android:name=".forms.Form02"
android:label="@string/form_02_title"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan|stateHidden" />
<activity
android:name=".forms.Form03"
android:label="@string/form_03_title"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan|stateHidden" />
<activity
android:name=".AddPerson"
android:label="@string/title_activity_add_person"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".AddItem"
android:label="@string/title_activity_add_item"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
在我发布的上述清单中,没有提及Theme.AppCompat
,但我在发布此之前确实做了更改。我一次更改了一个,然后我将它们全部更改为Theme.AppCompat
并且我仍然每次都收到错误,即使我正在使用 Theme.AppCompat
。< / p>
这是错误的logcat:
09-01 13:07:09.457 26231-26231/com.domain E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.domain, PID: 26231
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:252)
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:76)
at android.support.v7.app.AlertController.installContent(AlertController.java:216)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at com.domain.adapters.CustomSimpleCursorAdapter$1.onClick(CustomSimpleCursorAdapter.java:57)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5258)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
有什么建议吗?
谢谢!
答案 0 :(得分:1)
只需将getApplicationContext()
更改为getContext()
。
public void drawThePersonView(){
Context context = getContext();
...
// your stuff
...
}
我还没有测试过这段代码。请测试并告诉我。
更多阅读Understanding Context In Android Application。
我希望它有所帮助。
答案 1 :(得分:0)
这个问题已经发生在我身上
这是因为在样式不是(Theme.AppCompat)
的活动中使用“警报”对话框构建器
我将样式更改为Theme.AppCompat
但仍无法正常工作
我注意到当你改变活动的风格时android studio有问题所以我建议你复制与你的活动相关的java和xml的代码
然后删除活动,然后使用相同的java和xml再次重新创建它,但样式为Theme.AppCompat
可能有效的另一个解决方案是,您可以进行要构建的对话的活动,而不是进行警报对话
它更易于使用,在清单文件中,您可以将主题设置为对话活动,如android:theme="@style/Base.Theme.AppCompat.Dialog"
,以便您创建的对话活动显示为对话