应用TypeFace时出现NullPointerException

时间:2014-04-24 21:29:59

标签: android casting android-typeface

package com.example.kym;  
import android.content.SharedPreferences;  
import android.graphics.Typeface;  
import android.graphics.drawable.Drawable;  
import android.os.Bundle;  
import android.preference.PreferenceManager;  
import android.support.v7.app.ActionBarActivity;  
import android.widget.ImageView;  
import android.widget.TextView;  
public class Accumulation extends ActionBarActivity  
{  
    TextView tv1,tv2;  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        tv1=(TextView)findViewById(R.id.acc1);  
        Typeface font=null;  
        tv2=(TextView)findViewById(R.id.acc2);  
        try  
        {  
            font = Typeface.createFromAsset(getAssets(),"fonts/Devanagari.ttf");  
            getData= PreferenceManager.getDefaultSharedPreferences(getBaseContext());  
            String s=getData.getString("lang", "E");  
            if(s.contentEquals("E"))  
                setContentView(R.layout.accumulation);  
            else if(s.contentEquals("H"))  
            {  
                setContentView(R.layout.accumulationh);  
                tv1.setTypeface(font);  
                tv1.setText("कर दिया");  
            }  
            else if(s.contentEquals("B"))  
            {  
                setContentView(R.layout.accumulationb);  
            }  
            getSupportActionBar().setTitle("Accumulation Distribution");  
            Drawable d=getResources().getDrawable(R.drawable.blue);  
            getSupportActionBar().setBackgroundDrawable(d);  
        }  
        catch(NullPointerException e)  
        {  
            e.printStackTrace();  
        }  
    }  
}  




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@drawable/blue"
    >
<ScrollView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarStyle="insideInset">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">    
<TextView
    android:id="@+id/accimg1"
    android:layout_width="wrap_content"
    android:textColor="@color/black"
    android:layout_height="wrap_content"
    android:text=""/>
<ImageView 
    android:id="@+id/acc1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    />
<TextView 
    android:id="@+id/acc2"
    android:textColor="@color/black"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"/>

</LinearLayout>
</ScrollView>
</LinearLayout>

为什么会有ClassCastException

我正在尝试在textView1上使用自定义TypeFace,但每次都会给出相同的错误。 TextView1为空&amp;我想使用字体来实现不同的语言。但每次它都显示为空白,即使我应用了tv1.setText。

1 个答案:

答案 0 :(得分:0)

在您通过调用其中的findViewById()或手动对其进行充气之后,您无法致电setContentView()