0

I have been having this problem in Android studio where I create a project, and I try to change the theme but it always gives me rendering problems. I try to change the styles.xml but it doesn't really seem to work. Here's the theme I want: Material Light

Whenever I try to select it, I get rendering problems: Rendering Problems

Here's the error details:

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:192)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:186)
at sun.reflect.GeneratedConstructorAccessor271.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:465)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:172)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:186)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:334)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:345)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:245)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:548)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:533)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$72(RenderTask.java:659)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

As seen from the first line of the error log, it says

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.

So, Naturally I select the Appcompat Theme like so: AppCompat

And this works without any problems: AppCompat Theme

MainActivity.java file as requested:

 package com.example.moham.currencyconverter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

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

How can I make sure that I can use any theme I want and make it work without rendering problems?

edited the java file to extend Activity, and that didn' solve the problem. Instead, I now have an additional problem in the java code: enter image description here java file

aabb bbaa
  • 195
  • 1
  • 3
  • 14
  • 1
    Your primary issue is not related to the rendering problems, as that is mostly just IDE bugs. If you are using `AppCompatActivity`, you have to use a theme inherited from `Theme.AppCompat`. If you do not want to use a theme inherited from `Theme.AppCompat`, stop using `AppCompatActivity`. – CommonsWare Oct 08 '16 at 17:39
  • post your Acvtivity class – Nickan Oct 08 '16 at 17:39
  • @NickanB: Do you mean the MainActivity.java? – aabb bbaa Oct 08 '16 at 18:03
  • yes , MainActivity.java – Nickan Oct 08 '16 at 18:04
  • @CommonsWare: Where do I change that? I'm sorry if this seems like a noob question, but that's because it is. – aabb bbaa Oct 08 '16 at 18:05
  • @NickanB Done.. – aabb bbaa Oct 08 '16 at 18:10
  • Possible duplicate of [You need to use a Theme.AppCompat theme (or descendant) with this activity](http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity) – Leb Dec 23 '16 at 22:13

2 Answers2

0

As @CommonsWare rightly mentioned, your activity is extending AppCombatActivity due to which you have to use AppCombat theme.

Simply extend Activity instead of AppCombatActivity. It'll solve your problem.

But prefer to use AppCombatActivity as you'll be able to easily use design widgets like floatingActionButton, etc. Without AppCombat theme and activity you'll not be able to use them. Hope it helps.

Check this link, link2 to know why to use AppCombatActivity instead of ActionBarActivity, Activity, etc.

Community
  • 1
  • 1
Vishal Puri
  • 823
  • 8
  • 15
  • Do you mean extend it in the MainActivity.java (Code just posted in the question above)? – aabb bbaa Oct 08 '16 at 18:13
  • @uddinstock yes exactly. – Vishal Puri Oct 08 '16 at 18:16
  • Why do you want to use activity in first place? If you remove AppCombatActivity various errors(error inflating floating button,error inflating coordinator layout) will occur. As a android dev you hav to update yourself with every google updates, not downgrade. Check the link i added in my answer. – Vishal Puri Oct 09 '16 at 02:08
0

As @CommonsWare and @Vishal Puri explain to you , your problem should solve if you Change

public class MainActivity extends AppCompatActivity {

To

public class MainActivity extends Activity {
Nickan
  • 466
  • 5
  • 17