android tv新建空白页面

新建一个项目,选的是Android TV 的No Activity。设置好Activity和layout后,TV模拟器启动没反应,LOG有报错,我尝试用自动生成的activity,对着AndroidManifest设置也不行。

img

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-feature
        android:name="android.software.leanback"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.ActivityTest"
        android:banner="@drawable/app_icon_your_company">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/jdImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/xk"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent"/>

    <TextView
        android:id="@+id/jdName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="16dp"
        android:text="@string/text"
        android:textSize="50sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/tvButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginBottom="16dp"
        android:text="@string/button"
        android:textAllCaps="false"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

package com.example.activitytest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

如果您在使用Android TV的No Activity模板创建项目后遇到问题,模拟器没有反应并且在日志中有错误报告,我可以提供一些可能的解决方案:

  1. 检查模拟器配置:确保您的模拟器已正确配置并已启动。确保您使用的是最新版本的Android TV模拟器,并且已正确设置模拟器的目标API级别和其他配置选项。

  2. 检查错误日志:查看错误日志以获取更多详细信息。使用Android Studio或命令行工具运行模拟器,并查看日志输出。报告的错误可能会提供有关问题的线索,例如缺少依赖项或配置错误。

  3. 检查AndroidManifest.xml文件:确保您的AndroidManifest.xml文件正确设置了活动和其他必要的配置。检查以下几个方面:

    • 活动是否已正确声明为LAUNCHER类别,并且具有合适的intent-filter以启动应用程序。
    • 检查活动的名称、包名和导入语句是否正确。
    • 确保活动的布局文件与setContentView方法中引用的布局文件一致。
    • 确保您没有添加任何与Android TV不兼容的功能或权限。
  4. 检查布局文件:确保您的布局文件正确设置,并且与活动的内容视图相匹配。在Android TV上,通常使用Leanback库提供的TV布局和小部件。

  5. 检查依赖项:确保您的项目中包含所需的依赖项。检查您的build.gradle文件,确保已添加Android TV相关库和支持库的正确版本。您可以使用Android Studio的"Project Structure"对话框来管理依赖项。

  6. 清除构建并重新构建:有时,构建过程中可能会发生一些错误或缓存问题。尝试清除构建并重新构建项目。您可以在Android Studio中选择"Build"菜单中的"Clean Project"和"Rebuild Project"选项。

  • 这篇博客: Android TV开发(—)开始电视应用中的 声明一个电视的Activity 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • <application
      android:banner="@drawable/banner" >
      ...
      <activity
        android:name="com.example.android.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="com.example.android.TvActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.Leanback">
    
        <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
        </intent-filter>
    
      </activity>
    </application>

    category.LEANBACK_LAUNCHER 用于在Google Play上过滤你的应用,表示是一个电视App。