0
我在使用自定義字體時出現此錯誤。我曾嘗試使用MainActivity中的代碼和相應的XML活動(查詢)(在這種情況下,沒有錯誤,但字體不會適用)。在Android中使用自定義字體的NullPointerException
MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
WebView webView;
TextView tv_appname;
Typeface typeface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String fontpath="font/batman.ttf";
tv_appname=(TextView)findViewById(R.id.tv_appname);
typeface= Typeface.createFromAsset(getAssets(),fontpath);
tv_appname.setTypeface(typeface);
胡亞蓉調查(其中文本放在我想改變其字體)
<LinearLayout 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"
android:background="@drawable/bg"
tools:context="mayank2511.galgotiasuniversity.EnquiryActivity"
android:id="@+id/nav_enq"
android:paddingLeft="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enquiry Form"
android:textSize="34sp"
android:textColor="#fff"
android:id="@+id/tv_appname"
android:layout_marginTop="10dp"
android:gravity="center"/>
探究活動(櫃面它helps-它的評論RN)
public class EnquiryActivity extends AppCompatActivity {
/* TextView tv_appname;
Typeface typeface; */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enquiry);
/*String fontpath="font/batman.ttf";
tv_appname=(TextView)findViewById(R.id.tv_appname);
typeface= Typeface.createFromAsset(getAssets(),fontpath);
tv_appname.setTypeface(typeface);
getSupportActionBar().hide();*/
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
Font Directory and main activity
tv_appname.setTypeface(字體) 這是拋出NPE這是主要問題。 –
這是因爲您在主Activity中聲明瞭tv_app ...,而不是它所屬的Activity。 –
是的。顯然,你使用2種不同的佈局('activity_enquiry'和'activity_main'),但使用相同的代碼。確保你有一個ID爲'tv_appname'的TextView –