I had an old project in Eclipse and I imported it into Android Studio to rebuild it with Material Design.
I done all things...
configured build.gradle and dependencies:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.mhrz.Minesweeper"
minSdkVersion 8
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:cardview-v7:+'
}
and styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#1E9618</item>
<item name="colorPrimaryDark">#146310</item>
<item name="colorAccent">#1E9618</item>
</style>
</resources>
and this is Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mhrz.Minesweeper"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MinesweeperGame"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and also CardView works well.
The problem is that app works correctly with this and give me Holo Design:
public class MinesweeperGame extends Activity {
but I want Material and I get FC on:
public class MinesweeperGame extends AppCompatActivity {
with this log:
01-03 16:29:38.779 12720-12720/com.mhrz.Minesweeper E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mhrz.Minesweeper, PID: 12720
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mhrz.Minesweeper/com.mhrz.Minesweeper.MinesweeperGame}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at com.mhrz.Minesweeper.MinesweeperGame.onCreate(MinesweeperGame.java:55)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Where is mistake? How to fix it?
Android system doesn't know that you want to use alternate theme for application (like yours AppTheme) until you define it in application or activity attribute inside AndroidManifest.xml file.
You need to add the android:theme="#style/AppTheme" attribute to your <application> tag in AndroidManifest.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mhrz.Minesweeper"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".MinesweeperGame"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Related
API Version Incompatibilities
I have made an app using android studio, until now I have been testing it on a 'Nexus 5X (API 23) emulator'. However, today i started debugging the app on a 'Samsung Galaxy Tab 2 7.0 (API 18)' and the app will not even start. I know this is because of incompatibilities between the API's specifically something to do with the Theme i'm using. What i want to know is how i can fix this problem without changing the look of the app drastically.
Log Cat Crash Log
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.***.********/com.***.********.MenuActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:124)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at com.***.*******.MenuActivity.onCreate(MenuActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MenuActivity"
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=".About" />
<activity android:name=".SettingsActivity" />
<activity android:name=".********Activity" />
<activity android:name=".********Activity" />
<activity
android:name=".VideoPlayerActivity"
android:screenOrientation="landscape" />
<activity android:name=".*******Activity"></activity>
</application>
Gradle Build App File
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.***.*******"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "2.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
}
Note: The last two lines (the ones with compile...) are underlined in red and say 'This support library should not use a different version(22) than the compile SDK version(21).'
Edit: If I change the Theme for the main activity to '#style/Theme.AppCompat.NoActionBar' the application will start however, the look of the application changes. Also, other activities that do not use the new theme work fine.
Any Help is Appreciated.
I know it is simple but I wasnt able to fix it.Tried many solution from SOF.com but none worked. I am trying to display a title for activity and throws NPE. Using API19 for testing.Below are the codes:
In the activity:
public class DisplayImageActivity extends RootActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().setTitle("Image Preview");
In the Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".DisplayImageActivity"
android:label="< app name >"
android:screenOrientation="portrait" >
</activity>
In the style.xml:
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
Finally, the logcat says:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kittu.<> /com.example.kittu.<>.DisplayImageActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.kittu.< >.DisplayImageActivity.onCreate(DisplayImageActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
If you are using the support library, your activity needs to extend it
import android.support.v7.app.AppCompatActivity
public class MainActivity extends AppCompatActivity{
and use getSupportActionBar() instead of getActionBar()
I think you are using AppCompat, then you should use getSupportActionBar() instead of getActionBar().
Change this:
getActionBar().setTitle("Image Preview");
to this:
getSupportActionBar().setTitle("Image Preview");
Hope this helps :)
I know that this question has been asked before, but the provided answers did not solve my case, so I'll ask for an individual solution.
My code
My AndroidManifest.xml looks like this:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".KategorieAuswahl"
android:label="#string/title_activity_kategorie_auswahl" >
</activity>
<activity
android:name=".ZeigeFragen"
android:label="#string/title_activity_zeige_fragen" >
</activity>
</application>
And my styles.xml looks like this:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
I want my App to open in fullscreen and to hide the TitleBar. The first thing I did was to add a android:theme="#android:style/Theme.NoTitleBar.Fullscreen" attribute to the <application>-Tag of my AndroidManifest.xml.
This leads to the error You need to use a Theme.AppCompat theme (or descendant) with this activity. Here is the full stacktrace:
05-27 21:24:26.889 2121-2121/de.test.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.test.myapp/de.test.myapp.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at de.test.myapp.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
There are multiple questions on stackoverflow referencing this error.
Approach #1
One solution suggests to change the Java inheritance from ActionBarActivity to Activity and leave the manifest as it is.
So I updated my the <style> element in my styles.xml and removed the DarkActionBar part:
<style name="AppTheme" parent="Theme.AppCompat.Light">
This still gives me the same error.
Approach #2
The next solution suggests to change my styles.xml's parent attribute from
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to <style name="AppTheme" parent="Theme.AppCompat">
This makes it possible to use android:theme="#style/Theme.AppCompat.NoActionBar" for my <activity>, but I don't see an option to make it fullscreen then.
Approach #3
Another solution soggests to add <item name="android:windowNoTitle">true</item> to my <style> element but this doesn't change anything at all.
Conclusion
I really tried a lot, but nothing seems to work. Anyway I have problems understanding the theme inheritance chain and when I may use .NoTitleBar and .Fullscreen.
After all I don't care which theme to use, as long I can make the app fullscreen without a title bar. Can you help me to pick the right attributes?
extend ActionBarActivity or the new one, and use Theme.AppCompat.NoActionBar, now in your Activity
if (Build.VERSION.SDK_INT < 16) {
Activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}else{
Activity.getWindow().getDecorView().
setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}
Hope it helps..
I refactored my package name and put my CuteMainActivityin the package org.azurespot.cutemain.CuteMainActivity. Plus rearranged all my other classes. So now I get a red error on the .CuteMainActivity part in my Manifest, where the activity should launch.
I also changed the top package name of my Manifest to the new one: org.azurespot, and also changed my applicationID in my build.gradle file to the new package name org.azurespot too. Then I rebuilt, cleaned, and even tried invalidate and restart to my project but nothing is getting rid of this red error. I also tried to assign the new package/activity name to launch in my run configurations, and got this error:
Launching application: org.azurespot/org.azurespot.cutemain.CuteMainActivity.
DEVICE SHELL COMMAND: am start -n "org.azurespot/org.azurespot.cutemain.CuteMainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=org.azurespot/.cutemain.CuteMainActivity }
Error type 3
Error: Activity class {org.azurespot/org.azurespot.cutemain.CuteMainActivity} does not exist
One strange thing, is that my package name has the word (androidTest) beside it. Would that make things not work? Usually studio creates a regular package name and a package_name (androidTest), but in the refactoring, I only see this (androidTest) one.
I have been researching this problem for a while, but nothing seems to work. Any suggestions? Thank you.
Manifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.azurespot" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".CuteMainActivity" //This has a red error
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
LogCat after changed Manifest name to ".cutemain.CuteMainActivity"
1480-1480/org.azurespot E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.azurespot, PID: 1480
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.azurespot/org.azurespot.cutemain.CuteMainActivity}: java.lang.ClassNotFoundException: Didn't find class "org.azurespot.cutemain.CuteMainActivity" on path: DexPathList[[zip file "/data/app/org.azurespot-2.apk"],nativeLibraryDirectories=[/data/app-lib/org.azurespot-2, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.azurespot.cutemain.CuteMainActivity" on path: DexPathList[[zip file "/data/app/org.azurespot-2.apk"],nativeLibraryDirectories=[/data/app-lib/org.azurespot-2, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
You should change it to one of the followings:
android:name="org.azurespot.cutemain.CuteMainActivity"
or
android:name=".cutemain.CuteMainActivity"
Ah... the package name with (androidTest) was indeed the culprit. All of the above answers should have fixed things, but the final problem was that when I refactored (I have no idea how it happened), all of my new package names got put into my androidTest directory in the src folder, and nothing was in my main of my src folder. So when my app went looking for the main activity, it could not be found. The fix to this was to put the whole java folder back into the main folder (as it was empty for some reason), then restart the app and everything worked again. It was not necessary to delete the androidTest folder, and it seems fine that it's empty.
This question already has answers here:
You need to use a Theme.AppCompat theme (or descendant) with this activity
(63 answers)
Closed 8 years ago.
I am a beginner and have been using treehouse to learn. I am very early in the course and suddenly started getting a crash message when I start the app. I am unsure what is happening. I have posted everything that I know of. Sorry if I missed something or posted too much.
Main:
package com.example.crystalball;
import android.R.string;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private CrystalBall mCrystalBall = new CrystalBall();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our View variables
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String answer = mCrystalBall.getAnAnswer();
// Update label with dynamic answer
answerLabel.setText(answer);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CrystalBall:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.crystalball"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.crystalball.MainActivity"
android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.crystalball"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.crystalball.MainActivity"
android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat:
07-28 01:50:31.530: D/AndroidRuntime(980): Shutting down VM
07-28 01:50:31.530: W/dalvikvm(980): threadid=1: thread exiting with uncaught exception (group=0xb1ae7ba8)
07-28 01:50:31.570: E/AndroidRuntime(980): FATAL EXCEPTION: main
07-28 01:50:31.570: E/AndroidRuntime(980): Process: com.example.crystalball, PID: 980
07-28 01:50:31.570: E/AndroidRuntime(980): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.os.Handler.dispatchMessage(Handler.java:102)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.os.Looper.loop(Looper.java:136)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-28 01:50:31.570: E/AndroidRuntime(980): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 01:50:31.570: E/AndroidRuntime(980): at java.lang.reflect.Method.invoke(Method.java:515)
07-28 01:50:31.570: E/AndroidRuntime(980): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-28 01:50:31.570: E/AndroidRuntime(980): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-28 01:50:31.570: E/AndroidRuntime(980): at dalvik.system.NativeStart.main(Native Method)
07-28 01:50:31.570: E/AndroidRuntime(980): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-28 01:50:31.570: E/AndroidRuntime(980): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
07-28 01:50:31.570: E/AndroidRuntime(980): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:16)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.Activity.performCreate(Activity.java:5231)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-28 01:50:31.570: E/AndroidRuntime(980): ... 11 more
07-28 01:50:37.070: I/Process(980): Sending signal. PID: 980 SIG: 9
Your Activity extends ActionBarActivity.
public class MainActivity extends ActionBarActivity
Indicates you are using AppCompat in which case you need to set Theme derieved from Theme.AppCompat to the Activity
or
android:theme="#style/Theme.AppCompat" >
set the theme for the application
To simply add ActionBar Compat your activity or application should use #style/Theme.AppCompat theme in AndroidManifest.xml like this:
<activity
...
android:theme="#style/Theme.AppCompat" />
This will add actionbar in activty(or all activities if you added this theme to application)
i.e. in your manifest file,Change
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
to
android:theme="#style/Theme.AppCompat"
Please change
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
to
android:theme="#android:style/Theme.AppCompat"