-1

I am creating android application which is open a webpage using webview but on run time I got an error . This is mobile js page in which is I want to open webview.

Log-cat :

    06-19 00:44:25.756: I/Process(1130): Sending signal. PID: 1130 SIG: 9
    06-19 00:46:45.136: D/AndroidRuntime(1156): Shutting down VM
    06-19 00:46:45.136: W/dalvikvm(1156): threadid=1: thread exiting with uncaught exception (group=0xb4a48ba8)
    06-19 00:46:45.156: E/AndroidRuntime(1156): FATAL EXCEPTION: main
    06-19 00:46:45.156: E/AndroidRuntime(1156): Process: com.example.dynakodetechnologypvt.ltd, PID: 1156
    06-19 00:46:45.156: E/AndroidRuntime(1156): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dynakodetechnologypvt.ltd/com.example.dynakodetechnologypvt.ltd.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.os.Handler.dispatchMessage(Handler.java:102)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.os.Looper.loop(Looper.java:136)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at java.lang.reflect.Method.invoke(Method.java:515)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at dalvik.system.NativeStart.main(Native Method)
    06-19 00:46:45.156: E/AndroidRuntime(1156): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at com.example.dynakodetechnologypvt.ltd.MainActivity.onCreate(MainActivity.java:19)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.app.Activity.performCreate(Activity.java:5231)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    06-19 00:46:45.156: E/AndroidRuntime(1156):     ... 11 more

Main Activity

@SuppressLint("SetJavaScriptEnabled") 
public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        String url = "http://www.google.com";

        WebView view =   (WebView) this.findViewById(R.id.webView1);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl(url);
    }


    @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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

Android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dynakodetechnologypvt.ltd"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <activity
            android:name="com.example.dynakodetechnologypvt.ltd.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>
    </application>

</manifest>

How can I solve this please help

Krupa Patel
  • 3,309
  • 3
  • 23
  • 28
user3547883
  • 83
  • 3
  • 11
  • 1
    Did you check the exception? _You need to use a Theme.AppCompat theme (or descendant) with this activity._ – matiash Jun 19 '14 at 05:11
  • possible duplicate of http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity – Giru Bhai Jun 19 '14 at 05:12
  • [http://developer.android.com/guide/topics/ui/actionbar.html](http://developer.android.com/guide/topics/ui/actionbar.html) – M D Jun 19 '14 at 05:16
  • i am change the theme bt error is still – user3547883 Jun 19 '14 at 05:22
  • check you imports for `ActionBarActivity`. it should be using ` support library` – maddy d Jun 19 '14 at 05:34

1 Answers1

1

ActionBarActivity requires a theme that extends from Theme.AppCompat, otherwise your app will crash with this error:

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

So in Manifest file use the new theme with MainActivity: as

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dynakodetechnologypvt.ltd"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <activity
            android:name="com.example.dynakodetechnologypvt.ltd.MainActivity"
            android:label="@string/app_name" 
            android:theme="@style/AppTheme.AppCompat"> //Add here
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

This will add Actionbar in MainActivity
But usually you need to customize you Actionbar. To do this you need to create two styles with Theme.AppCompat parent, for example, "@style/Theme.AppCompat.Light". First one will be for api 11>= (versions of android with build in android actionbar), second one for api 7-10 (no build in actionbar).

Let's look at first style. It will be located in res/values-v11/styles.xml . It will look like this:

<style name="Theme.Styled" parent="@style/Theme.AppCompat.Light">
    <!-- Setting values in the android namespace affects API levels 11+ -->
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!-- Setting values in the android namespace affects API levels 11+ -->
    <item name="android:background">@drawable/ab_custom_solid_styled</item>
    <item name="android:backgroundStacked"
      >@drawable/ab_custom_stacked_solid_styled</item>
    <item name="android:backgroundSplit"
      >@drawable/ab_custom_bottom_solid_styled</item>
</style>

And you need to have same style for api 7-10. It will be located in res/values/styles.xml, BUT because that api levels don't yet know about original android actionbar style items, we should use one, provided by support library. ActionBar Compat items are defined just like original android, but without "android:" part in the front:

<style name="Theme.Styled" parent="@style/Theme.AppCompat.Light">
    <!-- Setting values in the default namespace affects API levels 7-11 -->
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!-- Setting values in the default namespace affects API levels 7-11 -->
    <item name="background">@drawable/ab_custom_solid_styled</item>
    <item name="backgroundStacked">@drawable/ab_custom_stacked_solid_styled</item>
    <item name="backgroundSplit">@drawable/ab_custom_bottom_solid_styled</item>
</style>

For more info see ActionBarCompat and I/O 2013 App Source

Giru Bhai
  • 14,370
  • 5
  • 46
  • 74