从Activity类移到AppCompatActivity类

时间:2016-11-15 04:48:39

标签: java android xml

我正在尝试开发一个Android应用程序,该应用程序将播放视频然后转换到另一个活动。

我遇到的问题是视频需要播放FullScreen,为了做到这一点,我必须切换来自" AppCompatActivity"到"活动"否则程序会在尝试全屏运行时崩溃。

因此,当我转换到下一个活动时,我必须使用extends Activity调用该类。 现在,当我尝试实现抽屉侧边栏时,我遇到了问题,因为该课程没有扩展AppCompat,并且明显存在" getSupportActionBar"

(视频播放,并转换到第二个屏幕而不会发出它只是从那里开始我已经被抓住了)

有没有办法让我的第二个活动扩展AppCompat,而不会崩溃?或者,如果有人知道更有效的实现方法吗?

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>

然后是Home视图(由于AppCompat问题而没有写完)

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>

然后最后清单

<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错误文件HomeView扩展了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) 

使用<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"更新了LogCat:

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) 

2 个答案:

答案 0 :(得分:3)

AppCompatActivity 而不是活动扩展您的第二堂课。

public class HomeView extends AppCompatActivity

快乐的编码。干杯!

修改
对于那些只是低估了我的答案的伟大,这里是一段代码片段而没有来自Android Studio的任何错误:

public class test extends AppCompatActivity{
  private ListView theDrawer;
  private ArrayAdapter<String> theAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
  }
}

以下是有关支持操作栏的必要文档的link

答案 1 :(得分:1)

要解决 appCompat 错误,您需要将支持库添加到您的app build.gradle依赖项:

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

请务必使用相同的API版本调整androidBuildToolsVersionandroidCompileSdkVersionandroidTargetSdkVersion。在这种情况下,您需要使用API​​ Level 24。

然后,您可以使用Activity扩展您的AppCompatActivity,例如:

public class HomeView extends AppCompatActivity {
  ...
}

支持资料库了解详情:

  1. Support Library Features
  2. Support Library Setup
  3. <强>更新

    根据您的错误日志,这意味着您需要使用AppCompat作为您活动的主题。使用AppCompat更改styles.xml资源,如下所示:

      <!-- 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>
    

    然后将主题添加到 AndroidManifest.xml 中的活动或应用程序,其中android:theme="@style/AppTheme"为:

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