user7160059 user7160059 - 24 days ago 14
Android Question

Move from Activity class to AppCompatActivity class

I'm trying to develop an Android Application that will play through a video and then transition to another activity.

The issue I'm having is that the video needs to be played FullScreen and in order to do so I had to switch the extends from "AppCompatActivity" to "Activity" otherwise the program would crash when trying to run full screen.

So when I transition to the next activity I had to call that class with extends Activity as well.
Now when I try to implement a drawer sidebar I'm running into issues because the class doesn't extend AppCompat, and is having explicit issues with "getSupportActionBar"

(The video plays, and transitions to the second screen without issue it's just proceeding from there that I'm caught on)

Is there any way to have my second activity extend AppCompat, without it crashing? Or if anyone is aware of a more effective way of implementing this?

public class Main extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_main);

VideoView splash = (VideoView) findViewById(R.id.SplashView);

Uri splashPath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.phsplash);


splash.setVideoURI(splashPath);
splash.start();

final int interval = 7000;
Handler handler = new Handler();
Runnable runnable = new Runnable(){
public void run(){

Intent goHome = new Intent(Main.this, HomeView.class);
startActivity(goHome);

}
};

handler.postAtTime(runnable, System.currentTimeMillis()+interval);
handler.postDelayed(runnable, interval);

}
}


xml



<VideoView
android:id="@+id/SplashView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />

</RelativeLayout>


There is then the Home view (Not finished writing due to AppCompat issue)

public class HomeView extends Activity {

private ListView theDrawer;
private ArrayAdapter<String> theAdapter;

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

theDrawer = (ListView)findViewById(R.id.drawlayout);

theOptions();
setupDrawer();

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void theOptions(){
String[] panelarray = {"About PHam", "Upcoming Projects", "Idea Submit", "Locate PHam"};
theAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, panelarray);
theDrawer.setAdapter(theAdapter);

theDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(HomeView.this, "WTF is Toast!", Toast.LENGTH_SHORT).show();
}
});
}
private void setupDrawer(){

}
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="This is the Home Screen!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView" />

<ListView
android:id="@+id/navlist"
android:layout_width="200dp"
android:layout_gravity="left|start"
android:layout_height="match_parent"
android:background="#ffeeeeee" />

</LinearLayout>


And then Finally the manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phsplash.captain.pcketham">

<application
android:allowBackup="true"
android:icon="@mipmap/phico"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeView"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black" >
</activity>

</application>

</manifest>


Log Cat Error Files when implementing Public class HomeView extends AppCompatActivity:

11-15 21:05:54.857 13627-13627/? E/Typeface: SANS_LOC file not found.
11-15 21:05:54.867 13627-13627/? E/Typeface: SANS_LOC file not found.
11-15 21:06:02.107 13627-13627/com.phsplash.captain.pcketham E/VdcInflateDelegate: Exception while inflating <vector>
java.lang.RuntimeException: Failed to resolve attribute at index 1
at android.content.res.TypedArray.getColorStateList(TypedArray.java:425)
at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:518)
at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:472)
at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436)
at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:708)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:348)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:83)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at com.phsplash.captain.pcketham.HomeView.onCreate(HomeView.java:22)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
11-15 21:06:02.117 13627-13627/com.phsplash.captain.pcketham E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.phsplash.captain.pcketham, PID: 13627
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phsplash.captain.pcketham/com.phsplash.captain.pcketham.HomeView}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.phsplash.captain.pcketham.HomeView.onCreate(HomeView.java:23)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5696) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 


Updated LogCat with
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"
:

11-15 21:37:15.627 27184-27184/? E/Typeface: SANS_LOC file not found.
11-15 21:37:15.627 27184-27184/? E/Typeface: SANS_LOC file not found.
11-15 21:37:22.867 27184-27184/com.phsplash.captain.pcketham E/VdcInflateDelegate: Exception while inflating <vector>
java.lang.RuntimeException: Failed to resolve attribute at index 1
at android.content.res.TypedArray.getColorStateList(TypedArray.java:425)
at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:518)
at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:472)
at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436)
at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:708)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:348)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:83)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at com.phsplash.captain.pcketham.HomeView.onCreate(HomeView.java:22)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
11-15 21:37:22.877 27184-27184/com.phsplash.captain.pcketham E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.phsplash.captain.pcketham, PID: 27184
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phsplash.captain.pcketham/com.phsplash.captain.pcketham.HomeView}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.phsplash.captain.pcketham.HomeView.onCreate(HomeView.java:23)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5696) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 

Answer

To solve appCompat error, you need to add support library to your app build.gradle dependencies:

compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:support-v4:24.2.0'

Remember to always adjust your androidBuildToolsVersion, androidCompileSdkVersion, and androidTargetSdkVersion with the same API version. In this case, you need to use API Level 24.

Then you can extend your Activity with AppCompatActivity, something like:

public class HomeView extends AppCompatActivity {
  ...
}

Read more for Support Library from:

  1. Support Library Features
  2. Support Library Setup

UPDATE

Based on your error log, it means that you need to use AppCompat as the theme for you activity. Change styles.xml resource with AppCompat, something like this:

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>   <!-- Your primary color -->
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>    <!-- Your dark color -->
    <item name="colorAccent">@color/colorAccent</item>    <!-- your accent color -->
  </style>

Then add the theme to your Activity or Application in AndroidManifest.xml with android:theme="@style/AppTheme" something like this:

<application
    android:allowBackup="true"
    android:icon="@mipmap/phico"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">