2016-07-29 6 views
1

私は下線付きのコードはありませんが、エミュレータで私のアプリケーションを起動するとすぐに、私はこれらのエラーを受け取ります。TextViewでカスタムフォントを設定しようとしています - nullpointer

FATAL EXCEPTION: main 
Process: com.milos.sportisa, PID: 2735 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.milos.sportisa/com.milos.sportisa.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
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.milos.sportisa.SplashActivity.onCreate(SplashActivity.java:29) 
at android.app.Activity.performCreate(Activity.java:5933) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  
at android.app.ActivityThread.access$800(ActivityThread.java:144)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:135)  
at android.app.ActivityThread.main(ActivityThread.java:5221)  
at java.lang.reflect.Method.invoke(Native Method)  
at java.lang.reflect.Method.invoke(Method.java:372)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

そして、これは私のSplashActivityです:

public class SplashActivity extends AppCompatActivity { 
    private TextView etSportisa; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_splash); 
     /* ButterKnife.inject(this); 
     mProgressBar.setIndeterminateDrawable(new foldingCirclesDrawable.Builder(this) 
       .build()); 
     mProgressBar.setIndeterminateDrawable(new foldingCirclesDrawable.Builder(this) 
       .colors(getResources().getIntArray(R.array.colors) //Array of 4 colors 
         .build()); */ 

     Typeface myTypeFace = Typeface.createFromAsset(getAssets(), "fonts/esp_ital.ttf"); 
     etSportisa.setTypeface(myTypeFace); 
     etSportisa = (TextView) findViewById(R.id.tv_sportisa); 



     final ProgressDialog pd = new ProgressDialog(SplashActivity.this); 
     pd.setMessage("Učitavanje..."); 
     pd.show(); 

     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      public void run() { 
       // Actions to do after 10 seconds 
       pd.dismiss(); 

       Intent i = new Intent(SplashActivity.this, MainActivity.class); 
       startActivity(i); 
      } 
     }, 2000); 
    } 
} 

特殊なフォントは、フォントフォルダ\資産に追加されます。私もロード画面用の画像を追加しましたが、私はフォントがこれらの問題

+0

* - うん、あなたのコードがコンパイルされているため。あなたはRuntimeExceptionを持っています –

答えて

3

があなたのコードを次のように変更しますいとこであることを考える:

Typeface myTypeFace = Typeface.createFromAsset(getAssets(), "fonts/esp_ital.ttf"); 
etSportisa = (TextView) findViewById(R.id.tv_sportisa); 
etSportisa.setTypeface(myTypeFace); 

初期化した後、あなたはどんなレイアウトで任意の操作を行うことができます。

+0

ありがとうございました!問題は解決しました! :) – ajzaklija

1

問題はここにある:

etSportisa

セットそれnullの後、私は何の下線付きのコードを持っていません*あなたfindViewByID

Typeface myTypeFace = Typeface.createFromAsset(getAssets(), "fonts/esp_ital.ttf"); 
     etSportisa = (TextView) findViewById(R.id.tv_sportisa); 
     etSportisa.setTypeface(myTypeFace); 
+0

あなたはちょうど既存のコードをコピーして貼り付けました – SoroushA

+0

@SoroushA私は知っています。私が投稿している間、私はエラーが発生しました。私はそのページをリフレッシュした。古いコードにデフォルト設定されています – Shank

+0

ありがとうございました!問題は解決しました! :) – ajzaklija

関連する問題