无法启动活动ComponentInfo您需要在此活动中使用Theme.AppCompat主题



为什么我无法启动管理

package cse.svu;
/**
 * Created by DELL on 11/11/2014.
 */
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;
import java.util.Timer;
import java.util.TimerTask;
public class splashscreen extends Activity {
    long delay=8000;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Remove the Title Bar
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            // Get the view from splash_screen.xml
            setContentView(R.layout.splash_screen);
            // Create a Timer
            Timer RunSplash = new Timer();
            // Task to do when the timer ends
            TimerTask ShowSplash = new TimerTask() {
                @Override
                public void run() {
                    // Close SplashScreenActivity.class
                    finish();
                    // Start MainActivity.class
                    Intent myIntent = new Intent(splashscreen.this,MainActivity.class);
                    startActivity(myIntent);
                }
            };
            // Start the timer
            RunSplash.schedule(ShowSplash, delay);
        }
//METHOD 2
		/*
		new Handler().postDelayed(new Runnable() {
            // Using handler with postDelayed called runnable run method
            @Override
            public void run() {
                Intent i = new Intent(MainSplashScreen.this, FirstScreen.class);
                startActivity(i);
                // close this activity
                finish();
            }
        }, 5*1000); // wait for 5 seconds
    }*/
}
所以请在我的程序中找到解决方案

package cse.svu;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
    Button con,about_svu,academic,admin,campus,research;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        con=(Button)findViewById(R.id.contact);
        about_svu=(Button)findViewById(R.id.about);
        academic=(Button)findViewById(R.id.academic);
        admin=(Button)findViewById(R.id.admin);
        campus=(Button)findViewById(R.id.campus);
        research=(Button)findViewById(R.id.research);
        con.setOnClickListener(this);
        about_svu.setOnClickListener(this);
        academic.setOnClickListener(this);
        admin.setOnClickListener(this);
        campus.setOnClickListener(this);
        research.setOnClickListener(this);
       }
            @Override
       public void onClick(View v) {
               switch(v.getId()){
                    case R.id.contact:
                        Intent intent=new Intent(MainActivity.this,contact.class);
                        startActivity(intent);
                        break;
                    case R.id.about:
                        Intent intent1=new Intent(MainActivity.this,about_svu.class);
                        startActivity(intent1);
                        break;
                    case R.id.academic:
                        Intent intent2=new Intent(MainActivity.this,academic_excellence.class);
                        startActivity(intent2);
                        break;
                    case R.id.admin:
                        Intent intent3=new Intent(MainActivity.this,adminstration.class);
                        startActivity(intent3);
                        break;
                    case R.id.campus:
                        Intent intent4=new Intent(MainActivity.this,campus_facilities.class);
                        startActivity(intent4);
                        break;
                    case R.id.research:
                        Intent intent5=new Intent(MainActivity.this,researchandpublication.class);
                        startActivity(intent5);
                        break;
               }
       };
    @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);
    }
}

程序中出现的错误是java.lang.RuntimeException:无法启动活动ComponentInfo{cse.svu/cse.svu.MainActivity}:java.lang.IllegalStateException:您需要对此活动使用Theme.AppCompat主题(或子代)。在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2059)

1-您应该删除values-v21文件夹中的style.xml2-值文件夹中的style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
        <item name="colorPrimary">#ff0000</item>
    </style>
</resources>

和AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >
    </application>

祝你好运:)

如果在MainActivity中扩展ActionBarActivity,则还必须更改values-v11中的父主题。因此values-v11中的style.xml将是-

<!-- res/values-v11/themes.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="QueryTheme" parent="@style/Theme.AppCompat">
  <!-- Any customizations for your app running on devices with Theme.Holo here -->
</style>

ActionBarCompat问题

相关内容