首页 > 解决方案 > 尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setTypeface(android.graphics.Typeface)”

问题描述

我正在尝试为我的 android 应用程序的数组列表设置字体,但每次构建项目时都会出现以下错误:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
                  at com.example.hussienalrubaye.quranonline.RecitesName.onCreate(RecitesName.java:55)

这是代码,具体而言,我在包含以下内容的行中收到错误消息:txtRecitesName.setTypeface(tfrn); 因为他认为 txtRecitesName 是一个空对象......但在我的代码中我已经初始化了它

public class RecitesName extends AppCompatActivity {
   public ArrayList<AuthorClass> listrecites = new ArrayList<AuthorClass>();
    ListView lVRecites;
    TextView rn;
    InterstitialAd mInterstitialAd;
    String RecitesName="";
    Typeface tfrn;
    TextView txtRecitesName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recites_name);
        // check if he load for first time to selling the language
        SaveSettings sv = new SaveSettings(this);
        sv.LoadData();
        tfrn = Typeface.createFromAsset(getAssets(),"fonts/Amiri-Bold.ttf");
        txtRecitesName = (TextView) findViewById(R.id.txtRecitesName);
       txtRecitesName.setTypeface(tfrn);

        lVRecites = (ListView) findViewById(R.id.listView);
//get list of recites
        LnaguageClass lc = new LnaguageClass();
        listrecites = lc.AutherList();
        lVRecites.setAdapter(new VivzAdapter(listrecites));
        lVRecites.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //load recites aya


               // AuthorClass temp = listrecites.get(position);
                for (AuthorClass listrecitesitem : listrecites) {
                    if (listrecitesitem.RealName.equals(txtRecitesName.getText())) {
                        RecitesName = listrecitesitem.ServerName;
                       // String welcomes = listrecitesitem.ServerName;
                        DisplayAya();
                        break;
                    }
                }

            }
        });
        if(SaveSettings.OnTimeAds==false) {
            if(SaveSettings.IsRated==1){
            SaveSettings.OnTimeAds=true;
            }

        }
    }

这是我的 ttf 字体文件位置的图片:

在此处输入图像描述

在下面你会找到我的布局活动文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
android:orientation="vertical"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="82dp"
    android:layout_marginTop="2pt"
    android:background="@drawable/bglist"
    android:orientation="horizontal"
    android:paddingBottom="4pt"
    android:paddingTop="4pt">

    <ImageView
        android:id="@+id/imgchannel"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="7dp"
        android:layout_weight="0"
        android:background="@android:color/transparent"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@drawable/quran_recites" />

    <TextView
        android:id="@+id/txtRecitesName"
        android:layout_width="308dp"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/imgchannel"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/imgchannel"
        android:layout_weight="1"
        android:background="@null"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="right"
        android:paddingRight="7dp"
        android:paddingTop="8dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textSize="20dp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="20dp"
        android:layout_height="43dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_weight="0"
        android:background="@null"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:paddingTop="5dp"

        android:src="@drawable/backirrow" />
</LinearLayout>

标签: javaandroidtypeface

解决方案


推荐阅读