Java 问题与您需要将主题.AppCompat主题(或子代)用于此活动主题。AppCompat主题(或子代)用于此活动;

Java 问题与您需要将主题.AppCompat主题(或子代)用于此活动主题。AppCompat主题(或子代)用于此活动;,java,android,android-appcompat,Java,Android,Android Appcompat,我突然明白了这个问题。我已经浏览了这个网站和其他网站上的一些帖子。以下是我在试图了解这个问题的原因时访问过的几个链接 [ [ [ 据我所知,在每种情况下,这个错误的原因都是不同的 05-30 20:20:23.278 13996-13996/ca.rvogl.tpbcui2 E/AndroidRuntime: FATAL EXCEPTION: main Process: ca.rvogl.tpbcui2, PID: 13996 java.lang.Runtim

我突然明白了这个问题。我已经浏览了这个网站和其他网站上的一些帖子。以下是我在试图了解这个问题的原因时访问过的几个链接

  • [
  • [

  • [

据我所知,在每种情况下,这个错误的原因都是不同的

05-30 20:20:23.278 13996-13996/ca.rvogl.tpbcui2 E/AndroidRuntime: FATAL EXCEPTION: main
        Process: ca.rvogl.tpbcui2, PID: 13996
        java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.rvogl.tpbcui2/ca.rvogl.tpbcui2.Views.BowlerActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
            at android.app.ActivityThread.-wrap12(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6119)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
         Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)
            at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
            at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
            at ca.rvogl.tpbcui2.Views.BowlerActivity.onCreate(BowlerActivity.java:50)
            at android.app.Activity.performCreate(Activity.java:6679)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
            at android.app.ActivityThread.-wrap12(ActivityThread.java) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:154) 
            at android.app.ActivityThread.main(ActivityThread.java:6119) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
安卓清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ca.rvogl.tpbcui2">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Tenpin Bowling Companion"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true">
        <activity
            android:name=".views.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".views.BowlerActivity">
        </activity>
    </application>
</manifest>

Styles.xml

<resources>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
        <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
        </style>
       <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
       <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

@颜色/原色
@颜色/原色暗
@颜色/颜色重音
假的
真的
BowlerAcitivity-根据Logicat,这是它失败的地方

public class BowlerActivity extends AppCompatActivity  {

        private BowlerAdapter mAdapter;
        private List<Bowler> bowlersList = new ArrayList<>();
        private CoordinatorLayout coordinatorLayout;
        private RecyclerView recyclerView;
        private TextView noBowlersView;

        private DatabaseHelper db;

        private TextView myText;
        private String savedExtra;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_bowler);
            //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            //setSupportActionBar(toolbar);
            //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            //getSupportActionBar().setDisplayShowHomeEnabled(true);

            savedExtra = String.valueOf(getIntent().getIntExtra("leagueId",2)); //<<<< ADDED
            myText = (TextView) findViewById(R.id.tvLeagueId);
            final String s = myText.toString();

            coordinatorLayout = findViewById(R.id.coordinator_layout);
            recyclerView = findViewById(R.id.recycler_view);
            noBowlersView = findViewById(R.id.empty_bowlers_view);

            db = new DatabaseHelper(this);

            bowlersList.addAll(db.getAllBowlers(savedExtra));

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.add_bowler_fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showBowlerDialog(false, null, -1);
                }
            });

            mAdapter = new BowlerAdapter(this, bowlersList);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16));
            recyclerView.setAdapter(mAdapter);

            toggleEmptyBowlers();

            //On Long Click On The RecyclerView Item An Alert Dialog Is Opened With The Option To Choose Edit/Delete
            recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
                    recyclerView, new RecyclerTouchListener.ClickListener() {
                @Override
                public void onClick(View view, final int position) {

                    //Intent myIntent = new Intent(getApplicationContext(), SeriesActivity.class);
                    //startActivity(myIntent);
                }

                @Override
                public void onLongClick(View view, int position) {
                    showActionsDialog(position);
                }
            }));
        }

        //Inserting New Bowler In The Database And Refreshing The List
        private void createBowler(String bowler, String leagueId) {
            //Inserting Bowler In The Database And Getting Newly Inserted Bowler Id
            long id = db.insertBowler(bowler, leagueId);

            //Get The Newly Inserted Bowler From The Database
            Bowler n = db.getBowler(leagueId);

            if (n != null) {
                //Adding New Bowler To The Array List At Position 0
                bowlersList.add(0, n);

                //Refreshing The List
                mAdapter.notifyDataSetChanged();

                toggleEmptyBowlers();
            }
        }

        //Updating Bowler In The Database And Updating The Item In The List By Its Position
        private void updateBowler(String name, int position) {
            Bowler n = bowlersList.get(position);

            //Updating Bowler Text
            n.setName(name);
            n.setLeagueId(savedExtra);

            //Updating The Bowler In The Database
            db.updateBowler(n);

            //Refreshing The List
            bowlersList.set(position, n);
            mAdapter.notifyItemChanged(position);

            toggleEmptyBowlers();
        }

        //Deleting Bowler From SQLite Database And Removing The Bowler Item From The List By Its Position
        private void deleteBowler(int position) {
            // deleting the note from db
            db.deleteBowler(bowlersList.get(position));

            //Removing The Bowler From The List
            bowlersList.remove(position);
            mAdapter.notifyItemRemoved(position);

            toggleEmptyBowlers();
        }

        //Opens Dialog With Edit/Delete Options
        //Edit - 0
        //Delete - 0
        private void showActionsDialog(final int position) {
            CharSequence colors[] = new CharSequence[]{"Edit", "Delete"};

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Choose option");
            builder.setItems(colors, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        showBowlerDialog(true, bowlersList.get(position), position);
                    } else {
                        deleteBowler(position);
                    }
                }
            });
            builder.show();
        }

        //Show Alert Dialog With EditText Options to Enter/Edit A League
        //When shouldUpdate = true, It Will Automatically Display Old Bowler Name And Change The Button Text To UPDATE
        private void showBowlerDialog(final boolean shouldUpdate, final Bowler bowler, final int position) {
            LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
            View view = layoutInflaterAndroid.inflate(R.layout.dialog_bowler, null);

            AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(BowlerActivity.this);
            alertDialogBuilderUserInput.setView(view);

            final EditText inputBowler = view.findViewById(R.id.etBowlerNameInput);
            myText.setText(savedExtra);
            TextView dialogTitle = view.findViewById(R.id.dialog_title);
            dialogTitle.setText(!shouldUpdate ? getString(R.string.lbl_new_bowler_title) : getString(R.string.lbl_edit_bowler_title));

            if (shouldUpdate && bowler != null) {
                inputBowler.setText(bowler.getName());
                myText.setText(bowler.getLeagueId());

            }
            alertDialogBuilderUserInput
                    .setCancelable(false)
                    .setPositiveButton(shouldUpdate ? "update" : "save", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogBox, int id) {
                        }
                    })
                    .setNegativeButton("cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialogBox, int id) {
                                    dialogBox.cancel();
                                }
                            });

            final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
            alertDialog.show();

            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //Show Toast Message When No Text Is Entered
                    if (TextUtils.isEmpty(inputBowler.getText().toString())) {
                        Toast.makeText(BowlerActivity.this, "Enter Bowler!", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        alertDialog.dismiss();
                    }

                    //Check If User Is Updating Bowler
                    if (shouldUpdate && bowler != null) {

                        //Updating Bowler By Its Id
                        updateBowler(inputBowler.getText().toString(), position);

                    } else {
                        //Creating New Bowler
                        createBowler(inputBowler.getText().toString(), myText.getText().toString());
                    }
                }
            });
        }

        //Toggling List And Empty Bowler View
        private void toggleEmptyBowlers() {
            //You Can Check bowlerList.size() > 0

            if (db.getBowlersCount() > 0) {
                noBowlersView.setVisibility( View.GONE);
            } else {
                noBowlersView.setVisibility( View.VISIBLE);
            }
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate( R.menu.menu_main, menu );
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected( item );
        }
    }
公共类BowlerActivity扩展了AppCompatActivity{
私人BowlerAdapter mAdapter;
private List bowlersList=new ArrayList();
私人协调人布局协调人布局;
私人回收站;
私有文本视图noBowlersView;
专用数据库;
私有文本查看myText;
私有字符串savedExtra;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bowler);
//Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
//设置支持操作栏(工具栏);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//getSupportActionBar().setDisplayShowHomeEnabled(true);

savedExtra=String.valueOf(getIntent().getIntExtra(“leagueId”,2));//因此,由于我发布的评论对您有效,我决定将ehr解决方案发布在这里,以提高其可视性并帮助其他用户

解决方案

您必须在
清单
中将主题设置为您的
BowlerActivity

<activity 
   androidd:name=".views.BowlerActivity" 
   android:theme="@style/AppTheme" />


这似乎很明显,但您是否厌倦了在清单中为您的
BowlerActivity
设置主题?
?是的,您提到了这一点,它确实看起来非常明显。这就是问题所在,感谢您提醒我注意这一点。