2017-08-10 36 views
0

剛剛接觸android編程,請耐心等待。在文本視圖上設置字體系列時出現渲染錯誤

當我在一個活動的textView中設置fontFamily時,我得到一個「渲染錯誤」 - 無法實例化一個或多個類。


以下類不能instatiated: - android.support.v7.widget.AppCompatTextView(...)

這究竟是爲什麼?我究竟做錯了什麼?

我試圖創建一個空的活動,沒有任何操作欄,所以我使用了一個名爲AppCompat的東西。 (不知道那是什麼),但互聯網上的例子告訴我使用

<style name="MainTheme" parent="Theme.AppCompat.NoActionBar"> 
    <item name="android:windowBackground">@drawable/main_background</item> 
    <item name="android:textColor">@color/black</item> 
</style> 

我設置了他們清單下的應用:

 <activity 
     android:name=".TestActivity" 
     android:theme="@style/MainTheme" /> 

類文件是空的,除了自動生成的代碼。

在佈局文件中,我添加了一個textView並將fontFamily設置爲除空以外的任何內容。例如。 「無襯線字體」。 (在我設置fontFamily之前沒有渲染錯誤)。

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.constraint.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="net.phonefly.game.TestActivity"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:fontFamily="sans-serif" 
     android:text="TextView" 
     tools:layout_editor_absoluteX="190dp" 
     tools:layout_editor_absoluteY="16dp" /> 
</android.support.constraint.ConstraintLayout> 

我的依賴關係:

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    //For bottom navigation bar: 
    //End of bottom navigation bar. 


    compile 'com.android.support:appcompat-v7:26.+' 
    compile 'com.mcxiaoke.volley:library:1.0.19' 
    compile 'com.android.support:design:26.+' 
    compile 'com.android.support:support-vector-drawable:26.+' 
    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    testCompile 'junit:junit:4.12' 
} 

回答

1

在您的應用程序gradle這個只是改變了庫版本,並使用一些比26+喜歡低於下:

編譯「com.android.support:appcompat-v7:25.3.1」

+0

我得到一個錯誤「這個支持庫不應該使用與compileSdkVersion(26)不同的版本(25)。 – Easyrider

+0

你必須改變你的編譯版本,它可能與庫版本相同 –

+0

它似乎現在工作,我將「compileSdkVersion」和「targetSdkVersion」都更改爲25,這是否正確?我想知道爲什麼我必須這樣做?我現在選擇某些庫的舊版本嗎?爲什麼26版本不能工作?謝謝! – Easyrider

1

你應該在你的項目級的build.gradle文件檢查庫引用您的字體像這樣的@ font /無襯線

android:fontFamily="@font/sans-serif" 
+0

'@字體/ sans- serif'沒有這樣的財產 –

+0

然後你的字體丟失。要使用自定義字體,您需要將它們添加到字體資源中。 – jbarat

+0

我使用設計模式更改了字體,其中我從下拉列表中選擇了字體。我沒有手動編輯它。我應該改變標籤的價值嗎? – Easyrider

1

包含了新的谷歌的maven like below

allprojects { 
    repositories { 
     jcenter() 
     maven { 
      url "https://maven.google.com" 
     } 
    } 
} 
+0

對不起,忘了說我也有這個部分。所以這是alreade照顧。 – Easyrider